Skip to content

Commit

Permalink
docs(Services): improve docs for local storage service
Browse files Browse the repository at this point in the history
Signed-off-by: arafaysaleem <a.rafaysaleem@gmail.com>
  • Loading branch information
arafaysaleem committed Jun 15, 2021
1 parent 2b68a3d commit 5569d9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/services/local_storage/prefs_base.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:shared_preferences/shared_preferences.dart';

///Internal class for shared preferences methods
///Base class for shared preferences methods
///This class provides low level preferences methods
class PrefsBase{
///Instance of shared preferences
Expand Down
34 changes: 22 additions & 12 deletions lib/services/local_storage/prefs_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,67 @@ import '../../models/user_model.dart';
//states
import '../../providers/states/auth_state.dart';

/// A service class for providing methods to store and retrieve data from
/// shared preferences.
class PrefsService {
final authTokenKey = "authToken";
final authStateKey = "authStateKey";
final authPasswordKey = "authPasswordKey";
final authUserKey = "authUserKey";

/// The name of auth token key
static const _authTokenKey = "authToken";

/// The name of auth state key
static const _authStateKey = "authStateKey";

/// The name of user password key
static const _authPasswordKey = "authPasswordKey";

/// The name of user model key
static const _authUserKey = "authUserKey";

///Instance of prefs class
final _prefs = PrefsBase.instance;

///Returns logged in user password
String getAuthPassword() {
return _prefs.get<String>(authPasswordKey) ?? '';
return _prefs.get<String>(_authPasswordKey) ?? '';
}

///Returns last authentication status
bool getAuthState() {
return _prefs.get<bool>(authStateKey) ?? false;
return _prefs.get<bool>(_authStateKey) ?? false;
}

///Returns last authenticated user
UserModel? getAuthUser() {
final user = _prefs.get<String>(authUserKey);
final user = _prefs.get<String>(_authUserKey);
if(user == null) return null;
return UserModel.fromJson(jsonDecode(user));
}

///Returns last authentication token
String getAuthToken() {
return _prefs.get<String>(authTokenKey) ?? '';
return _prefs.get<String>(_authTokenKey) ?? '';
}

///Sets the authentication password to this value
void setAuthPassword(String password) {
_prefs.set<String>(authPasswordKey, password);
_prefs.set<String>(_authPasswordKey, password);
}

///Sets the authentication status to this value
void setAuthState(AuthState authState) {
if(authState is AUTHENTICATED) {
_prefs.set<bool>(authStateKey, true);
_prefs.set<bool>(_authStateKey, true);
}
}

///Sets the authenticated user to this value
void setAuthUser(UserModel user) {
_prefs.set<String>(authUserKey, jsonEncode(user.toJson()));
_prefs.set<String>(_authUserKey, jsonEncode(user.toJson()));
}

///Sets the authentication token to this value
void setAuthToken(String token) {
_prefs.set<String>(authTokenKey, token);
_prefs.set<String>(_authTokenKey, token);
}

///Resets the authentication
Expand Down

0 comments on commit 5569d9a

Please sign in to comment.