add userID pref and generate it if it isn't set

This commit is contained in:
sneurlax 2023-01-24 15:28:56 -06:00
parent 3b993135da
commit b4c16ec0b0
2 changed files with 26 additions and 0 deletions

View file

@ -317,6 +317,10 @@ class _MaterialAppWithThemeState extends ConsumerState<MaterialAppWithTheme>
break; break;
} }
} }
ref
.read(prefsChangeNotifierProvider)
.userID; // Just reading the ref should set it if it's not already set
} catch (e, s) { } catch (e, s) {
Logger.print("$e $s", normalLength: false); Logger.print("$e $s", normalLength: false);
} }

View file

@ -5,6 +5,7 @@ import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart'; import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
import 'package:stackwallet/utilities/enums/languages_enum.dart'; import 'package:stackwallet/utilities/enums/languages_enum.dart';
import 'package:stackwallet/utilities/enums/sync_type_enum.dart'; import 'package:stackwallet/utilities/enums/sync_type_enum.dart';
import 'package:uuid/uuid.dart';
class Prefs extends ChangeNotifier { class Prefs extends ChangeNotifier {
Prefs._(); Prefs._();
@ -38,6 +39,7 @@ class Prefs extends ChangeNotifier {
_startupWalletId = await _getStartupWalletId(); _startupWalletId = await _getStartupWalletId();
_externalCalls = await _getHasExternalCalls(); _externalCalls = await _getHasExternalCalls();
_familiarity = await _getHasFamiliarity(); _familiarity = await _getHasFamiliarity();
_userID = await _getUserID();
_initialized = true; _initialized = true;
} }
@ -602,4 +604,24 @@ class Prefs extends ChangeNotifier {
} }
return true; return true;
} }
String? _userID;
String? get userID => _userID;
Future<String?> _getUserID() async {
String? userID = await DB.instance
.get<dynamic>(boxName: DB.boxNamePrefs, key: "userID") as String?;
if (userID == null) {
userID = const Uuid().v4();
await saveUserID(userID);
}
return userID;
}
Future<void> saveUserID(String userID) async {
_userID = userID;
await DB.instance
.put<dynamic>(boxName: DB.boxNamePrefs, key: "userID", value: _userID);
// notifyListeners();
}
} }