Skip to content

Commit

Permalink
Merge pull request #1 from ksh-b/dev
Browse files Browse the repository at this point in the history
fixes subscription selection issues
  • Loading branch information
ksh-b authored Dec 26, 2023
2 parents 84bec46 + 9e372b7 commit c44cad5
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions lib/pages/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ class CategoryPopup extends StatefulWidget {
}

class _CategoryPopupState extends State<CategoryPopup> {
List selectedSources = []; // List<UserSubscription>
List customSources = []; // List<UserSubscription>
List selectedSubscriptions = []; // List<UserSubscription>
List customSubscriptions = []; // List<UserSubscription>
String customCategory = "";
TextEditingController customCategoryController = TextEditingController();

@override
void initState() {
setState(() {
selectedSources = Hive.box("subscriptions").get("selected") ??
selectedSubscriptions = Hive.box("subscriptions").get("selected") ??
List<UserSubscription>.empty(growable: true);
customSources = Hive.box("subscriptions").get("custom") ??
customSubscriptions = Hive.box("subscriptions").get("custom") ??
List<UserSubscription>.empty(growable: true);
});
super.initState();
Expand Down Expand Up @@ -169,8 +169,14 @@ class _CategoryPopupState extends State<CategoryPopup> {
// All checkbox
return CheckboxListTile(
title: Text(subCategoryKey),
value: selectedSources.contains(userSubscription),
value: selectedSubscriptions.contains(userSubscription),
onChanged: (value) {
if (value!) {
selectedSubscriptions.removeWhere((element) {
return element.publisher ==
userSubscription.publisher;
});
}
updateList(value, userSubscription);
},
);
Expand All @@ -184,9 +190,9 @@ class _CategoryPopupState extends State<CategoryPopup> {
// category checkbox
return CheckboxListTile(
title: Text(subCategoryKey),
value: selectedSources.contains(userSubscription),
onChanged: selectedSources
.where((element) => element.category == "/")
value: selectedSubscriptions.contains(userSubscription),
onChanged: selectedSubscriptions
.where((element) => element.publisher == userSubscription.publisher && element.category == "/")
.isNotEmpty
? null
: (value) {
Expand All @@ -197,23 +203,23 @@ class _CategoryPopupState extends State<CategoryPopup> {
),
ListView.builder(
shrinkWrap: true,
itemCount: customSources.length,
itemCount: customSubscriptions.length,
itemBuilder: (context, index) {
return CheckboxListTile(
secondary: IconButton(icon: const Icon(Icons.delete_forever), onPressed: () {
var subscription = customSources[index];
var subscription = customSubscriptions[index];
setState(() {
customSources.remove(subscription);
selectedSources.remove(subscription);
customSubscriptions.remove(subscription);
selectedSubscriptions.remove(subscription);
});

Hive.box("subscriptions").put("custom", customSources);
Hive.box("subscriptions").put("selected", selectedSources);
Hive.box("subscriptions").put("custom", customSubscriptions);
Hive.box("subscriptions").put("selected", selectedSubscriptions);
}),
title: Text(convertString((customSources[index] as UserSubscription).category)),
value: selectedSources.contains(customSources[index]),
title: Text(convertString((customSubscriptions[index] as UserSubscription).category)),
value: selectedSubscriptions.contains(customSubscriptions[index]),
onChanged: (value) {
updateList(value, customSources[index]);
updateList(value, customSubscriptions[index]);
},
);
},),
Expand Down Expand Up @@ -248,14 +254,14 @@ class _CategoryPopupState extends State<CategoryPopup> {
? IconButton(
onPressed: () {
setState(() {
customSources.add(UserSubscription(
customSubscriptions.add(UserSubscription(
widget.newsSource,
customCategory,
));
});
Hive.box("subscriptions").put(
"custom",
customSources,
customSubscriptions,
);
},
icon: const Icon(Icons.save_alt))
Expand All @@ -274,7 +280,7 @@ class _CategoryPopupState extends State<CategoryPopup> {
child: FilledButton(
onPressed: () {
Hive.box("subscriptions")
.put("selected", selectedSources);
.put("selected", selectedSubscriptions);
Navigator.of(context).pop();
widget.callback();
},
Expand Down Expand Up @@ -302,9 +308,9 @@ class _CategoryPopupState extends State<CategoryPopup> {
void updateList(bool? value, UserSubscription userSubscription) {
setState(() {
if (value!) {
selectedSources.add(userSubscription);
selectedSubscriptions.add(userSubscription);
} else {
selectedSources.remove(userSubscription);
selectedSubscriptions.remove(userSubscription);
}
});
}
Expand Down

0 comments on commit c44cad5

Please sign in to comment.