Json export and import feature #5
@@ -1,15 +1,62 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../service/notifying.dart';
|
||||
import '../service/permission_service.dart';
|
||||
import '../service/storage.dart';
|
||||
|
||||
class SettingsPage extends StatelessWidget {
|
||||
class SettingsPage extends StatefulWidget {
|
||||
static const routeName = '/settings';
|
||||
const SettingsPage({super.key});
|
||||
|
||||
@override
|
||||
State<SettingsPage> createState() => _SettingsPageState();
|
||||
}
|
||||
|
||||
class _SettingsPageState extends State<SettingsPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings)),
|
||||
body: SizedBox(
|
||||
width: MediaQuery.of(context).size.width * 0.9,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
AppLocalizations.of(context)!.appData,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => onActivateJsonImportPressed(),
|
||||
child: Text(AppLocalizations.of(context)!.import),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () => onActivateJsonExportPressed(),
|
||||
child: Text(AppLocalizations.of(context)!.export),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void onActivateJsonExportPressed() async {
|
||||
if (await checkStoragePermission) Storage.exportToJsonFile();
|
||||
}
|
||||
|
||||
void onActivateJsonImportPressed() async {
|
||||
if (await checkStoragePermission) Storage.importFromJsonFile();
|
||||
}
|
||||
|
||||
Future<bool> get checkStoragePermission async {
|
||||
if (!(await PermissionService.requestStoragePermission).isGranted) {
|
||||
if (mounted) {
|
||||
Notifying.showStoragePermissionErrorSnackbar(context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
69
lib/service/json_file_service.dart
Normal file
69
lib/service/json_file_service.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../model/bookmark.dart';
|
||||
import '../model/collection.dart';
|
||||
import '../assets/constants.dart' as constants;
|
||||
|
||||
class JsonFileService {
|
||||
static Future<bool> exportToJson({
|
||||
required List<Collection> collections,
|
||||
required List<Bookmark> bookmarks,
|
||||
}) async {
|
||||
final dir = await _directoryPath;
|
||||
|
||||
final data = {
|
||||
'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}');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static Future<({List<Collection> collections, List<Bookmark> bookmarks})>
|
||||
importFromJson() async {
|
||||
const typeGroup = XTypeGroup(label: 'json', extensions: <String>['json']);
|
||||
final XFile? file = await openFile(
|
||||
acceptedTypeGroups: <XTypeGroup>[typeGroup],
|
||||
);
|
||||
|
||||
if (file == null) {
|
||||
return (collections: <Collection>[], bookmarks: <Bookmark>[]);
|
||||
}
|
||||
|
||||
final jsonString = await file.readAsString();
|
||||
|
||||
final data = jsonDecode(jsonString) as Map<String, dynamic>;
|
||||
|
||||
final collections = (data['collections'] as List<dynamic>? ?? [])
|
||||
.map((json) => Collection.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
final bookmarks = (data['bookmarks'] as List<dynamic>? ?? [])
|
||||
.map((json) => Bookmark.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
return (collections: collections, bookmarks: bookmarks);
|
||||
}
|
||||
|
||||
static Future<String> get _directoryPath async {
|
||||
if (Platform.isAndroid) {
|
||||
return await getDirectoryPath(
|
||||
initialDirectory: constants.defaultAndroidExportDirectory,
|
||||
) ??
|
||||
'';
|
||||
}
|
||||
return await getDirectoryPath() ?? '';
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../model/bookmark.dart';
|
||||
import '../model/collection.dart';
|
||||
import 'json_file_service.dart';
|
||||
|
||||
class Storage {
|
||||
static const String _bookmarksKey = 'bookmarks';
|
||||
@@ -163,4 +164,20 @@ class Storage {
|
||||
}
|
||||
return _prefsWithCache!;
|
||||
}
|
||||
|
||||
static Future<bool> exportToJsonFile() => JsonFileService.exportToJson(
|
||||
collections: loadCollections(),
|
||||
bookmarks: loadBookmarks(),
|
||||
);
|
||||
|
||||
static Future<bool> importFromJsonFile() async {
|
||||
final import = await JsonFileService.importFromJson();
|
||||
|
||||
if (import.bookmarks.isNotEmpty || import.collections.isNotEmpty) {
|
||||
saveBookmarks(import.bookmarks);
|
||||
saveCollections(import.collections);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user