2023-12-29 21:39:54 +01:00
|
|
|
// ignore_for_file: inference_failure_on_function_invocation
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
2024-01-08 15:38:31 +01:00
|
|
|
import 'package:dynamic_color/dynamic_color.dart';
|
|
|
|
import 'package:flex_color_picker/flex_color_picker.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
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';
|
2024-01-08 21:19:15 +01:00
|
|
|
import 'package:prasule/api/wallet_manager.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:prasule/main.dart';
|
2024-01-08 15:38:31 +01:00
|
|
|
import 'package:prasule/pw/platformbutton.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:prasule/pw/platformdialog.dart';
|
|
|
|
import 'package:prasule/pw/platformfield.dart';
|
|
|
|
import 'package:prasule/pw/platformroute.dart';
|
2024-01-08 15:38:31 +01:00
|
|
|
import 'package:prasule/util/text_color.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:prasule/views/settings/settings.dart';
|
|
|
|
import 'package:prasule/views/setup.dart';
|
2024-01-08 15:38:31 +01:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// 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;
|
2023-12-31 12:41:10 +01:00
|
|
|
logger.i(selectedWallet!.categories);
|
2023-12-29 21:39:54 +01:00
|
|
|
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,
|
2023-12-31 12:41:10 +01:00
|
|
|
children: selectedWallet == null
|
|
|
|
? [const CircularProgressIndicator()]
|
|
|
|
: [
|
|
|
|
Text(
|
|
|
|
AppLocalizations.of(context).setupCategoriesEditHint,
|
|
|
|
textAlign: TextAlign.center,
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-12-31 12:41:10 +01:00
|
|
|
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,
|
|
|
|
);
|
2024-01-08 15:38:31 +01:00
|
|
|
if (icon != null) {
|
|
|
|
selectedWallet!.categories[i].icon = icon;
|
|
|
|
}
|
|
|
|
final materialEnabled =
|
|
|
|
(await SharedPreferences.getInstance())
|
|
|
|
.getBool("useMaterialYou") ??
|
|
|
|
false;
|
2024-01-22 14:38:01 +01:00
|
|
|
if (!context.mounted) return;
|
2024-01-08 15:38:31 +01:00
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (c) => PlatformDialog(
|
|
|
|
actions: [
|
|
|
|
PlatformButton(
|
|
|
|
text: AppLocalizations.of(context).done,
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(c).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
title:
|
|
|
|
AppLocalizations.of(context).pickColor,
|
|
|
|
content: Column(
|
|
|
|
children: [
|
|
|
|
ColorPicker(
|
|
|
|
pickersEnabled: {
|
|
|
|
ColorPickerType.wheel: true,
|
|
|
|
ColorPickerType.primary: false,
|
|
|
|
ColorPickerType.custom: false,
|
|
|
|
ColorPickerType.bw: false,
|
|
|
|
ColorPickerType.accent:
|
|
|
|
materialEnabled,
|
|
|
|
},
|
|
|
|
color: selectedWallet!
|
|
|
|
.categories[i].color,
|
|
|
|
onColorChanged: (color) {
|
|
|
|
selectedWallet!
|
|
|
|
.categories[i].color = color;
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2023-12-31 12:41:10 +01:00
|
|
|
await WalletManager.saveWallet(selectedWallet!);
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: BorderRadius.circular(16),
|
2024-01-08 15:38:31 +01:00
|
|
|
color: selectedWallet!.categories[i].color,
|
2023-12-31 12:41:10 +01:00
|
|
|
),
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: Icon(
|
|
|
|
selectedWallet!.categories[i].icon,
|
2024-01-08 15:38:31 +01:00
|
|
|
color: selectedWallet!.categories[i].color
|
|
|
|
.calculateTextColor(),
|
2023-12-31 12:41:10 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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!,
|
|
|
|
);
|
2024-01-22 14:38:01 +01:00
|
|
|
if (!context.mounted) return;
|
2023-12-31 12:41:10 +01:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-12-31 12:41:10 +01:00
|
|
|
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',
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2024-01-08 15:38:31 +01:00
|
|
|
color: Colors.blueGrey.harmonizeWith(
|
|
|
|
Theme.of(context).colorScheme.primary,
|
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
|
|
|
);
|
2023-12-31 12:41:10 +01:00
|
|
|
await WalletManager.saveWallet(selectedWallet!);
|
|
|
|
setState(() {});
|
2023-12-29 21:39:54 +01:00
|
|
|
},
|
2023-12-31 12:41:10 +01:00
|
|
|
icon: const Icon(Icons.add),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-12-31 12:41:10 +01:00
|
|
|
],
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|