|
1 | 1 | import 'package:currency_picker/currency_picker.dart';
|
| 2 | +import 'package:file_picker/file_picker.dart'; |
2 | 3 | import 'package:fintracker/bloc/cubit/app_cubit.dart';
|
3 | 4 | import 'package:fintracker/helpers/color.helper.dart';
|
4 | 5 | import 'package:fintracker/helpers/db.helper.dart';
|
5 |
| -import 'package:fintracker/theme/colors.dart'; |
6 | 6 | import 'package:fintracker/widgets/buttons/button.dart';
|
7 | 7 | import 'package:fintracker/widgets/dialog/confirm.modal.dart';
|
| 8 | +import 'package:fintracker/widgets/dialog/loading_dialog.dart'; |
8 | 9 | import 'package:flutter/material.dart';
|
9 | 10 | import 'package:flutter_bloc/flutter_bloc.dart';
|
10 | 11 | import 'package:material_symbols_icons/material_symbols_icons.dart';
|
@@ -107,28 +108,98 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
107 | 108 | ),
|
108 | 109 | ListTile(
|
109 | 110 | dense: true,
|
110 |
| - onTap: () async { |
| 111 | + onTap:() async { |
111 | 112 | ConfirmModal.showConfirmDialog(
|
112 | 113 | context, title: "Are you sure?",
|
113 |
| - content: const Text("After deleting data can't be recovered"), |
| 114 | + content: const Text("want to export all the data to a file"), |
114 | 115 | onConfirm: ()async{
|
115 | 116 | Navigator.of(context).pop();
|
116 |
| - Navigator.of(context).pop(); |
117 |
| - await context.read<AppCubit>().reset(); |
118 |
| - await resetDatabase(); |
| 117 | + LoadingModal.showLoadingDialog(context, content: const Text("Exporting data please wait")); |
| 118 | + await export().then((value){ |
| 119 | + ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text("File has been saved in $value"))); |
| 120 | + }).catchError((err){ |
| 121 | + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Something went wrong while exporting data"))); |
| 122 | + }).whenComplete((){ |
| 123 | + Navigator.of(context).pop(); |
| 124 | + }); |
119 | 125 | },
|
120 | 126 | onCancel: (){
|
121 | 127 | Navigator.of(context).pop();
|
122 | 128 | }
|
123 | 129 | );
|
124 | 130 | },
|
125 |
| - leading: CircleAvatar( |
126 |
| - backgroundColor: ThemeColors.error.withAlpha(90), |
127 |
| - child: const Icon(Icons.delete,) |
| 131 | + leading: const CircleAvatar( |
| 132 | + child: Icon(Symbols.download,) |
| 133 | + ), |
| 134 | + title: Text('Export', style: Theme.of(context).textTheme.bodyMedium?.merge(const TextStyle(fontWeight: FontWeight.w500, fontSize: 15))), |
| 135 | + subtitle: Text("Export to file",style: Theme.of(context).textTheme.bodySmall?.apply(color: Colors.grey, overflow: TextOverflow.ellipsis)), |
| 136 | + ), |
| 137 | + ListTile( |
| 138 | + dense: true, |
| 139 | + onTap:() async { |
| 140 | + await FilePicker.platform.pickFiles( |
| 141 | + dialogTitle: "Pick file", |
| 142 | + allowMultiple: false, |
| 143 | + allowCompression: false, |
| 144 | + type:FileType.custom, |
| 145 | + allowedExtensions: ["json"] |
| 146 | + ).then((pick){ |
| 147 | + if(pick == null || pick.files.isEmpty) { |
| 148 | + return ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Please select file"))); |
| 149 | + } |
| 150 | + PlatformFile file = pick.files.first; |
| 151 | + ConfirmModal.showConfirmDialog( |
| 152 | + context, title: "Are you sure?", |
| 153 | + content: const Text("All payment data, categories, and accounts will be erased and replaced with the information imported from the backup."), |
| 154 | + onConfirm: ()async{ |
| 155 | + Navigator.of(context).pop(); |
| 156 | + LoadingModal.showLoadingDialog(context, content: const Text("Exporting data please wait")); |
| 157 | + await import(file.path!).then((value){ |
| 158 | + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Successfully imported."))); |
| 159 | + Navigator.of(context).pop(); |
| 160 | + }).catchError((err){ |
| 161 | + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Something went wrong while importing data"))); |
| 162 | + }); |
| 163 | + }, |
| 164 | + onCancel: (){ |
| 165 | + Navigator.of(context).pop(); |
| 166 | + } |
| 167 | + ); |
| 168 | + }).catchError((err){ |
| 169 | + return ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Something went wrong while importing data"))); |
| 170 | + }); |
| 171 | + }, |
| 172 | + leading: const CircleAvatar( |
| 173 | + child: Icon(Symbols.upload,) |
128 | 174 | ),
|
129 |
| - title: Text('Reset', style: Theme.of(context).textTheme.bodyMedium?.merge(const TextStyle(fontWeight: FontWeight.w500, fontSize: 15))), |
130 |
| - subtitle: Text("Delete all the data",style: Theme.of(context).textTheme.bodySmall?.apply(color: Colors.grey, overflow: TextOverflow.ellipsis)), |
| 175 | + title: Text('Import', style: Theme.of(context).textTheme.bodyMedium?.merge(const TextStyle(fontWeight: FontWeight.w500, fontSize: 15))), |
| 176 | + subtitle: Text("Import from backup file",style: Theme.of(context).textTheme.bodySmall?.apply(color: Colors.grey, overflow: TextOverflow.ellipsis)), |
| 177 | + |
131 | 178 | ),
|
| 179 | + // ListTile( |
| 180 | + // dense: true, |
| 181 | + // onTap: () async { |
| 182 | + // ConfirmModal.showConfirmDialog( |
| 183 | + // context, title: "Are you sure?", |
| 184 | + // content: const Text("After deleting data can't be recovered"), |
| 185 | + // onConfirm: ()async{ |
| 186 | + // Navigator.of(context).pop(); |
| 187 | + // Navigator.of(context).pop(); |
| 188 | + // await context.read<AppCubit>().reset(); |
| 189 | + // await resetDatabase(); |
| 190 | + // }, |
| 191 | + // onCancel: (){ |
| 192 | + // Navigator.of(context).pop(); |
| 193 | + // } |
| 194 | + // ); |
| 195 | + // }, |
| 196 | + // leading: CircleAvatar( |
| 197 | + // backgroundColor: ThemeColors.error.withAlpha(90), |
| 198 | + // child: const Icon(Symbols.device_reset,) |
| 199 | + // ), |
| 200 | + // title: Text('Reset', style: Theme.of(context).textTheme.bodyMedium?.merge(const TextStyle(fontWeight: FontWeight.w500, fontSize: 15))), |
| 201 | + // subtitle: Text("Delete all the data",style: Theme.of(context).textTheme.bodySmall?.apply(color: Colors.grey, overflow: TextOverflow.ellipsis)), |
| 202 | + // ), |
132 | 203 | ],
|
133 | 204 | )
|
134 | 205 | );
|
|
0 commit comments