Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ksh-b committed Jun 21, 2024
1 parent 3ec6fc5 commit d5e7df8
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 142 deletions.
188 changes: 46 additions & 142 deletions lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:package_info_plus/package_info_plus.dart';
import 'package:raven/extractor/trend/google.dart';
import 'package:raven/extractor/trend/yahoo.dart';
import 'package:raven/model/trends.dart';
import 'package:raven/pages/widget/options_popup.dart';
import 'package:raven/service/simplytranslate.dart';
import 'package:raven/utils/store.dart';
import 'package:raven/utils/theme_provider.dart';
Expand Down Expand Up @@ -41,7 +42,7 @@ class _SettingsPageState extends State<SettingsPage> {
title: const Text('Color'),
subtitle: Text(Store.themeColorSetting),
onTap: () {
_showPopup(
showPopup(
context,
"Color",
(String option) {
Expand All @@ -54,17 +55,22 @@ class _SettingsPageState extends State<SettingsPage> {
ListTile(
leading: const Icon(Icons.format_size_rounded),
title: const Text('Font size'),
subtitle: Slider(
value: Store.fontScale,
min: 0.8,
max: 1.5,
divisions: 7,
label: (Store.fontScale * 100).round().toString(),
onChanged: (double value) {
setState(() {
Store.fontScale = value;
});
},
subtitle: SliderTheme(
data: SliderThemeData(
showValueIndicator: ShowValueIndicator.onlyForContinuous,),
child: Slider(
value: Store.fontScale,
min: 0.8,
max: 1.5,
// divisions: 7,
label: (Store.fontScale * 100).round().toString(),
onChanged: (double value) {
setState(() {
Store.fontScale = value;
});
},

),
),
),
],
Expand Down Expand Up @@ -95,33 +101,21 @@ class _SettingsPageState extends State<SettingsPage> {
ListTile(
leading: const Icon(Icons.linear_scale),
title: const Text('Max articles per subscription'),
subtitle: Slider(
value: Store.articlesPerSub * 1.0,
min: 5,
max: 20,
divisions: 3,
label: Store.articlesPerSub.toString(),
onChanged: (double value) {
setState(() {
Store.articlesPerSub = value.toInt();
});
},
),
),
ListTile(
leading: Icon(Icons.alt_route_rounded),
title: const Text('Alternate URL (Long tap)'),
subtitle: Text(Store.ladderSetting),
onTap: () {
_showPopup(
context,
"Ladder",
(String option) {
Store.ladderSetting = option;
subtitle: SliderTheme(
data: SliderThemeData(
showValueIndicator: ShowValueIndicator.onlyForContinuous,),
child: Slider(
value: Store.articlesPerSub * 1.0,
min: 5,
max: 30,
label: Store.articlesPerSub.toString(),
onChanged: (double value) {
setState(() {
Store.articlesPerSub = value.toInt();
});
},
Store.ladders.keys.toList(),
);
},
),
),
),
SwitchListTile(
secondary: Icon(Icons.translate_rounded),
Expand All @@ -145,7 +139,7 @@ class _SettingsPageState extends State<SettingsPage> {
title: const Text('Translate language'),
subtitle: Text(Store.languageSetting),
onTap: () {
_showPopup(
showPopup(
context,
"Translate language",
(String option) {
Expand All @@ -161,7 +155,7 @@ class _SettingsPageState extends State<SettingsPage> {
title: const Text('Translator instance'),
subtitle: Text(Store.translatorInstanceSetting),
onTap: () {
_showPopup(
showPopup(
context,
"Translator instance",
(String option) {
Expand All @@ -180,7 +174,7 @@ class _SettingsPageState extends State<SettingsPage> {
title: const Text('Translator engine'),
subtitle: Text(Store.translatorEngineSetting),
onTap: () {
_showPopup(
showPopup(
context,
"Translate engine",
(String option) {
Expand Down Expand Up @@ -216,7 +210,7 @@ class _SettingsPageState extends State<SettingsPage> {
title: const Text('Suggestions provider'),
subtitle: Text(Store.trendsProviderSetting),
onTap: () {
_showPopup(
showPopup(
context,
"Suggestions provider",
(String option) {
Expand All @@ -230,7 +224,7 @@ class _SettingsPageState extends State<SettingsPage> {
? ListTile(
leading: const Icon(Icons.location_on_rounded),
onTap: () {
_showPopup(context, "Trends Provider", (String option) {
showPopup(context, "Trends Provider", (String option) {
Store.countrySetting = option;
}, GoogleTrend.locations);
},
Expand All @@ -240,15 +234,15 @@ class _SettingsPageState extends State<SettingsPage> {
: const SizedBox.shrink(),
Store.trendsProviderSetting == "Yahoo"
? ListTile(
leading: const Icon(Icons.location_on_rounded),
onTap: () {
_showPopup(context, "Trends Provider", (String option) {
Store.countrySetting = option;
}, YahooTrend.locations);
},
title: Text("Yahoo Trends location"),
subtitle: Text(Store.countrySetting),
)
leading: const Icon(Icons.location_on_rounded),
onTap: () {
showPopup(context, "Trends Provider", (String option) {
Store.countrySetting = option;
}, YahooTrend.locations);
},
title: Text("Yahoo Trends location"),
subtitle: Text(Store.countrySetting),
)
: const SizedBox.shrink(),
const SizedBox(height: 20),
],
Expand Down Expand Up @@ -293,94 +287,4 @@ class _SettingsPageState extends State<SettingsPage> {
);
}

void _showPopup(BuildContext context, String title, Function callback,
List<String> options) {
showDialog(
context: context,
builder: (BuildContext context) {
return OptionsPopup(
title: title,
callback: callback,
options: options,
);
},
);
}
}

class OptionsPopup extends StatefulWidget {
final String title;
final Function callback;
final List<String> options;

const OptionsPopup({
super.key,
required this.title,
required this.callback,
required this.options,
});

@override
State<OptionsPopup> createState() => _OptionsPopupState();
}

class _OptionsPopupState extends State<OptionsPopup> {
late List<String> filteredOptions;
late TextEditingController searchController;

@override
void initState() {
super.initState();
filteredOptions = widget.options;
searchController = TextEditingController();
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(widget.title),
content: Container(
width: double.maxFinite,
child: ListView.builder(
shrinkWrap: true,
itemCount: filteredOptions.length + 1,
itemBuilder: (BuildContext context, int index) {
if (index == 0) {
return widget.options.length > 10
? TextField(
onChanged: _filterOptions,
decoration: InputDecoration(
icon: Icon(Icons.search_rounded),
hintText: "Search...",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
)
: const SizedBox.shrink();
}
return ListTile(
title: Text(filteredOptions[index - 1]),
onTap: () {
widget.callback(filteredOptions[index - 1]);
Navigator.of(context).pop();
},
);
},
),
),
);
}

void _filterOptions(String query) {
setState(() {
if (query.isEmpty) {
filteredOptions = widget.options;
} else {
filteredOptions = widget.options
.where((entry) => entry.toLowerCase().contains(query.toLowerCase()))
.toList();
}
});
}
}
97 changes: 97 additions & 0 deletions lib/pages/widget/options_popup.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:flutter/material.dart';

class OptionsPopup extends StatefulWidget {
final String title;
final Function callback;
final List<String> options;

const OptionsPopup({
super.key,
required this.title,
required this.callback,
required this.options,
});

@override
State<OptionsPopup> createState() => _OptionsPopupState();
}

class _OptionsPopupState extends State<OptionsPopup> {
late List<String> filteredOptions;
late TextEditingController searchController;

@override
void initState() {
super.initState();
filteredOptions = widget.options;
searchController = TextEditingController();
}

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(widget.title),
content: Container(
width: double.maxFinite,
child: ListView.builder(
shrinkWrap: true,
itemCount: filteredOptions.length + 1,
itemBuilder: (BuildContext context, int index) {
if (index == 0) {
return widget.options.length > 10
? TextField(
onChanged: _filterOptions,
decoration: InputDecoration(
icon: Icon(Icons.search_rounded),
hintText: "Search...",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
)
: const SizedBox.shrink();
}
return ListTile(
title: Text(filteredOptions[index - 1]),
onTap: () {
widget.callback(filteredOptions[index - 1]);
Navigator.of(context).pop();
},
);
},
),
),
);
}

void _filterOptions(String query) {
setState(() {
if (query.isEmpty) {
filteredOptions = widget.options;
} else {
filteredOptions = widget.options
.where((entry) => entry.toLowerCase().contains(query.toLowerCase()))
.toList();
}
});
}
}


void showPopup(
BuildContext context,
String title,
Function callback,
List<String> options,
) {
showDialog(
context: context,
builder: (BuildContext context) {
return OptionsPopup(
title: title,
callback: callback,
options: options,
);
},
);
}

0 comments on commit d5e7df8

Please sign in to comment.