Skip to content

Commit

Permalink
Refactor savePath reset and SharedPreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Voklen committed May 22, 2023
1 parent c583b23 commit c963037
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
5 changes: 3 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:daily_diary/storage.dart';
import 'package:daily_diary/themes.dart';
import 'package:daily_diary/screens/home.dart';

import 'package:shared_storage/saf.dart';
import 'package:shared_preferences/shared_preferences.dart';

// This will be removed when widgets can react to spell check changes
bool? startupCheckSpelling;
Expand All @@ -31,7 +31,8 @@ main() async {
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

static final SettingsNotifier settingsNotifier = SettingsNotifier(savePath!);
static final settingsNotifier = SettingsNotifier(savePath!);
static final preferences = SharedPreferences.getInstance();

@override
Widget build(BuildContext context) {
Expand Down
27 changes: 20 additions & 7 deletions lib/path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';

import 'package:daily_diary/main.dart';

import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_storage/saf.dart';

class SavePath {
Expand Down Expand Up @@ -53,7 +54,7 @@ class SavePath {

Future<SavePath> getPath() async {
WidgetsFlutterBinding.ensureInitialized();
final preferences = await SharedPreferences.getInstance();
final preferences = await App.preferences;
String? path = preferences.getString('save_path');
bool? isAndroidScoped = preferences.getBool('is_android_scoped');
if (path != null) {
Expand All @@ -63,14 +64,26 @@ Future<SavePath> getPath() async {
}
return SavePath.normal(path);
} else {
String path = await defaultPath;
preferences.setString('save_path', path);
preferences.setBool('is_android_scoped', false);
return SavePath.normal(path);
return resetPathToDefault();
}
}

Future<String> get defaultPath async {
/// Resets the savePath to default in `SharedPreferences` and returns the
/// `SavePath` it was set to. It does NOT set the global `savePath`.
///
/// To set `savePath` do:
/// ```
/// savePath = await resetPathToDefault();
/// ```
Future<SavePath> resetPathToDefault() async {
final preferences = await App.preferences;
String path = await _defaultPath;
preferences.setString('save_path', path);
preferences.setBool('is_android_scoped', false);
return SavePath.normal(path);
}

Future<String> get _defaultPath async {
final directory = await _directory;
return directory.path;
}
Expand Down
40 changes: 17 additions & 23 deletions lib/screens/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';

import 'package:daily_diary/main.dart';
import 'package:daily_diary/path.dart';

import 'package:file_picker/file_picker.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_storage/shared_storage.dart' as saf;

import 'package:daily_diary/main.dart';
import 'package:daily_diary/path.dart';

class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});

Expand Down Expand Up @@ -340,17 +339,10 @@ class SavePathSetting extends StatefulWidget implements SettingTile {

@override
Future<SavePathSetting> newDefault() async {
await resetSavePath();
savePath = await resetPathToDefault();
return const SavePathSetting();
}

Future<void> resetSavePath() async {
savePath = SavePath.normal(await defaultPath);
final preferences = await SharedPreferences.getInstance();
preferences.setString('save_path', await defaultPath);
preferences.setBool('is_android_scoped', false);
}

@override
State<SavePathSetting> createState() => _SavePathSettingState();
}
Expand All @@ -372,27 +364,19 @@ class _SavePathSettingState extends State<SavePathSetting> {
if (Platform.isAndroid) {
return _askForPathAndroid();
}
// Load SharedPreferences while user is picking a path
final preferencesFuture = SharedPreferences.getInstance();
String? path = await FilePicker.platform.getDirectoryPath();
if (path == null) {
return null;
}

final preferences = await preferencesFuture;
final preferences = await App.preferences;
preferences.setString('save_path', path);
preferences.setBool('is_android_scoped', false);
return SavePath.normal(path);
}

Future<SavePath?> _askForPathAndroid() async {
// Load SharedPreferences while user is picking a path
final preferencesFuture = SharedPreferences.getInstance();
// Remove previous permissions
if (savePath != null && savePath!.uri != null) {
final previousPath = savePath!.uri!;
saf.releasePersistableUriPermission(previousPath);
}
_removePreviousPermissions();

// Ask user for path and permissions
Uri? uri;
Expand All @@ -404,12 +388,22 @@ class _SavePathSettingState extends State<SavePathSetting> {
final asDocumentFile = await uri.toDocumentFile();
Map asMap = asDocumentFile!.toMap();
String asString = json.encode(asMap);
final preferences = await preferencesFuture;
final preferences = await App.preferences;
preferences.setString('save_path', asString);
preferences.setBool('is_android_scoped', true);
return SavePath.android(uri);
}

Future<void> _removePreviousPermissions() async {
SavePath? path = savePath;
if (path == null) return;

Uri? previousPath = path.uri;
if (previousPath == null) return;

await saf.releasePersistableUriPermission(previousPath);
}

@override
Widget build(BuildContext context) {
return Column(
Expand Down

0 comments on commit c963037

Please sign in to comment.