238caf9203
Also make sure home loads the wallet again after exiting settings. Also removed 'type' from Category, because I don't know what it was supposed to do there.
244 lines
9.6 KiB
Dart
244 lines
9.6 KiB
Dart
// ignore_for_file: inference_failure_on_function_invocation
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
import 'package:flutter_iconpicker/flutter_iconpicker.dart';
|
|
import 'package:prasule/api/category.dart';
|
|
import 'package:prasule/api/wallet.dart';
|
|
import 'package:prasule/api/walletmanager.dart';
|
|
import 'package:prasule/main.dart';
|
|
import 'package:prasule/pw/platformdialog.dart';
|
|
import 'package:prasule/pw/platformfield.dart';
|
|
import 'package:prasule/pw/platformroute.dart';
|
|
import 'package:prasule/views/settings/settings.dart';
|
|
import 'package:prasule/views/setup.dart';
|
|
|
|
/// Allows adding, editing or removing [WalletCategory]s
|
|
class EditCategoriesView extends StatefulWidget {
|
|
/// Allows adding, editing or removing [WalletCategory]s
|
|
const EditCategoriesView({super.key});
|
|
|
|
@override
|
|
State<EditCategoriesView> createState() => _EditCategoriesViewState();
|
|
}
|
|
|
|
class _EditCategoriesViewState extends State<EditCategoriesView> {
|
|
Wallet? selectedWallet;
|
|
List<Wallet> wallets = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
loadWallet();
|
|
}
|
|
|
|
Future<void> loadWallet() async {
|
|
wallets = await WalletManager.listWallets();
|
|
if (wallets.isEmpty && mounted) {
|
|
unawaited(
|
|
Navigator.of(context)
|
|
.pushReplacement(platformRoute((c) => const SetupView())),
|
|
);
|
|
return;
|
|
}
|
|
selectedWallet = wallets.first;
|
|
logger.i(selectedWallet!.categories);
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: DropdownButton<int>(
|
|
value:
|
|
(selectedWallet == null) ? -1 : wallets.indexOf(selectedWallet!),
|
|
items: [
|
|
...wallets.map(
|
|
(e) => DropdownMenuItem(
|
|
value: wallets.indexOf(
|
|
e,
|
|
),
|
|
child: Text(e.name),
|
|
),
|
|
),
|
|
DropdownMenuItem(
|
|
value: -1,
|
|
child: Text(AppLocalizations.of(context).newWallet),
|
|
),
|
|
],
|
|
onChanged: (v) async {
|
|
if (v == null || v == -1) {
|
|
await Navigator.of(context).push(
|
|
platformRoute(
|
|
(c) => const SetupView(
|
|
newWallet: true,
|
|
),
|
|
),
|
|
);
|
|
wallets = await WalletManager.listWallets();
|
|
logger.i(wallets.length);
|
|
selectedWallet = wallets.last;
|
|
setState(() {});
|
|
return;
|
|
}
|
|
selectedWallet = wallets[v];
|
|
setState(() {});
|
|
},
|
|
),
|
|
actions: [
|
|
PopupMenuButton(
|
|
itemBuilder: (context) => [
|
|
AppLocalizations.of(context).settings,
|
|
AppLocalizations.of(context).about,
|
|
].map((e) => PopupMenuItem(value: e, child: Text(e))).toList(),
|
|
onSelected: (value) {
|
|
if (value == AppLocalizations.of(context).settings) {
|
|
Navigator.of(context).push(
|
|
platformRoute(
|
|
(context) => const SettingsView(),
|
|
),
|
|
);
|
|
} else if (value == AppLocalizations.of(context).about) {
|
|
showAboutDialog(
|
|
context: context,
|
|
applicationLegalese: AppLocalizations.of(context).license,
|
|
applicationName: "Prašule",
|
|
);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: selectedWallet == null
|
|
? [const CircularProgressIndicator()]
|
|
: [
|
|
Text(
|
|
AppLocalizations.of(context).setupCategoriesEditHint,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
SizedBox(
|
|
height: MediaQuery.of(context).size.height * 0.64,
|
|
child: ListView.builder(
|
|
shrinkWrap: true,
|
|
itemBuilder: (context, i) => (i == 0)
|
|
? const SizedBox()
|
|
: ListTile(
|
|
leading: GestureDetector(
|
|
onTap: () async {
|
|
final icon =
|
|
await FlutterIconPicker.showIconPicker(
|
|
context,
|
|
);
|
|
if (icon == null) return;
|
|
selectedWallet!.categories[i].icon = icon;
|
|
await WalletManager.saveWallet(selectedWallet!);
|
|
setState(() {});
|
|
},
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(16),
|
|
color:
|
|
Theme.of(context).colorScheme.secondary,
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8),
|
|
child: Icon(
|
|
selectedWallet!.categories[i].icon,
|
|
color: Theme.of(context)
|
|
.colorScheme
|
|
.onSecondary,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
trailing: IconButton(
|
|
icon: const Icon(Icons.cancel),
|
|
onPressed: () async {
|
|
await selectedWallet!.removeCategory(
|
|
selectedWallet!.categories[i],
|
|
);
|
|
setState(() {});
|
|
},
|
|
),
|
|
title: GestureDetector(
|
|
onTap: () {
|
|
final controller = TextEditingController(
|
|
text: selectedWallet!.categories[i].name,
|
|
);
|
|
showDialog(
|
|
context: context,
|
|
builder: (c) => PlatformDialog(
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () async {
|
|
if (controller.text.isEmpty) return;
|
|
selectedWallet!.categories[i].name =
|
|
controller.text;
|
|
await WalletManager.saveWallet(
|
|
selectedWallet!,
|
|
);
|
|
if (!mounted) return;
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text(
|
|
AppLocalizations.of(context).ok,
|
|
),
|
|
),
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text(
|
|
AppLocalizations.of(context).cancel,
|
|
),
|
|
),
|
|
],
|
|
title: AppLocalizations.of(context)
|
|
.setupCategoriesEditingName,
|
|
content: SizedBox(
|
|
width: 400,
|
|
child:
|
|
PlatformField(controller: controller),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
child: Text(
|
|
selectedWallet!.categories[i].name,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
itemCount: selectedWallet!.categories.length,
|
|
),
|
|
),
|
|
IconButton(
|
|
onPressed: () async {
|
|
selectedWallet!.categories.add(
|
|
WalletCategory(
|
|
name: AppLocalizations.of(context)
|
|
.setupWalletNamePlaceholder,
|
|
id: selectedWallet!.nextCategoryId,
|
|
icon: IconData(
|
|
Icons.question_mark.codePoint,
|
|
fontFamily: 'MaterialIcons',
|
|
),
|
|
),
|
|
);
|
|
await WalletManager.saveWallet(selectedWallet!);
|
|
setState(() {});
|
|
},
|
|
icon: const Icon(Icons.add),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|