Compare commits
15 Commits
cad43c7664
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b3aee85a84 | |||
| a984269c15 | |||
| 600ff26016 | |||
| 9c85d565a9 | |||
| 446ef9a57a | |||
| 6103d0b679 | |||
| 5fd690197a | |||
| 31c0ade243 | |||
| 8ec264cebe | |||
| 83bfdf322b | |||
| d51f3d4ba7 | |||
| 06a76afc42 | |||
| dca8c64555 | |||
| 1aaea5f6d9 | |||
| 3a54a077f3 |
@@ -4,9 +4,6 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,28 +2,38 @@ import '../assets/constants.dart' as constants;
|
|||||||
|
|
||||||
class Settings {
|
class Settings {
|
||||||
final String exportDirectoryPath;
|
final String exportDirectoryPath;
|
||||||
|
final bool alwaysExportEnabled;
|
||||||
|
|
||||||
Settings._({required this.exportDirectoryPath});
|
Settings._({
|
||||||
|
required this.exportDirectoryPath,
|
||||||
|
required this.alwaysExportEnabled,
|
||||||
|
});
|
||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {'exportDirectoryPath': exportDirectoryPath};
|
return {
|
||||||
|
'exportDirectoryPath': exportDirectoryPath,
|
||||||
|
'alwaysExportEnabled': alwaysExportEnabled,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
factory Settings.fromJson(Map<String, dynamic> json) {
|
factory Settings.fromJson(Map<String, dynamic> json) {
|
||||||
return Settings._(
|
return Settings._(
|
||||||
exportDirectoryPath: json['exportDirectoryPath'] as String,
|
exportDirectoryPath: json['exportDirectoryPath'] as String,
|
||||||
|
alwaysExportEnabled: json['alwaysExportEnabled'] as bool,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
factory Settings.defaults() {
|
factory Settings.defaults() {
|
||||||
return Settings._(
|
return Settings._(
|
||||||
exportDirectoryPath: constants.defaultAndroidExportDirectory,
|
exportDirectoryPath: constants.defaultAndroidExportDirectory,
|
||||||
|
alwaysExportEnabled: false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Settings copyWith({String? exportDirectoryPath}) {
|
Settings copyWith({String? exportDirectoryPath, bool? alwaysExportEnabled}) {
|
||||||
return Settings._(
|
return Settings._(
|
||||||
exportDirectoryPath: exportDirectoryPath ?? this.exportDirectoryPath,
|
exportDirectoryPath: exportDirectoryPath ?? this.exportDirectoryPath,
|
||||||
|
alwaysExportEnabled: alwaysExportEnabled ?? this.alwaysExportEnabled,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import '../service/notifying.dart';
|
|||||||
import '../service/shared_link_provider.dart';
|
import '../service/shared_link_provider.dart';
|
||||||
import '../service/storage.dart';
|
import '../service/storage.dart';
|
||||||
import '../service/url_launcher.dart';
|
import '../service/url_launcher.dart';
|
||||||
|
import '../widgets/collection_page_widgets/list_item_actions_widget.dart';
|
||||||
import '../widgets/create_bookmark_dialog.dart';
|
import '../widgets/create_bookmark_dialog.dart';
|
||||||
|
|
||||||
class CollectionPage extends StatefulWidget {
|
class CollectionPage extends StatefulWidget {
|
||||||
@@ -22,6 +23,7 @@ class CollectionPage extends StatefulWidget {
|
|||||||
|
|
||||||
class _CollectionPageState extends State<CollectionPage> {
|
class _CollectionPageState extends State<CollectionPage> {
|
||||||
MapsLinkMetadata? selectedMapsLink;
|
MapsLinkMetadata? selectedMapsLink;
|
||||||
|
int selectedBookmarkId = -1;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -52,29 +54,46 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
).whenComplete(() => setState(() {}));
|
).whenComplete(() => setState(() {}));
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
).whenComplete(deselectBookmark);
|
||||||
|
|
||||||
void onBookmarkSaved(Bookmark bookmark) {
|
void onBookmarkSaved(Bookmark bookmark) {
|
||||||
Storage.addOrUpdateBookmark(bookmark);
|
Storage.addOrUpdateBookmark(bookmark);
|
||||||
setState(() {});
|
setState(() {});
|
||||||
context.read<SharedLinkProvider>().removeCurrentMapsLink();
|
Provider.of<SharedLinkProvider>(
|
||||||
|
context,
|
||||||
|
listen: false,
|
||||||
|
).removeCurrentMapsLink();
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) {
|
Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) {
|
||||||
|
final selected = selectedBookmarkId == bookmark.id;
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text(bookmark.name),
|
title: Text(bookmark.name),
|
||||||
onTap: () => launchUrlFromString(bookmark.link).then((errorCode) {
|
selected: selected,
|
||||||
if (context.mounted) {
|
onTap: () {
|
||||||
return Notifying.showUrlErrorSnackbar(context, errorCode);
|
if (selected) {
|
||||||
|
onCancelSelectionPressed();
|
||||||
|
setState(() {});
|
||||||
|
} else if (selectedBookmarkId != -1 && !selected) {
|
||||||
|
selectedBookmarkId = bookmark.id;
|
||||||
|
setState(() {});
|
||||||
|
} else {
|
||||||
|
launchUrlFromString(bookmark.link).then((errorCode) {
|
||||||
|
if (context.mounted) {
|
||||||
|
return Notifying.showUrlErrorSnackbar(context, errorCode);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
onLongPress: () => setState(() {
|
||||||
|
selectedBookmarkId = bookmark.id;
|
||||||
}),
|
}),
|
||||||
onLongPress: () => editBookmark(bookmark),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
SharedLinkProvider provider = context.watch<SharedLinkProvider>();
|
SharedLinkProvider provider = Provider.of<SharedLinkProvider>(context);
|
||||||
selectedMapsLink = provider.currentMapsLinkMetadata;
|
selectedMapsLink = provider.currentMapsLinkMetadata;
|
||||||
|
|
||||||
if (BookmarksProvider.selectedCollectionId == null) {
|
if (BookmarksProvider.selectedCollectionId == null) {
|
||||||
@@ -87,36 +106,69 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
(c) => c.id == BookmarksProvider.selectedCollectionId,
|
(c) => c.id == BookmarksProvider.selectedCollectionId,
|
||||||
);
|
);
|
||||||
|
|
||||||
return Scaffold(
|
return PopScope(
|
||||||
appBar: AppBar(
|
canPop: selectedBookmarkId == -1,
|
||||||
title: selectedMapsLink != null
|
onPopInvokedWithResult: (didPop, result) {
|
||||||
? Text(
|
if (didPop == false) deselectBookmark();
|
||||||
AppLocalizations.of(context)!.addToCollection(collection.name),
|
},
|
||||||
|
child: Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: selectedMapsLink != null
|
||||||
|
? Text(
|
||||||
|
AppLocalizations.of(
|
||||||
|
context,
|
||||||
|
)!.addToCollection(collection.name),
|
||||||
|
)
|
||||||
|
: Text(collection.name),
|
||||||
|
actions: [
|
||||||
|
if (selectedMapsLink != null)
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => provider.removeCurrentMapsLink(),
|
||||||
|
child: Text(AppLocalizations.of(context)!.cancel),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
bottomNavigationBar: selectedBookmarkId > 0
|
||||||
|
? ListItemActionsWidget(
|
||||||
|
onDeletePressed: onDeleteBookmarkPressed,
|
||||||
|
onCancelPressed: onCancelSelectionPressed,
|
||||||
|
onEditPressed: () => editBookmark(
|
||||||
|
bookmarks.firstWhere(
|
||||||
|
(element) => element.id == selectedBookmarkId,
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
: Text(collection.name),
|
: null,
|
||||||
actions: [
|
body: Center(
|
||||||
if (selectedMapsLink != null)
|
child: SizedBox(
|
||||||
TextButton(
|
width: MediaQuery.of(context).size.width * 0.9,
|
||||||
onPressed: () => provider.removeCurrentMapsLink(),
|
child: ListView.separated(
|
||||||
child: Text(AppLocalizations.of(context)!.cancel),
|
itemBuilder: (context, index) =>
|
||||||
|
bookmarksListItemBuilder(context, bookmarks.elementAt(index)),
|
||||||
|
itemCount: bookmarks.length,
|
||||||
|
separatorBuilder: (context, index) => SizedBox(height: 10),
|
||||||
),
|
),
|
||||||
],
|
|
||||||
),
|
|
||||||
body: Center(
|
|
||||||
child: SizedBox(
|
|
||||||
width: MediaQuery.of(context).size.width * 0.9,
|
|
||||||
child: ListView.separated(
|
|
||||||
itemBuilder: (context, index) =>
|
|
||||||
bookmarksListItemBuilder(context, bookmarks.elementAt(index)),
|
|
||||||
itemCount: bookmarks.length,
|
|
||||||
separatorBuilder: (context, index) => SizedBox(height: 10),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
floatingActionButton: FloatingActionButton(
|
||||||
floatingActionButton: FloatingActionButton(
|
onPressed: onAddButtonPressed,
|
||||||
onPressed: onAddButtonPressed,
|
child: Icon(selectedMapsLink != null ? Icons.save : Icons.add),
|
||||||
child: Icon(selectedMapsLink != null ? Icons.save : Icons.add),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onCancelSelectionPressed() => deselectBookmark();
|
||||||
|
|
||||||
|
void onDeleteBookmarkPressed() {
|
||||||
|
Storage.deleteBookmarkById(
|
||||||
|
selectedBookmarkId,
|
||||||
|
).whenComplete(() => setState(() {}));
|
||||||
|
deselectBookmark();
|
||||||
|
}
|
||||||
|
|
||||||
|
void deselectBookmark() {
|
||||||
|
selectedBookmarkId = -1;
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,8 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
|||||||
final collections = Storage.loadCollections();
|
final collections = Storage.loadCollections();
|
||||||
bookmarkCountMap = Storage.loadPerCollectionBookmarkCount();
|
bookmarkCountMap = Storage.loadPerCollectionBookmarkCount();
|
||||||
addingNewBookmark =
|
addingNewBookmark =
|
||||||
context.watch<SharedLinkProvider>().currentMapsLinkMetadata != null;
|
Provider.of<SharedLinkProvider>(context).currentMapsLinkMetadata !=
|
||||||
|
null;
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: addingNewBookmark
|
title: addingNewBookmark
|
||||||
@@ -96,8 +97,10 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
|||||||
actions: [
|
actions: [
|
||||||
if (addingNewBookmark)
|
if (addingNewBookmark)
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () =>
|
onPressed: () => Provider.of<SharedLinkProvider>(
|
||||||
context.read<SharedLinkProvider>().removeCurrentMapsLink(),
|
context,
|
||||||
|
listen: false,
|
||||||
|
).removeCurrentMapsLink(),
|
||||||
child: Text(AppLocalizations.of(context)!.cancel),
|
child: Text(AppLocalizations.of(context)!.cancel),
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import 'dart:async';
|
|||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import '../l10n/app_localizations.dart';
|
import '../l10n/app_localizations.dart';
|
||||||
import '../service/notifying.dart';
|
import '../service/notifying.dart';
|
||||||
import '../service/permission_service.dart';
|
import '../service/permission_service.dart';
|
||||||
|
import '../service/settings_provider.dart';
|
||||||
import '../service/storage.dart';
|
import '../service/storage.dart';
|
||||||
|
|
||||||
class SettingsPage extends StatefulWidget {
|
class SettingsPage extends StatefulWidget {
|
||||||
@@ -29,10 +31,16 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Localize
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final titlePadding = Theme.of(context).listTileTheme.contentPadding!;
|
final titlePadding = Theme.of(context).listTileTheme.contentPadding!;
|
||||||
checkStoragePermission;
|
checkStoragePermission();
|
||||||
|
final alwaysExportEnabled = context
|
||||||
|
.watch<SettingsProvider>()
|
||||||
|
.settings
|
||||||
|
.alwaysExportEnabled;
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings)),
|
appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings)),
|
||||||
body: Center(
|
body: Center(
|
||||||
@@ -51,10 +59,13 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
SizedBox(height: tileSpacing),
|
SizedBox(height: tileSpacing),
|
||||||
ListTile(
|
ListTile(
|
||||||
title: Text('Grant storage permisson'),
|
title: Text('Grant storage permisson'),
|
||||||
subtitle: Text(
|
subtitle: storagePermissionIsGranted
|
||||||
'For app-data settings to work, you need to grant the app permissions to manage internal storage.',
|
? Text('Storage permission granted')
|
||||||
),
|
: Text(
|
||||||
onTap: () => PermissionService.requestStoragePermission,
|
'For app-data settings to work, you need to grant the app permissions to manage internal storage.',
|
||||||
|
),
|
||||||
|
onTap: () => PermissionService.requestStoragePermission
|
||||||
|
.whenComplete(() => checkStoragePermission()),
|
||||||
trailing: Icon(Icons.arrow_forward_ios_rounded),
|
trailing: Icon(Icons.arrow_forward_ios_rounded),
|
||||||
enabled: !storagePermissionIsGranted,
|
enabled: !storagePermissionIsGranted,
|
||||||
),
|
),
|
||||||
@@ -62,7 +73,7 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
ListTile(
|
ListTile(
|
||||||
title: Text(AppLocalizations.of(context)!.import),
|
title: Text(AppLocalizations.of(context)!.import),
|
||||||
subtitle: Text('Import app-data from a json file.'),
|
subtitle: Text('Import app-data from a json file.'),
|
||||||
onTap: () => onActivateJsonImportPressed(),
|
onTap: () => onJsonImportPressed(),
|
||||||
trailing: Icon(Icons.arrow_forward_ios_rounded),
|
trailing: Icon(Icons.arrow_forward_ios_rounded),
|
||||||
enabled: storagePermissionIsGranted,
|
enabled: storagePermissionIsGranted,
|
||||||
),
|
),
|
||||||
@@ -72,10 +83,39 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
subtitle: Text(
|
subtitle: Text(
|
||||||
'Export app-data to a json file in the selected directory.',
|
'Export app-data to a json file in the selected directory.',
|
||||||
),
|
),
|
||||||
onTap: () => onActivateJsonExportPressed(),
|
onTap: () => onJsonExportPressed(),
|
||||||
trailing: Icon(Icons.arrow_forward_ios_rounded),
|
trailing: Icon(Icons.arrow_forward_ios_rounded),
|
||||||
enabled: storagePermissionIsGranted,
|
enabled: storagePermissionIsGranted,
|
||||||
),
|
),
|
||||||
|
SizedBox(height: tileSpacing),
|
||||||
|
ListTile(
|
||||||
|
title: Text('Always save to file'),
|
||||||
|
subtitle: Text(
|
||||||
|
'Export app data to a directory, every time you make a change',
|
||||||
|
),
|
||||||
|
onTap: () => onAlwaysSaveToJsonPressed(),
|
||||||
|
trailing: Checkbox(
|
||||||
|
value: alwaysExportEnabled,
|
||||||
|
onChanged: (value) {
|
||||||
|
onAlwaysSaveToJsonPressed();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
enabled: storagePermissionIsGranted,
|
||||||
|
),
|
||||||
|
if (alwaysExportEnabled) SizedBox(height: tileSpacing),
|
||||||
|
if (alwaysExportEnabled)
|
||||||
|
ListTile(
|
||||||
|
title: Text('Change export directory'),
|
||||||
|
subtitle: Text(
|
||||||
|
context
|
||||||
|
.watch<SettingsProvider>()
|
||||||
|
.settings
|
||||||
|
.exportDirectoryPath,
|
||||||
|
),
|
||||||
|
onTap: () => onChangeExportDirectoryPressed(),
|
||||||
|
trailing: Icon(Icons.arrow_forward_ios_rounded),
|
||||||
|
enabled: storagePermissionIsGranted,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -83,23 +123,54 @@ class _SettingsPageState extends State<SettingsPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onActivateJsonExportPressed() async {
|
void onJsonExportPressed() async {
|
||||||
if (!await PermissionService.storagePermissionStatus.isGranted) return;
|
if (!await checkStoragePermission()) return;
|
||||||
Storage.exportToJsonFile().then(showExportInfo);
|
Storage.exportToJsonFile().then(showExportInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onActivateJsonImportPressed() async {
|
void onJsonImportPressed() async {
|
||||||
if (!await PermissionService.storagePermissionStatus.isGranted) return;
|
if (!await checkStoragePermission()) return;
|
||||||
Storage.importFromJsonFile().then(showImportInfo);
|
Storage.importFromJsonFile().then(showImportInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> get checkStoragePermission async {
|
void onAlwaysSaveToJsonPressed() async {
|
||||||
|
if (context.read<SettingsProvider>().settings.alwaysExportEnabled) {
|
||||||
|
context.read<SettingsProvider>().setExportDirectoryPath('', silent: true);
|
||||||
|
context.read<SettingsProvider>().setAlwaysExportEnabled(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await PermissionService.storagePermissionStatus.isGranted) return;
|
||||||
|
final dir = await Storage.selectDirectoryPath();
|
||||||
|
if (dir.isEmpty || !context.mounted) return;
|
||||||
|
|
||||||
|
// ignore: use_build_context_synchronously
|
||||||
|
context.read<SettingsProvider>().setExportDirectoryPath(dir, silent: true);
|
||||||
|
|
||||||
|
// ignore: use_build_context_synchronously
|
||||||
|
Storage.saveDataToFile().whenComplete(
|
||||||
|
// ignore: use_build_context_synchronously
|
||||||
|
() => context.read<SettingsProvider>().setAlwaysExportEnabled(true),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onChangeExportDirectoryPressed() async {
|
||||||
|
if (!await PermissionService.storagePermissionStatus.isGranted) return;
|
||||||
|
final dir = await Storage.selectDirectoryPath();
|
||||||
|
if (dir.isEmpty || !context.mounted) return;
|
||||||
|
|
||||||
|
// ignore: use_build_context_synchronously
|
||||||
|
context.read<SettingsProvider>().setExportDirectoryPath(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> checkStoragePermission() async {
|
||||||
PermissionService.storagePermissionStatus.then((value) {
|
PermissionService.storagePermissionStatus.then((value) {
|
||||||
storagePermissionIsGranted = value.isGranted;
|
|
||||||
if (context.mounted && value.isGranted != storagePermissionIsGranted) {
|
if (context.mounted && value.isGranted != storagePermissionIsGranted) {
|
||||||
|
storagePermissionIsGranted = value.isGranted;
|
||||||
setState(() {});
|
setState(() {});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return storagePermissionIsGranted;
|
||||||
}
|
}
|
||||||
|
|
||||||
void showExportInfo(bool success) => Notifying.showSnackbar(
|
void showExportInfo(bool success) => Notifying.showSnackbar(
|
||||||
|
|||||||
@@ -14,21 +14,10 @@ class JsonFileService {
|
|||||||
required List<Bookmark> bookmarks,
|
required List<Bookmark> bookmarks,
|
||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
final dir = await _directoryPath;
|
final dir = await selectDirectoryPath();
|
||||||
if (dir.isEmpty) return false;
|
if (dir.isEmpty) return false;
|
||||||
|
|
||||||
final data = {
|
saveDataToFile(collections, bookmarks, dir);
|
||||||
'collections': collections.map((c) => c.toJson()).toList(),
|
|
||||||
'bookmarks': bookmarks.map((b) => b.toJson()).toList(),
|
|
||||||
};
|
|
||||||
final json = jsonEncode(data).codeUnits;
|
|
||||||
final file = XFile.fromData(
|
|
||||||
Uint8List.fromList(json),
|
|
||||||
mimeType: 'application/json',
|
|
||||||
name: constants.jsonFileName,
|
|
||||||
);
|
|
||||||
|
|
||||||
file.saveTo('$dir/${constants.jsonFileName}');
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -65,7 +54,31 @@ class JsonFileService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<String> get _directoryPath async {
|
static Future<bool> saveDataToFile(
|
||||||
|
List<Collection> collections,
|
||||||
|
List<Bookmark> bookmarks,
|
||||||
|
String directory,
|
||||||
|
) async {
|
||||||
|
try {
|
||||||
|
final data = jsonEncode({
|
||||||
|
'collections': collections.map((c) => c.toJson()).toList(),
|
||||||
|
'bookmarks': bookmarks.map((b) => b.toJson()).toList(),
|
||||||
|
}).codeUnits;
|
||||||
|
|
||||||
|
final file = XFile.fromData(
|
||||||
|
Uint8List.fromList(data),
|
||||||
|
mimeType: 'application/json',
|
||||||
|
name: constants.jsonFileName,
|
||||||
|
);
|
||||||
|
|
||||||
|
file.saveTo('$directory/${constants.jsonFileName}');
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<String> selectDirectoryPath() async {
|
||||||
if (Platform.isAndroid) {
|
if (Platform.isAndroid) {
|
||||||
return await getDirectoryPath(
|
return await getDirectoryPath(
|
||||||
initialDirectory: constants.defaultAndroidExportDirectory,
|
initialDirectory: constants.defaultAndroidExportDirectory,
|
||||||
|
|||||||
@@ -12,9 +12,13 @@ class SettingsProvider extends ChangeNotifier {
|
|||||||
|
|
||||||
void setExportDirectoryPath(String path, {bool silent = false}) {
|
void setExportDirectoryPath(String path, {bool silent = false}) {
|
||||||
_settings = _settings.copyWith(exportDirectoryPath: path);
|
_settings = _settings.copyWith(exportDirectoryPath: path);
|
||||||
_saveSettings();
|
Storage.saveSettings(_settings);
|
||||||
if (!silent) notifyListeners();
|
if (!silent) notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _saveSettings() => Storage.saveSettings(_settings);
|
void setAlwaysExportEnabled(bool enabled, {bool silent = false}) {
|
||||||
|
_settings = _settings.copyWith(alwaysExportEnabled: enabled);
|
||||||
|
Storage.saveSettings(_settings);
|
||||||
|
if (!silent) notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class Storage {
|
|||||||
static SharedPreferencesWithCache? _prefsWithCache;
|
static SharedPreferencesWithCache? _prefsWithCache;
|
||||||
static const String _settingsKey = 'settings';
|
static const String _settingsKey = 'settings';
|
||||||
static const String _statsKey = 'stats';
|
static const String _statsKey = 'stats';
|
||||||
|
static Settings _currentSettings = Settings.defaults();
|
||||||
|
|
||||||
static Future<void> initialize() async {
|
static Future<void> initialize() async {
|
||||||
_prefsWithCache = await SharedPreferencesWithCache.create(
|
_prefsWithCache = await SharedPreferencesWithCache.create(
|
||||||
@@ -31,7 +32,8 @@ class Storage {
|
|||||||
final jsonString = _prefs.getString(_settingsKey);
|
final jsonString = _prefs.getString(_settingsKey);
|
||||||
if (jsonString != null) {
|
if (jsonString != null) {
|
||||||
final json = jsonDecode(jsonString) as Map<String, dynamic>;
|
final json = jsonDecode(jsonString) as Map<String, dynamic>;
|
||||||
return Settings.fromJson(json);
|
_currentSettings = Settings.fromJson(json);
|
||||||
|
return _currentSettings;
|
||||||
} else {
|
} else {
|
||||||
final settings = Settings.defaults();
|
final settings = Settings.defaults();
|
||||||
saveSettings(settings);
|
saveSettings(settings);
|
||||||
@@ -41,6 +43,7 @@ class Storage {
|
|||||||
|
|
||||||
static Future<void> saveSettings(Settings settings) {
|
static Future<void> saveSettings(Settings settings) {
|
||||||
final json = jsonEncode(settings.toJson());
|
final json = jsonEncode(settings.toJson());
|
||||||
|
_currentSettings = settings;
|
||||||
return _prefs.setString(_settingsKey, json);
|
return _prefs.setString(_settingsKey, json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,11 +66,13 @@ class Storage {
|
|||||||
static Future<void> saveCollections(List<Collection> collections) async {
|
static Future<void> saveCollections(List<Collection> collections) async {
|
||||||
final jsonList = collections.map((c) => c.toJson()).toList();
|
final jsonList = collections.map((c) => c.toJson()).toList();
|
||||||
await _prefs.setString(_collectionsKey, jsonEncode(jsonList));
|
await _prefs.setString(_collectionsKey, jsonEncode(jsonList));
|
||||||
|
if (_currentSettings.alwaysExportEnabled) saveDataToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> saveBookmarks(List<Bookmark> bookmarks) async {
|
static Future<void> saveBookmarks(List<Bookmark> bookmarks) async {
|
||||||
final jsonList = bookmarks.map((b) => b.toJson()).toList();
|
final jsonList = bookmarks.map((b) => b.toJson()).toList();
|
||||||
await _prefs.setString(_bookmarksKey, jsonEncode(jsonList));
|
await _prefs.setString(_bookmarksKey, jsonEncode(jsonList));
|
||||||
|
if (_currentSettings.alwaysExportEnabled) saveDataToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<Bookmark> loadBookmarksForCollection(int collectionId) {
|
static List<Bookmark> loadBookmarksForCollection(int collectionId) {
|
||||||
@@ -196,6 +201,15 @@ class Storage {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<String> selectDirectoryPath() =>
|
||||||
|
JsonFileService.selectDirectoryPath();
|
||||||
|
|
||||||
|
static Future<bool> saveDataToFile() => JsonFileService.saveDataToFile(
|
||||||
|
loadCollections(),
|
||||||
|
loadBookmarks(),
|
||||||
|
_currentSettings.exportDirectoryPath,
|
||||||
|
);
|
||||||
|
|
||||||
static SharedPreferencesWithCache get _prefs {
|
static SharedPreferencesWithCache get _prefs {
|
||||||
if (_prefsWithCache == null) {
|
if (_prefsWithCache == null) {
|
||||||
throw StateError(
|
throw StateError(
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ ThemeData _baseTheme(ColorScheme scheme) =>
|
|||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
borderRadius: BorderRadiusGeometry.circular(12),
|
borderRadius: BorderRadiusGeometry.circular(12),
|
||||||
),
|
),
|
||||||
|
textColor: scheme.onPrimaryContainer,
|
||||||
|
selectedTileColor: scheme.primaryContainer,
|
||||||
|
selectedColor: scheme.onPrimaryContainer,
|
||||||
contentPadding: EdgeInsetsDirectional.only(start: 16.0, end: 24.0),
|
contentPadding: EdgeInsetsDirectional.only(start: 16.0, end: 24.0),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class ListItemActionsWidget extends StatelessWidget {
|
||||||
|
final VoidCallback _onDeletePressed;
|
||||||
|
final VoidCallback _onCancelPressed;
|
||||||
|
final VoidCallback _onEditPressed;
|
||||||
|
|
||||||
|
const ListItemActionsWidget({
|
||||||
|
super.key,
|
||||||
|
required void Function() onDeletePressed,
|
||||||
|
required void Function() onCancelPressed,
|
||||||
|
required void Function() onEditPressed,
|
||||||
|
}) : _onEditPressed = onEditPressed,
|
||||||
|
_onCancelPressed = onCancelPressed,
|
||||||
|
_onDeletePressed = onDeletePressed;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Container(
|
||||||
|
padding: EdgeInsetsGeometry.fromLTRB(
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
MediaQuery.of(context).viewPadding.bottom,
|
||||||
|
),
|
||||||
|
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||||
|
child: Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
IconButton(
|
||||||
|
onPressed: _onCancelPressed,
|
||||||
|
icon: const Icon(Icons.close_rounded),
|
||||||
|
),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
IconButton(
|
||||||
|
onPressed: _onDeletePressed,
|
||||||
|
icon: const Icon(Icons.delete_forever_rounded),
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
onPressed: _onEditPressed,
|
||||||
|
icon: const Icon(Icons.edit_rounded),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user