Skip to content

Commit

Permalink
Fix sort option not getting saved
Browse files Browse the repository at this point in the history
  • Loading branch information
AhsanSarwar45 committed Apr 16, 2024
1 parent 205961b commit 62092ac
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
11 changes: 7 additions & 4 deletions lib/common/widgets/list/custom_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class CustomListView<Item extends ListItem> extends StatefulWidget {
this.listFilters = const [],
this.customActions = const [],
this.sortOptions = const [],
this.initialSortIndex = 0,
this.onChangeSortIndex,
});

final List<Item> items;
Expand All @@ -51,10 +53,12 @@ class CustomListView<Item extends ListItem> extends StatefulWidget {
final bool isReorderable;
final bool isDeleteEnabled;
final bool isDuplicateEnabled;
final int initialSortIndex;
final bool shouldInsertOnTop;
final List<ListFilterItem<Item>> listFilters;
final List<ListFilterCustomAction<Item>> customActions;
final List<ListSortOption<Item>> sortOptions;
final Function(int index)? onChangeSortIndex;

@override
State<CustomListView> createState() => _CustomListViewState<Item>();
Expand All @@ -66,10 +70,7 @@ class _CustomListViewState<Item extends ListItem>
double _itemCardHeight = 0;
final _scrollController = ScrollController();
final _controller = AnimatedListController();
int selectedSortIndex = 0;
// late ListFilter<Item> _selectedFilter = widget.listFilters.isEmpty
// ? ListFilter("Default", (item) => true)
// : widget.listFilters[0];
late int selectedSortIndex = widget.initialSortIndex;

@override
void initState() {
Expand All @@ -81,6 +82,7 @@ class _CustomListViewState<Item extends ListItem>
widget.listController.setDuplicateItem(_handleDuplicateItem);
widget.listController.setReloadItems(_handleReloadItems);
widget.listController.setClearItems(_handleClear);
updateCurrentList();
// widget.listController.setChangeItemWithId(_handleChangeItemWithId);
}

Expand Down Expand Up @@ -352,6 +354,7 @@ class _CustomListViewState<Item extends ListItem>
],
onChange: (index) => setState(() {
selectedSortIndex = index;
widget.onChangeSortIndex?.call(index);
updateCurrentList();
_notifyChangeList();
}),
Expand Down
11 changes: 4 additions & 7 deletions lib/common/widgets/list/list_filter_chip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class ListSortChip<Item extends ListItem> extends StatelessWidget {
}

return CardContainer(
color: isFirstSelected ? null : colorScheme.primary,
// color: isFirstSelected ? null : colorScheme.primary,
onTap: showSelect,
child: Row(
children: [
Expand All @@ -282,18 +282,15 @@ class ListSortChip<Item extends ListItem> extends StatelessWidget {
child: Text(
"Sort${isFirstSelected ? "" : ": ${sortOptions[selectedIndex].abbreviation}"}",
style: textTheme.headlineSmall?.copyWith(
color: isFirstSelected
? colorScheme.onSurface
: colorScheme.onPrimary),
color: colorScheme.onSurface
),
),
),
Padding(
padding: const EdgeInsets.only(left: 2.0, right: 8.0),
child: Icon(
Icons.keyboard_arrow_down_rounded,
color: isFirstSelected
? colorScheme.onSurface.withOpacity(0.6)
: colorScheme.onPrimary.withOpacity(0.6),
color:colorScheme.onSurface.withOpacity(0.6)
),
),
],
Expand Down
50 changes: 30 additions & 20 deletions lib/common/widgets/list/persistent_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ class PersistentListController<T> {
{bool callOnModifyList = true}) {
_listController.changeItems(callback, callOnModifyList: callOnModifyList);
}
//
// void changeItemWithId(int id, SingleItemChangerCallback<T> callback,
// {bool callOnModifyList = true}) {
// _listController.changeItemWithId(id, callback,
// callOnModifyList: callOnModifyList);
// }
//
// void changeItemAtIndex(int index, SingleItemChangerCallback<T> callback,
// {bool callOnModifyList = true}) {
// _listController.changeItemAtIndex(index, callback,
// callOnModifyList: callOnModifyList);
// }


void addItem(T item) {
_listController.addItem(item);
Expand Down Expand Up @@ -84,6 +71,7 @@ class PersistentListView<Item extends ListItem> extends StatefulWidget {
this.listFilters = const [],
this.customActions = const [],
this.sortOptions = const [],
// this.initialSortIndex = 0,
});

final Widget Function(Item item) itemBuilder;
Expand All @@ -99,6 +87,7 @@ class PersistentListView<Item extends ListItem> extends StatefulWidget {
final bool isDuplicateEnabled;
final bool reloadOnPop;
final bool shouldInsertOnTop;
// final int initialSortIndex;
final List<ListFilterItem<Item>> listFilters;
final List<ListFilterCustomAction<Item>> customActions;
final List<ListSortOption<Item>> sortOptions;
Expand All @@ -109,24 +98,39 @@ class PersistentListView<Item extends ListItem> extends StatefulWidget {

class _PersistentListViewState<Item extends ListItem>
extends State<PersistentListView<Item>> {
late int _initialSortIndex;

List<Item> _items = [];

@override
void initState() {
super.initState();
widget.listController.setOnReload(loadItems);
widget.listController.setOnReload(_loadItems);
if (widget.saveTag.isNotEmpty) {
_items = loadListSync<Item>(widget.saveTag);
}
// watchList(widget.saveTag, (event) => reloadItems());
ListenerManager.addOnChangeListener(widget.saveTag, loadItems);
ListenerManager.addOnChangeListener(widget.saveTag, _loadItems);

if (widget.sortOptions.isNotEmpty) {
if (!textFileExistsSync("${widget.saveTag}-sort-index")) {
saveTextFile("${widget.saveTag}-sort-index", "0");
_initialSortIndex = 0;
} else {
_initialSortIndex =
int.parse(loadTextFileSync("${widget.saveTag}-sort-index"));
}
}
else {
_initialSortIndex = 0;
}
// ListenerManager.addOnChangeListener(
// "${widget.saveTag}-reload", reloadItems);
}

@override
void dispose() {
ListenerManager.removeOnChangeListener(widget.saveTag, loadItems);
ListenerManager.removeOnChangeListener(widget.saveTag, _loadItems);
// ListenerManager.removeOnChangeListener(
// "${widget.saveTag}-reload", loadItems);
// unwatchList(widget.saveTag);
Expand All @@ -138,26 +142,30 @@ class _PersistentListViewState<Item extends ListItem>
// widget.listController.reloadItems(newList);
// }

void loadItems() {
void _loadItems() {
if (widget.saveTag.isNotEmpty) {
widget.listController.changeItems(
(List<Item> items) {
List<Item> newList = loadListSync<Item>(widget.saveTag);
items.clear();
items.addAll(newList);
/* print("--------------------------------------------- ${listToString(items)}"); */
},
callOnModifyList: false,
);
}
}

void saveItems() {
void _saveItems() {
if (widget.saveTag.isNotEmpty) {
saveList<Item>(widget.saveTag, _items);
}
}

void _handleChangeSort(int index) {
// _initialSortIndex = index;
saveTextFile("${widget.saveTag}-sort-index", index.toString());
}

@override
Widget build(BuildContext context) {
return CustomListView<Item>(
Expand All @@ -169,14 +177,16 @@ class _PersistentListViewState<Item extends ListItem>
onAddItem: widget.onAddItem,
listController: widget.listController.listController,
placeholderText: widget.placeholderText,
onModifyList: saveItems,
onModifyList: _saveItems,
isReorderable: widget.isReorderable,
isDeleteEnabled: widget.isDeleteEnabled,
isDuplicateEnabled: widget.isDuplicateEnabled,
shouldInsertOnTop: widget.shouldInsertOnTop,
listFilters: widget.listFilters,
customActions: widget.customActions,
sortOptions: widget.sortOptions,
initialSortIndex: _initialSortIndex,
onChangeSortIndex: _handleChangeSort,
);
}
}
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:core';
import 'dart:io';
import 'dart:isolate';
import 'dart:ui';

Expand Down
2 changes: 2 additions & 0 deletions lib/settings/logic/initialize_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ Future<void> initializeStorage([bool clearSettingsOnDebug = true]) async {
await initList<TimerPreset>("timer_presets", defaultTimerPresets);
await initList<FileItem>("ringtones", await getSystemRingtones());
await initTextFile("time_format_string", "h:mm a");
// await initTextFile("alarms-sort-index", "0");
// await initTextFile("timers-sort-index", "0");
}

Future<void> initializeSettings() async {
Expand Down

0 comments on commit 62092ac

Please sign in to comment.