Done w/ filter and multi select stuff

This commit is contained in:
Imran Remtulla
2022-09-25 00:12:02 -04:00
parent 45e5544c5b
commit f58d26524c
4 changed files with 224 additions and 76 deletions

View File

@@ -7,11 +7,15 @@ class GeneratedFormModal extends StatefulWidget {
{super.key,
required this.title,
required this.items,
required this.defaultValues});
required this.defaultValues,
this.initValid = false,
this.message = ""});
final String title;
final String message;
final List<List<GeneratedFormItem>> items;
final List<String> defaultValues;
final bool initValid;
@override
State<GeneratedFormModal> createState() => _GeneratedFormModalState();
@@ -21,12 +25,25 @@ class _GeneratedFormModalState extends State<GeneratedFormModal> {
List<String> values = [];
bool valid = false;
@override
void initState() {
super.initState();
valid = widget.initValid;
}
@override
Widget build(BuildContext context) {
return AlertDialog(
scrollable: true,
title: Text(widget.title),
content: GeneratedForm(
content:
Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
if (widget.message.isNotEmpty) Text(widget.message),
if (widget.message.isNotEmpty)
SizedBox(
height: 16,
),
GeneratedForm(
items: widget.items,
onValueChanges: (values, valid) {
setState(() {
@@ -34,7 +51,8 @@ class _GeneratedFormModalState extends State<GeneratedFormModal> {
this.valid = valid;
});
},
defaultValues: widget.defaultValues),
defaultValues: widget.defaultValues)
]),
actions: [
TextButton(
onPressed: () {

View File

@@ -247,9 +247,8 @@ class _AppPageState extends State<AppPage> {
onPressed: () {
HapticFeedback
.selectionClick();
appsProvider
.removeApp(app!.app.id)
.then((_) {
appsProvider.removeApps(
[app!.app.id]).then((_) {
int count = 0;
Navigator.of(context)
.popUntil((_) =>

View File

@@ -43,7 +43,6 @@ class AppsPageState extends State<AppsPage> {
Widget build(BuildContext context) {
var appsProvider = context.watch<AppsProvider>();
var settingsProvider = context.watch<SettingsProvider>();
var existingUpdateAppIds = appsProvider.getExistingUpdates();
var sortedApps = appsProvider.apps.values.toList();
selectedIds = selectedIds
@@ -63,7 +62,11 @@ class AppsPageState extends State<AppsPage> {
if (filter != null) {
sortedApps = sortedApps.where((app) {
if (app.app.installedVersion == app.app.latestVersion &&
filter!.onlyNonLatest) {
!(filter!.includeUptodate)) {
return false;
}
if (app.app.installedVersion == null &&
!(filter!.includeNonInstalled)) {
return false;
}
if (filter!.nameFilter.isEmpty && filter!.authorFilter.isEmpty) {
@@ -184,38 +187,133 @@ class AppsPageState extends State<AppsPage> {
? 'Select All'
: 'Deselect ${selectedIds.length.toString()}')),
const VerticalDivider(),
const Spacer(),
selectedIds.isEmpty
? const SizedBox()
: IconButton(
Expanded(
child: selectedIds.isEmpty
? Container()
: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
onPressed: () {
// TODO: Delete selected Apps after confirming
showDialog<List<String>?>(
context: context,
builder: (BuildContext ctx) {
return GeneratedFormModal(
title: 'Remove Selected Apps?',
items: const [],
defaultValues: const [],
initValid: true,
message:
'${selectedIds.length} App${selectedIds.length == 1 ? '' : 's'} will be removed from Obtainium but remain installed. You still need to uninstall ${selectedIds.length == 1 ? 'it' : 'them'} manually.',
);
}).then((values) {
if (values != null) {
appsProvider.removeApps(selectedIds.toList());
}
});
},
icon: const Icon(Icons.install_mobile_outlined)),
selectedIds.isEmpty
? const SizedBox()
: IconButton(
onPressed: () {
// TODO: Install selected Apps if they are not up to date after confirming (replace existing button)
},
icon: const Icon(Icons.delete_outline_rounded)),
existingUpdateAppIds.isEmpty || filter != null
? const SizedBox()
: IconButton(
onPressed: appsProvider.areDownloadsRunning()
icon: const Icon(Icons.delete_outline_outlined),
),
IconButton(
onPressed: appsProvider.areDownloadsRunning() ||
selectedIds
.where((id) =>
appsProvider.apps[id]!.app
.installedVersion !=
appsProvider
.apps[id]!.app.latestVersion)
.isEmpty
? null
: () {
HapticFeedback.heavyImpact();
settingsProvider.getInstallPermission().then((_) {
appsProvider.downloadAndInstallLatestApp(
existingUpdateAppIds, context);
var existingUpdateIdsSelected =
appsProvider
.getExistingUpdates(
installedOnly: true)
.where((element) =>
selectedIds.contains(element))
.toList();
var newInstallIdsSelected = appsProvider
.getExistingUpdates(
nonInstalledOnly: true)
.where((element) =>
selectedIds.contains(element))
.toList();
List<List<GeneratedFormItem>> formInputs =
[];
if (existingUpdateIdsSelected
.isNotEmpty &&
newInstallIdsSelected.isNotEmpty) {
formInputs.add([
GeneratedFormItem(
label:
"Update ${existingUpdateIdsSelected.length} Apps?",
type: FormItemType.bool)
]);
formInputs.add([
GeneratedFormItem(
label:
"Install ${newInstallIdsSelected.length} new Apps?",
type: FormItemType.bool)
]);
}
showDialog<List<String>?>(
context: context,
builder: (BuildContext ctx) {
return GeneratedFormModal(
title: "Install Selected Apps?",
message:
"${existingUpdateIdsSelected.length} update${existingUpdateIdsSelected.length == 1 ? '' : 's'} and ${newInstallIdsSelected.length} new install${newInstallIdsSelected.length == 1 ? '' : 's'}.",
items: formInputs,
defaultValues: const [
"true",
"true"
],
initValid: true,
);
}).then((values) {
if (values != null) {
bool shouldInstallUpdates =
values.length < 2 ||
values[0] == "true";
bool shouldInstallNew =
values.length < 2 ||
values[1] == "true";
settingsProvider
.getInstallPermission()
.then((_) {
List<String> toInstall = [];
if (shouldInstallUpdates) {
toInstall.addAll(
existingUpdateIdsSelected);
}
if (shouldInstallNew) {
toInstall.addAll(
newInstallIdsSelected);
}
appsProvider
.downloadAndInstallLatestApp(
toInstall, context);
});
}
});
},
icon: const Icon(Icons.install_mobile_outlined),
),
icon: const Icon(
Icons.file_download_outlined,
)),
],
)),
const VerticalDivider(),
appsProvider.apps.isEmpty
? const SizedBox()
: IconButton(
: TextButton.icon(
label: Text(
filter == null ? 'Filter' : 'Filter *',
style: TextStyle(
fontWeight: filter == null
? FontWeight.normal
: FontWeight.bold),
),
onPressed: () {
showDialog<List<String>?>(
context: context,
@@ -231,27 +329,25 @@ class AppsPageState extends State<AppsPage> {
],
[
GeneratedFormItem(
label: "Ignore Up-to-Date Apps",
label: "Up to Date Apps",
type: FormItemType.bool)
],
[
GeneratedFormItem(
label: "Non-Installed Apps",
type: FormItemType.bool)
]
],
defaultValues: filter == null
? []
: [
filter!.nameFilter,
filter!.authorFilter,
filter!.onlyNonLatest ? 'true' : ''
]);
? AppsFilter().toValuesArray()
: filter!.toValuesArray());
}).then((values) {
if (values != null &&
values
.where((element) => element.isNotEmpty)
.isNotEmpty) {
if (values != null) {
setState(() {
filter = AppsFilter(
nameFilter: values[0],
authorFilter: values[1],
onlyNonLatest: values[2] == "true");
filter = AppsFilter.fromValuesArray(values);
if (AppsFilter().isIdenticalTo(filter!)) {
filter = null;
}
});
} else {
setState(() {
@@ -260,8 +356,7 @@ class AppsPageState extends State<AppsPage> {
}
});
},
icon: Icon(
filter == null ? Icons.search : Icons.manage_search))
icon: const Icon(Icons.filter_list_rounded))
],
),
],
@@ -272,10 +367,34 @@ class AppsPageState extends State<AppsPage> {
class AppsFilter {
late String nameFilter;
late String authorFilter;
late bool onlyNonLatest;
late bool includeUptodate;
late bool includeNonInstalled;
AppsFilter(
{this.nameFilter = "",
this.authorFilter = "",
this.onlyNonLatest = false});
this.includeUptodate = true,
this.includeNonInstalled = true});
List<String> toValuesArray() {
return [
nameFilter,
authorFilter,
includeUptodate ? "true" : "",
includeNonInstalled ? "true" : ""
];
}
AppsFilter.fromValuesArray(List<String> values) {
nameFilter = values[0];
authorFilter = values[1];
includeUptodate = values[2] == "true";
includeNonInstalled = values[3] == "true";
}
bool isIdenticalTo(AppsFilter other) =>
authorFilter.trim() == other.authorFilter.trim() &&
nameFilter.trim() == other.nameFilter.trim() &&
includeUptodate == other.includeUptodate &&
includeNonInstalled == other.includeNonInstalled;
}

View File

@@ -98,7 +98,7 @@ class AppsProvider with ChangeNotifier {
.isNotEmpty;
Future<bool> canInstallSilently(App app) async {
// TODO: This is unreliable - try to get from OS
// TODO: This is unreliable - try to get from OS in the future
var osInfo = await DeviceInfoPlugin().androidInfo;
return app.installedVersion != null &&
osInfo.version.sdkInt! >= 30 &&
@@ -203,9 +203,11 @@ class AppsProvider with ChangeNotifier {
}
if (context != null) {
for (var i in regularInstalls) {
if (regularInstalls.isNotEmpty) {
// ignore: use_build_context_synchronously
await askUserToReturnToForeground(context);
}
for (var i in regularInstalls) {
await installApk(i);
}
}
@@ -256,7 +258,8 @@ class AppsProvider with ChangeNotifier {
notifyListeners();
}
Future<void> removeApp(String appId) async {
Future<void> removeApps(List<String> appIds) async {
for (var appId in appIds) {
File file = File('${(await getAppsDir()).path}/$appId.json');
if (file.existsSync()) {
file.deleteSync();
@@ -264,8 +267,11 @@ class AppsProvider with ChangeNotifier {
if (apps.containsKey(appId)) {
apps.remove(appId);
}
}
if (appIds.isNotEmpty) {
notifyListeners();
}
}
bool checkAppObjectForUpdate(App app) {
if (!apps.containsKey(app.id)) {
@@ -309,16 +315,22 @@ class AppsProvider with ChangeNotifier {
return updates;
}
List<String> getExistingUpdates({bool installedOnly = false}) {
List<String> getExistingUpdates(
{bool installedOnly = false, bool nonInstalledOnly = false}) {
List<String> updateAppIds = [];
List<String> appIds = apps.keys.toList();
for (int i = 0; i < appIds.length; i++) {
App? app = apps[appIds[i]]!.app;
if (app.installedVersion != app.latestVersion &&
(app.installedVersion != null || !installedOnly)) {
(!installedOnly || !nonInstalledOnly)) {
if ((app.installedVersion == null &&
(nonInstalledOnly || !installedOnly) ||
(app.installedVersion != null &&
(installedOnly || !nonInstalledOnly)))) {
updateAppIds.add(app.id);
}
}
}
return updateAppIds;
}