mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-07-16 06:36:44 +02:00
91 lines
3.2 KiB
Dart
91 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:obtainium/pages/app.dart';
|
|
import 'package:obtainium/services/apps_provider.dart';
|
|
import 'package:obtainium/services/source_service.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class AddAppPage extends StatefulWidget {
|
|
const AddAppPage({super.key});
|
|
|
|
@override
|
|
State<AddAppPage> createState() => _AddAppPageState();
|
|
}
|
|
|
|
class _AddAppPageState extends State<AddAppPage> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final urlInputController = TextEditingController();
|
|
bool gettingAppInfo = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Center(
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
const Spacer(),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
|
child: TextFormField(
|
|
decoration: const InputDecoration(
|
|
hintText: 'https://github.com/Author/Project',
|
|
helperText: 'Enter the App source URL'),
|
|
controller: urlInputController,
|
|
validator: (value) {
|
|
if (value == null ||
|
|
value.isEmpty ||
|
|
Uri.tryParse(value) == null) {
|
|
return 'Please enter a supported source URL';
|
|
}
|
|
return null;
|
|
},
|
|
)),
|
|
Padding(
|
|
padding:
|
|
const EdgeInsets.symmetric(vertical: 16.0, horizontal: 16.0),
|
|
child: ElevatedButton(
|
|
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((_) {
|
|
urlInputController.clear();
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
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(),
|
|
],
|
|
),
|
|
));
|
|
}
|
|
}
|