More progress on UI - basics almost done

This commit is contained in:
Imran Remtulla
2022-08-18 15:25:30 -04:00
parent 605877f6bf
commit 0e36e53b46
5 changed files with 176 additions and 126 deletions

View File

@@ -22,67 +22,6 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
// home: const MyHomePage(title: 'Obtainium'),
home: const AppsPage());
}
}
// class MyHomePage extends StatefulWidget {
// const MyHomePage({super.key, required this.title});
// final String title;
// @override
// State<MyHomePage> createState() => _MyHomePageState();
// }
// class _MyHomePageState extends State<MyHomePage> {
// int ind = 0;
// List<String> urls = [
// 'https://github.com/Ashinch/ReadYou/releases/download', // Should work
// 'http://github.com/syncthing/syncthing-android/releases/tag/1.20.4', // Should work
// 'https://github.com/videolan/vlc' // Should not
// ];
// @override
// Widget build(BuildContext context) {
// ToastContext().init(context);
// return Scaffold(
// appBar: AppBar(
// title: Text(widget.title),
// ),
// body: Center(
// child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: <Widget>[
// Text(
// urls[ind],
// style: Theme.of(context).textTheme.headline4,
// ),
// ],
// ),
// ),
// floatingActionButton: FloatingActionButton(
// onPressed: () {
// context.read<AppsProvider>().installApp(urls[ind]).then((_) {
// setState(() {
// ind = ind == (urls.length - 1) ? 0 : ind + 1;
// });
// }).catchError((err) {
// if (err is! String) {
// err = "Unknown Error";
// }
// Toast.show(err);
// });
// },
// tooltip: 'Increment',
// child: const Icon(Icons.add),
// ),
// );
// }
// @override
// void dispose() {
// super.dispose();
// }
// }

View File

@@ -14,6 +14,7 @@ class AddAppPage extends StatefulWidget {
class _AddAppPageState extends State<AddAppPage> {
final _formKey = GlobalKey<FormState>();
final urlInputController = TextEditingController();
bool gettingAppInfo = false;
@override
Widget build(BuildContext context) {
@@ -25,10 +26,16 @@ class _AddAppPageState extends State<AddAppPage> {
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
const Spacer(),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'https://github.com/Author/Project',
helperText: 'Enter the App source URL'),
controller: urlInputController,
validator: (value) {
if (value == null ||
@@ -38,32 +45,48 @@ class _AddAppPageState extends State<AddAppPage> {
}
return null;
},
),
)),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
padding:
const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: ElevatedButton(
onPressed: () {
onPressed: gettingAppInfo
? null
: () {
if (_formKey.currentState!.validate()) {
setState(() {
gettingAppInfo = true;
});
SourceService()
.getApp(urlInputController.value.text)
.then((app) {
var appsProvider = context.read<AppsProvider>();
if (appsProvider.apps.containsKey(app.id)) {
throw 'App already added';
}
appsProvider.saveApp(app).then((_) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AppPage(appId: app.id)));
builder: (context) =>
AppPage(appId: app.id)));
});
}).catchError((e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.toString())),
);
}).whenComplete(() {
setState(() {
gettingAppInfo = false;
});
});
}
},
child: const Text('Add'),
),
),
const Spacer(),
if (gettingAppInfo) const LinearProgressIndicator(),
],
),
)),

View File

@@ -18,15 +18,68 @@ class _AppPageState extends State<AppPage> {
Widget build(BuildContext context) {
var appsProvider = context.watch<AppsProvider>();
App? app = appsProvider.apps[widget.appId];
if (app == null) {
Navigator.pop(context);
if (app?.installedVersion != null) {
appsProvider.getUpdate(app!.id);
}
return Scaffold(
appBar: AppBar(
title: Text('App - ${app?.name} - ${app?.author}'),
title: Text('${app?.author}/${app?.name}'),
),
body: WebView(
initialUrl: app?.url,
));
),
bottomSheet: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child:
Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
Expanded(
child: OutlinedButton(
onPressed: (app?.installedVersion == null ||
appsProvider.checkAppObjectForUpdate(app!)) &&
app?.currentDownloadId == null
? () {
appsProvider.backgroundDownloadAndInstallApp(app!);
}
: null,
child: Text(
app?.installedVersion == null ? 'Install' : 'Update'))),
const SizedBox(width: 16.0),
OutlinedButton(
onPressed: app?.currentDownloadId != null
? null
: () {
showDialog(
context: context,
builder: (BuildContext ctx) {
return AlertDialog(
title: const Text('Remove App?'),
content: Text(
'This will remove \'${app?.name}\' from Obtainium.${app?.installedVersion != null ? '\n\nNote that while Obtainium will no longer track its updates, the App will remain installed.' : ''}'),
actions: [
TextButton(
onPressed: () {
appsProvider.removeApp(app!.id).then((_) {
int count = 0;
Navigator.of(context)
.popUntil((_) => count++ >= 2);
});
},
child: const Text('Remove')),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Cancel'))
],
);
});
},
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).errorColor),
child: const Text('Remove'),
)
// TODO: Add progress bar when app?.currentDownloadId != null
])),
);
}
}

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:obtainium/pages/add_app.dart';
import 'package:obtainium/pages/app.dart';
import 'package:obtainium/services/apps_provider.dart';
import 'package:provider/provider.dart';
@@ -13,23 +14,38 @@ class AppsPage extends StatefulWidget {
class _AppsPageState extends State<AppsPage> {
@override
Widget build(BuildContext context) {
var appsProvider = context.watch<AppsProvider>();
return Scaffold(
appBar: AppBar(
title: const Text('Obtainium - Apps'),
title: const Text('Obtainium'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: () {
var appsProvider = context.watch<AppsProvider>();
if (appsProvider.loadingApps) {
return [const Text('Loading Apps...')];
} else if (appsProvider.apps.isEmpty) {
return [const Text('No Apps Yet.')];
} else {
return appsProvider.apps.values.map((e) => Text(e.id)).toList();
}
}(),
child: appsProvider.loadingApps
? const CircularProgressIndicator()
: appsProvider.apps.isEmpty
? Text(
'No Apps',
style: Theme.of(context).textTheme.headline4,
)
: ListView(
children: appsProvider.apps.values
.map(
(e) => ListTile(
title: Text(e.name),
subtitle: Text(e.author),
trailing:
Text(e.installedVersion ?? 'Not Installed'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AppPage(appId: e.id)),
);
},
),
)
.toList(),
),
),
floatingActionButton: FloatingActionButton(

View File

@@ -92,9 +92,9 @@ class AppsProvider with ChangeNotifier {
}
// Change App status to no longer downloading
App? foundApp;
apps.forEach((id, app) {
apps.forEach((appId, app) {
if (app.currentDownloadId == id) {
foundApp = apps[app.id];
foundApp = apps[appId];
}
});
foundApp!.currentDownloadId = null;
@@ -112,7 +112,7 @@ class AppsProvider with ChangeNotifier {
if (apkDir.existsSync()) apkDir.deleteSync(recursive: true);
apkDir.createSync(recursive: true);
String? downloadId = await FlutterDownloader.enqueue(
url: app.url,
url: app.apkUrl,
savedDir: apkDir.path,
showNotification: true,
openFileFromNotification: false,
@@ -158,23 +158,42 @@ class AppsProvider with ChangeNotifier {
notifyListeners();
}
bool checkUpdate(App app) {
Future<void> removeApp(String appId) async {
File file = File('${(await getAppsDir()).path}/$appId.json');
if (file.existsSync()) {
file.deleteSync();
}
if (apps.containsKey(appId)) {
apps.remove(appId);
}
notifyListeners();
}
bool checkAppObjectForUpdate(App app) {
if (!apps.containsKey(app.id)) {
throw 'App not found';
}
return app.latestVersion != apps[app.id]?.installedVersion;
}
Future<List<App>> checkUpdates() async {
List<App> updates = [];
List<String> appIds = apps.keys.toList();
for (int i = 0; i < appIds.length; i++) {
App? currentApp = apps[appIds[i]];
Future<App?> getUpdate(String appId) async {
App? currentApp = apps[appId];
App newApp = await SourceService().getApp(currentApp!.url);
if (newApp.latestVersion != currentApp.latestVersion) {
newApp.installedVersion = currentApp.installedVersion;
updates.add(newApp);
await saveApp(newApp);
return newApp;
}
return null;
}
Future<List<App>> getUpdates() async {
List<App> updates = [];
List<String> appIds = apps.keys.toList();
for (int i = 0; i < appIds.length; i++) {
App? newApp = await getUpdate(appIds[i]);
if (newApp != null) {
updates.add(newApp);
}
}
return updates;