Skip to content

Commit

Permalink
Make the filter on the eventsList work even when switching views.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjtappe committed Dec 3, 2024
1 parent 55dccad commit 0436f1f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 71 deletions.
7 changes: 5 additions & 2 deletions lib/data/appProviders/preferences_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ class PreferencesProvider {

// ---------------------------------------------------------
static const String _futureEventsKey = 'futureEvents';
static final ValueNotifier<bool> futureEventsNotifier = ValueNotifier(false);

static Future<bool> get futureEvents async {
static Future<void> loadFutureEvents() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(_futureEventsKey) ?? false; // Default
bool value = prefs.getBool(_futureEventsKey) ?? false; // Default
futureEventsNotifier.value = value;
}

static Future<void> setFutureEvents(bool value) async {
futureEventsNotifier.value = value;
final prefs = await SharedPreferences.getInstance();
await prefs.setBool(_futureEventsKey, value);
}
Expand Down
144 changes: 75 additions & 69 deletions lib/pages/events_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class EventsPage extends StatefulWidget {

class _EventsPageState extends State<EventsPage> {
late bool _isDayView = true;
late bool _futureEvents = true;

@override
void initState() {
Expand All @@ -33,63 +32,64 @@ class _EventsPageState extends State<EventsPage> {
bool value = await PreferencesProvider.isDayView;
setState(() {
_isDayView = value;
}); // Trigger a rebuild after updating _isDayView
});
}

Future<void> _loadEventListFilter() async {
bool value = await PreferencesProvider.futureEvents;
setState(() {
_futureEvents = value;
}); // Trigger a rebuild after updating _isDayView
await PreferencesProvider.loadFutureEvents();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Events'),
automaticallyImplyLeading: false,
backgroundColor: Theme
.of(context)
.appBarTheme
.backgroundColor,
foregroundColor: Theme
.of(context)
.appBarTheme
.foregroundColor,
actions: [
_isDayView ? const SizedBox.shrink() :
IconButton(
icon: Icon(_futureEvents ? Icons.filter_alt : Icons.filter_alt_off),
color: Theme
return ValueListenableBuilder<bool>(
valueListenable: PreferencesProvider.futureEventsNotifier,
builder: (context, futureEvents, child) {
return Scaffold(
appBar: AppBar(
title: const Text('Events'),
automaticallyImplyLeading: false,
backgroundColor: Theme
.of(context)
.colorScheme
.onSurface,
onPressed: () {
setState(() {
_futureEvents = !_futureEvents;
PreferencesProvider.setFutureEvents(_futureEvents);
});
},
tooltip: 'Filter only future events',
),
IconButton(
icon: Icon(_isDayView ? Icons.list : Icons.calendar_today),
color: Theme
.appBarTheme
.backgroundColor,
foregroundColor: Theme
.of(context)
.colorScheme
.onSurface,
onPressed: () {
setState(() {
_isDayView = !_isDayView;
PreferencesProvider.setIsDayView(_isDayView);
});
},
.appBarTheme
.foregroundColor,
actions: [
_isDayView ? const SizedBox.shrink() :
IconButton(
icon: Icon(
futureEvents ? Icons.filter_alt : Icons.filter_alt_off),
color: Theme
.of(context)
.colorScheme
.onSurface,
onPressed: () {
futureEvents = !futureEvents;
PreferencesProvider.setFutureEvents(futureEvents);
},
tooltip: 'Filter only future events',
),
IconButton(
icon: Icon(_isDayView ? Icons.list : Icons.calendar_today),
color: Theme
.of(context)
.colorScheme
.onSurface,
onPressed: () {
setState(() {
_isDayView = !_isDayView;
PreferencesProvider.setIsDayView(_isDayView);
});
},
),
],
),
],
),
body: _isDayView ? DayViewCalendar() : EventList(
futureEvents: _futureEvents),
body: _isDayView ? DayViewCalendar() : EventList(
futureEvents: futureEvents),
);
},
);
}
}
Expand Down Expand Up @@ -227,14 +227,14 @@ class EventList extends StatefulWidget {
}

class EventListState extends State<EventList> {
late bool _futureEvents = false;

@override
void didUpdateWidget(EventList oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.futureEvents != widget.futureEvents) {
_futureEvents = widget.futureEvents; // Update local variable if futureEvents changed
}
void initState() {
super.initState();
_loadPreferences();
}

Future<void> _loadPreferences() async {
PreferencesProvider.loadFutureEvents();
}

@override
Expand All @@ -250,21 +250,27 @@ class EventListState extends State<EventList> {
// ]
// ),
Expanded( // Use Expanded to allow ListView.builder to take available space
child: Consumer<EventsProvider>( // Wrap ListView.builder with Consumer
builder: (context, itemList, child) {
List<EventData> items;
if (_futureEvents) {
items = itemList.filterPastEvents();
} else {
items = itemList.items();
child: ValueListenableBuilder<bool>(
valueListenable: PreferencesProvider.futureEventsNotifier,
builder: (context, builderValue, child) {
return Consumer<
EventsProvider>( // Wrap ListView.builder with Consumer
builder: (context, itemList, child) {
List<EventData> items;
if (builderValue) {
items = itemList.filterPastEvents();
} else {
items = itemList.items();
}
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return EventListTile(item: items[index]);
},
);
},
);
}
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return EventListTile(item: items[index]);
},
);
},
),
),
],
Expand Down

0 comments on commit 0436f1f

Please sign in to comment.