Added central user credentials management
This commit is contained in:
78
lib/model/accountData.dart
Normal file
78
lib/model/accountData.dart
Normal file
@ -0,0 +1,78 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'accountModel.dart';
|
||||
|
||||
class AccountData {
|
||||
static const _usernameField = "username";
|
||||
static const _passwordField = "password";
|
||||
|
||||
static final AccountData _instance = AccountData._construct();
|
||||
final Future<SharedPreferences> _storage = SharedPreferences.getInstance();
|
||||
Completer<void> _populated = Completer();
|
||||
|
||||
factory AccountData() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
AccountData._construct() {
|
||||
_updateFromStorage();
|
||||
}
|
||||
|
||||
String? _username;
|
||||
String? _password;
|
||||
|
||||
String getUsername() {
|
||||
if(_username == null) throw Exception("Username not initialized");
|
||||
return _username!;
|
||||
}
|
||||
|
||||
String getPassword() {
|
||||
if(_password == null) throw Exception("Password not initialized");
|
||||
return _password!;
|
||||
}
|
||||
|
||||
Future<void> setData(BuildContext context, String username, String password) async {
|
||||
SharedPreferences storage = await _storage;
|
||||
|
||||
storage.setString(_usernameField, username);
|
||||
storage.setString(_passwordField, password);
|
||||
await _updateFromStorage();
|
||||
}
|
||||
|
||||
void removeData(BuildContext context) async {
|
||||
_populated = Completer();
|
||||
Provider.of<AccountModel>(context, listen: false).setState(AccountModelState.loggedOut);
|
||||
|
||||
SharedPreferences storage = await _storage;
|
||||
storage.remove(_usernameField);
|
||||
storage.remove(_passwordField);
|
||||
}
|
||||
|
||||
Future<void> _updateFromStorage() async {
|
||||
SharedPreferences storage = await _storage;
|
||||
await storage.reload();
|
||||
if(storage.containsKey(_usernameField) && storage.containsKey(_passwordField)) {
|
||||
_username = storage.getString(_usernameField);
|
||||
_password = storage.getString(_passwordField);
|
||||
_populated.complete();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> waitForPopulation() async {
|
||||
await _populated.future;
|
||||
return isPopulated();
|
||||
}
|
||||
|
||||
bool isPopulated() {
|
||||
return _username != null && _password != null;
|
||||
}
|
||||
|
||||
String buildHttpAuthString() {
|
||||
if(!isPopulated()) throw Exception("AccountData (e.g. username or password) is not initialized!");
|
||||
return "$_username:$_password";
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
class AccountModel extends ChangeNotifier {
|
||||
bool _isLoggedIn = false;
|
||||
AccountModelState _accountState = AccountModelState.undefined;
|
||||
AccountModelState get state => _accountState;
|
||||
|
||||
bool get isLoggedIn => _isLoggedIn;
|
||||
|
||||
void logout() {
|
||||
_isLoggedIn = false;
|
||||
void setState(AccountModelState state) {
|
||||
_accountState = state;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void login() {
|
||||
_isLoggedIn = true;
|
||||
notifyListeners();
|
||||
}
|
||||
enum AccountModelState {
|
||||
undefined,
|
||||
loggedIn,
|
||||
loggedOut,
|
||||
}
|
Reference in New Issue
Block a user