Skip to content

Commit

Permalink
fix(macos,ios): use regular shared prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingkor Roy Tirtho committed Apr 27, 2023
1 parent f1f60b9 commit 1b5bfec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
32 changes: 25 additions & 7 deletions lib/utils/persisted_state_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive/hive.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/utils/platform.dart';
import 'package:spotube/utils/primitive_utils.dart';

const secureStorage = FlutterSecureStorage(
Expand Down Expand Up @@ -32,22 +34,38 @@ abstract class PersistedStateNotifier<T> extends StateNotifier<T> {
static late LazyBox _box;
static late LazyBox _encryptedBox;

static Future<String?> read(String key) async {
final localStorage = await SharedPreferences.getInstance();
if (kIsMacOS || kIsIOS) {
return localStorage.getString(key);
} else {
return secureStorage.read(key: key);
}
}

static Future<void> write(String key, String value) async {
final localStorage = await SharedPreferences.getInstance();
if (kIsMacOS || kIsIOS) {
await localStorage.setString(key, value);
return;
} else {
return secureStorage.write(key: key, value: value);
}
}

static Future<void> initializeBoxes() async {
String? boxName = await secureStorage.read(key: kKeyBoxName);
String? boxName = await read(kKeyBoxName);

if (boxName == null) {
boxName = "spotube-${PrimitiveUtils.uuid.v4()}";
await secureStorage.write(key: kKeyBoxName, value: boxName);
await write(kKeyBoxName, boxName);
}

String? encryptionKey = await secureStorage.read(key: getBoxKey(boxName));
String? encryptionKey = await read(getBoxKey(boxName));

if (encryptionKey == null) {
encryptionKey = base64Url.encode(Hive.generateSecureKey());
await secureStorage.write(
key: getBoxKey(boxName),
value: encryptionKey,
);
await write(getBoxKey(boxName), encryptionKey);
}

_encryptedBox = await Hive.openLazyBox(
Expand Down
5 changes: 3 additions & 2 deletions macos/Runner/DebugProfile.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<true />
<key>com.apple.security.network.server</key>
<true />
<key>keychain-access-groups</key>
<array />
<!-- Requires Certification -->
<!-- <key>keychain-access-groups</key>
<array /> -->
</dict>
</plist>
5 changes: 3 additions & 2 deletions macos/Runner/Release.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<true />
<key>com.apple.security.network.server</key>
<true />
<key>keychain-access-groups</key>
<array />
<!-- Requires Certification -->
<!-- <key>keychain-access-groups</key>
<array /> -->
</dict>
</plist>

0 comments on commit 1b5bfec

Please sign in to comment.