Created
December 23, 2024 23:39
-
-
Save arafaysaleem/30b8111de3c5231d1894ad0627468fcc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ignore_for_file: avoid_positional_boolean_parameters | |
import 'dart:convert'; | |
import 'package:hooks_riverpod/hooks_riverpod.dart'; | |
// Services | |
import '../../features/notifications/notifications.dart'; | |
import '../networking/models/token_model.codegen.dart'; | |
import 'key_value_storage_base.dart'; | |
// Helpers | |
import '../../helpers/helpers.dart'; | |
// Features | |
import '../../features/map/map.dart'; | |
import '../../features/profile/profile.dart'; | |
/// A provider used to access instance of this service | |
final keyValueStorageServiceProvider = Provider((ref) { | |
return KeyValueStorageService(); | |
}); | |
/// A service class for providing methods to store and retrieve key-value data | |
/// from common or secure storage. | |
class KeyValueStorageService { | |
/// The name of auth token key | |
static const _authTokenKey = 'authToken'; | |
/// The name of user model key | |
static const _authUserKey = 'authUserKey'; | |
/// The last map data key | |
static const _mapDataKey = 'mapDataKey'; | |
/// The name of notifications key | |
static const _notificationsKey = 'notificationsKey'; | |
/// The name of the first map load key | |
static const _firstMapLoadKey = 'firstMapLoadKey'; | |
/// The name of the stored locale key | |
static const _localeKey = 'localeKey'; | |
/// The name of the stored job alerts area key | |
static const _jobAlertsAreaKey = 'jobAlertsAreaKey'; | |
/// Instance of key-value storage base class | |
static const _keyValueStorage = KeyValueStorageBase.instance; | |
/// Returns last authenticated user | |
UserModel? getAuthUser() { | |
final user = _keyValueStorage.getCommon<String>(_authUserKey); | |
if (user == null) return null; | |
return UserModel.fromJson(jsonDecode(user) as JSON); | |
} | |
/// Returns last authentication token | |
Future<TokenModel?> getAuthToken() async { | |
final token = await _keyValueStorage.getEncrypted(_authTokenKey); | |
if (token == null) return null; | |
return TokenModel.fromJson(jsonDecode(token) as JSON); | |
} | |
/// Returns whether this is the first time the map is being loaded | |
bool isFirstMapLoad() { | |
final firstLoad = _keyValueStorage.getCommon<bool>(_firstMapLoadKey); | |
if (firstLoad == null) return true; | |
return firstLoad; | |
} | |
/// Sets the first map load to this value. Even though this method is | |
/// asynchronous, we don't care about it's completion which is why we don't | |
/// use `await` and let it execute in the background. | |
void setFirstMapLoad(bool isFirstLoad) { | |
_keyValueStorage.setCommon<bool>(_firstMapLoadKey, isFirstLoad); | |
} | |
/// Returns the stored locale | |
String? getLocale() { | |
final locale = _keyValueStorage.getCommon<String>(_localeKey); | |
return locale; | |
} | |
/// Sets the stored locale to this value. Even though this method is | |
/// asynchronous, we don't care about it's completion which is why we don't | |
/// use `await` and let it execute in the background. | |
void setLocale(String locale) { | |
_keyValueStorage.setCommon<String>(_localeKey, locale); | |
} | |
/// Sets the authenticated user to this value. Even though this method is | |
/// asynchronous, we don't care about it's completion which is why we don't | |
/// use `await` and let it execute in the background. | |
void setAuthUser(UserModel user) { | |
_keyValueStorage.setCommon<String>(_authUserKey, jsonEncode(user.toJson())); | |
} | |
/// Sets the authentication token to this value. Even though this method is | |
/// asynchronous, we don't care about it's completion which is why we don't | |
/// use `await` and let it execute in the background. | |
void setAuthToken(TokenModel token) { | |
_keyValueStorage.setEncrypted(_authTokenKey, jsonEncode(token.toJson())); | |
} | |
void clearUserData() { | |
_keyValueStorage | |
..removeCommon(_authUserKey) | |
..removeCommon(_firstMapLoadKey) | |
..removeCommon(_mapDataKey) | |
..removeCommon(_notificationsKey) | |
..removeCommon(_jobAlertsAreaKey) | |
..removeEncrypted(_authTokenKey); | |
} | |
/// Returns the last map data | |
MapDataModel? getMapData() { | |
final json = _keyValueStorage.getCommon<String>(_mapDataKey); | |
if (json == null) return null; | |
return MapDataModel.fromJson(jsonDecode(json)); | |
} | |
/// Sets the last map data. Even though this method is asynchronous, | |
/// we don't care about it's completion which is why we don't use `await` and | |
/// let it execute in the background. | |
void setMapData(MapDataModel mapData) { | |
_keyValueStorage.setCommon(_mapDataKey, jsonEncode(mapData.toJson())); | |
} | |
/// Returns the last job alerts area | |
MapDataModel? getJobAlertsArea() { | |
final json = _keyValueStorage.getCommon<String>(_jobAlertsAreaKey); | |
if (json == null) return null; | |
return MapDataModel.fromJson(jsonDecode(json)); | |
} | |
/// Sets the last job alerts area. Even though this method is asynchronous, | |
/// we don't care about it's completion which is why we don't use `await` and | |
/// let it execute in the background. | |
void setJobAlertsArea(MapDataModel mapData) { | |
_keyValueStorage.setCommon(_jobAlertsAreaKey, jsonEncode(mapData.toJson())); | |
} | |
void clearJobAlertsArea() { | |
_keyValueStorage.removeCommon(_jobAlertsAreaKey); | |
} | |
/// Returns the last notifications | |
List<NotificationModel> getNotifications() { | |
final list = _keyValueStorage.getCommon<List<String>>(_notificationsKey); | |
if (list == null) return []; | |
return list.map((e) => NotificationModel.fromJson(jsonDecode(e))).toList(); | |
} | |
/// Sets the notifications. Even though this method is asynchronous, we don't | |
/// care about it's completion which is why we don't use `await` and let it | |
/// execute in the background. | |
Future<bool> addNotification(NotificationModel notification) async { | |
final json = jsonEncode(notification.toJson()); | |
final jsonList = | |
_keyValueStorage.getCommon<List<String>>(_notificationsKey); | |
return _keyValueStorage.setCommon(_notificationsKey, [...?jsonList, json]); | |
} | |
/// Resets the authentication. Even though these methods are asynchronous, we | |
/// don't care about their completion which is why we don't use `await` and | |
/// let them execute in the background. | |
void resetKeys() { | |
_keyValueStorage | |
..clearCommon() | |
..clearEncrypted(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment