Skip to content

Commit

Permalink
Allow Hiding of Calendar Events (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobkoerber authored Oct 8, 2024
1 parent bcbd3cc commit 024d834
Show file tree
Hide file tree
Showing 30 changed files with 449 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import de.tum.`in`.tumcampus.R
import de.tum.`in`.tumcampus.util.Const
import es.antonborri.home_widget.HomeWidgetPlugin
import kotlinx.serialization.json.Json
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.TextStyle
import java.util.*
Expand All @@ -34,6 +35,10 @@ class CalendarWidgetService : RemoteViewsService() {
calendarEvents = Json.decodeFromString<Array<WidgetCalendarItem>>(data).asList()
}

calendarEvents.filter { widgetCalendarItem ->
widgetCalendarItem.endDate.isAfter(LocalDateTime.now())
}

// Set isFirstOnDay flags
if (calendarEvents.isNotEmpty()) {
calendarEvents[0].isFirstOnDay = true
Expand Down
4 changes: 3 additions & 1 deletion assets/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"openIn": "In {} öffnen",
"unknownDirection": "Unbekannte Richtung",
"showWeekends": "Wochenenden anzeigen",
"showHiddenCalendarEntries": "Versteckte Kalendareintrage anzeigen",
"color": "Farbe",
"resetLogin": "Zurücksetzen & Anmelden",
"resetPreferences": "Einstellungen zurücksetzen",
Expand Down Expand Up @@ -225,5 +226,6 @@
"campus": "Campus",
"studies": "Studium",
"suggested": "Interessante {}",
"all": "Alle"
"all": "Alle",
"visibility": "Sichtbarkeit"
}
4 changes: 3 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"openIn": "Open in {}",
"unknownDirection": "Unknown Direction",
"showWeekends": "Show Weekends",
"showHiddenCalendarEntries": "Show Hidden Calendar Entries",
"color": "Color",
"resetLogin": "Reset & Login",
"resetPreferences": "Reset Preferences",
Expand Down Expand Up @@ -225,5 +226,6 @@
"campus": "Campus",
"studies": "Studies",
"suggested": "Suggested {}",
"all": "All"
"all": "All",
"visibility": "Visibility"
}
1 change: 1 addition & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:
- drift: true
3 changes: 2 additions & 1 deletion ios/CalendarWidget/CalendarWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ struct Provider: TimelineProvider {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let entries = try decoder.decode([CalendarEntry].self, from: data)
var entries = try decoder.decode([CalendarEntry].self, from: data)
entries = entries.filter({ $0.endDate > Date() })
let entry = CalendarWidgetEntry(date: dateFormatter.date(from: calendarSaved ?? "") ?? Date(), entries: entries, size: context.family)
completion(entry)
} catch {
Expand Down
10 changes: 5 additions & 5 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ PODS:
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
- sqflite (0.0.3):
- sqflite_darwin (0.0.4):
- Flutter
- FlutterMacOS
- "sqlite3 (3.46.1+1)":
Expand Down Expand Up @@ -163,7 +163,7 @@ DEPENDENCIES:
- permission_handler_apple (from `.symlinks/plugins/permission_handler_apple/ios`)
- quick_actions_ios (from `.symlinks/plugins/quick_actions_ios/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sqflite (from `.symlinks/plugins/sqflite/darwin`)
- sqflite_darwin (from `.symlinks/plugins/sqflite_darwin/darwin`)
- sqlite3_flutter_libs (from `.symlinks/plugins/sqlite3_flutter_libs/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
- video_player_avfoundation (from `.symlinks/plugins/video_player_avfoundation/darwin`)
Expand Down Expand Up @@ -223,8 +223,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/quick_actions_ios/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
sqflite:
:path: ".symlinks/plugins/sqflite/darwin"
sqflite_darwin:
:path: ".symlinks/plugins/sqflite_darwin/darwin"
sqlite3_flutter_libs:
:path: ".symlinks/plugins/sqlite3_flutter_libs/ios"
url_launcher_ios:
Expand Down Expand Up @@ -267,7 +267,7 @@ SPEC CHECKSUMS:
PromisesSwift: 9d77319bbe72ebf6d872900551f7eeba9bce2851
quick_actions_ios: 56f3cbaa71e94f212838d1f9fe354bd0734779bf
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
sqflite: 673a0e54cc04b7d6dba8d24fb8095b31c3a99eec
sqflite_darwin: a553b1fd6fe66f53bbb0fe5b4f5bab93f08d7a13
sqlite3: 0bb0e6389d824e40296f531b858a2a0b71c0d2fb
sqlite3_flutter_libs: c00457ebd31e59fa6bb830380ddba24d44fbcd3b
url_launcher_ios: 5334b05cef931de560670eeae103fd3e431ac3fe
Expand Down
3 changes: 2 additions & 1 deletion lib/base/enums/user_preference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ enum UserPreference {
calendarColors(String),
browser(bool),
failedGrades(bool),
weekends(bool);
weekends(bool),
hiddenCalendarEntries(bool);

final Type type;

Expand Down
15 changes: 13 additions & 2 deletions lib/calendarComponent/model/calendar_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import 'package:campus_flutter/base/extensions/cast.dart';
import 'package:campus_flutter/calendarComponent/model/calendar_event.dart';
import 'package:campus_flutter/settingsComponent/views/settings_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';

class MeetingDataSource extends CalendarDataSource {
final BuildContext context;
final WidgetRef ref;

MeetingDataSource(List<CalendarEvent> source, this.context) {
appointments = source;
MeetingDataSource(List<CalendarEvent> source, this.context, this.ref) {
if (!ref.read(showHiddenCalendarEntries)) {
appointments = source
.where(
(element) => element.isVisible ?? true,
)
.toList();
} else {
appointments = source;
}
}

@override
Expand Down
1 change: 1 addition & 0 deletions lib/calendarComponent/model/calendar_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CalendarEvent extends Searchable {
final String? location;

int? color;
bool? isVisible;

Duration get duration {
return endDate.difference(startDate);
Expand Down
19 changes: 19 additions & 0 deletions lib/calendarComponent/model/calendar_preferences.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:json_annotation/json_annotation.dart';

part 'calendar_preferences.g.dart';

@JsonSerializable()
class CalendarPreferences {
final Map<String, int> colorPreferences;
final Map<String, bool> visibilityPreferences;

CalendarPreferences(
this.colorPreferences,
this.visibilityPreferences,
);

factory CalendarPreferences.fromJson(Map<String, dynamic> json) =>
_$CalendarPreferencesFromJson(json);

Map<String, dynamic> toJson() => _$CalendarPreferencesToJson(this);
}
20 changes: 20 additions & 0 deletions lib/calendarComponent/model/calendar_preferences.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions lib/calendarComponent/model/color_preferences.dart

This file was deleted.

17 changes: 0 additions & 17 deletions lib/calendarComponent/model/color_preferences.g.dart

This file was deleted.

50 changes: 0 additions & 50 deletions lib/calendarComponent/services/calendar_color_service.dart

This file was deleted.

82 changes: 82 additions & 0 deletions lib/calendarComponent/services/calendar_preference_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'dart:convert';

import 'package:campus_flutter/calendarComponent/model/calendar_preferences.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CalendarPreferenceService {
static const key = "calendar";

final SharedPreferences sharedPreferences;

Map<String, int> colorPreferences = {};
Map<String, bool> visibilityPreferences = {};

CalendarPreferenceService(this.sharedPreferences);

void saveColorPreference(String id, Color color) {
colorPreferences[id] = color.value;
try {
sharedPreferences.setString(
key,
jsonEncode(
CalendarPreferences(
colorPreferences,
visibilityPreferences,
).toJson(),
),
);
} catch (_) {}
}

Color? getColorPreference(String key) {
if (colorPreferences.isEmpty) {
loadPreferences();
}

final color = colorPreferences[key];
return color != null ? Color(color) : null;
}

void saveVisibilityPreference(String id, bool isVisible) {
visibilityPreferences[id] = isVisible;
try {
sharedPreferences.setString(
key,
jsonEncode(
CalendarPreferences(
colorPreferences,
visibilityPreferences,
).toJson(),
),
);
} catch (_) {}
}

bool? getVisibilityPreference(String key) {
if (visibilityPreferences.isEmpty) {
loadPreferences();
}

return visibilityPreferences[key];
}

void loadPreferences() {
try {
final data = sharedPreferences.getString(key);
if (data != null) {
final json = jsonDecode(data);
final calendarPreferences = CalendarPreferences.fromJson(
json as Map<String, dynamic>,
);
colorPreferences = calendarPreferences.colorPreferences;
visibilityPreferences = calendarPreferences.visibilityPreferences;
}
} catch (_) {}
}

void resetPreferences() {
sharedPreferences.remove(key);
}
}
Loading

0 comments on commit 024d834

Please sign in to comment.