Created
November 22, 2021 13:06
-
-
Save ologunB/9dbd4abb024f4187f00f18c0e3e854b9 to your computer and use it in GitHub Desktop.
This file contains 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
import 'package:hive/hive.dart'; | |
import 'package:hive_flutter/hive_flutter.dart'; | |
import 'package:mms_app/core/models/auth/login_model.dart'; | |
import 'package:mms_app/core/models/auth/register_success_model.dart'; | |
const String kUserBox = 'userBox'; | |
const String profile = 'profile1'; | |
const String isFirst = 'isFirst'; | |
const String useBioKey = 'use-biosdddd'; | |
const String registeredKey = 'registeredKey'; | |
const String personalKey = 'personalKey'; | |
class AppCache { | |
static Future<void> init() async { | |
await Hive.initFlutter(); | |
await Hive.openBox<dynamic>(kUserBox); | |
} | |
static Box<dynamic> get _userBox => Hive.box<dynamic>(kUserBox); | |
static void haveFirstView() { | |
_userBox.put(isFirst, false); | |
} | |
static bool getIsFirst() { | |
final bool data = _userBox.get(isFirst, defaultValue: true); | |
return data; | |
} | |
static void setUseBios(bool key) { | |
_userBox.put(useBioKey, key); | |
} | |
static bool getUseBios() { | |
final bool data = _userBox.get(useBioKey, defaultValue: false); | |
return data; | |
} | |
static void setIsPersonal(bool t) { | |
_userBox.put(personalKey, t); | |
} | |
static bool getIsPersonal() { | |
final bool data = _userBox.get(personalKey, defaultValue: true); | |
return data; | |
} | |
static void saveRegisteredUser(RegisterSuccessModel user) { | |
_userBox.put(registeredKey, user.toJson()); | |
} | |
static RegisterSuccessModel get registeredUser { | |
final dynamic data = _userBox.get(registeredKey); | |
if (data == null) { | |
return null; | |
} | |
return RegisterSuccessModel.fromJson(data); | |
} | |
static void cacheUser(UserData user) { | |
_userBox.put(profile, user.toJson()); | |
} | |
static UserData get getUser { | |
final dynamic data = _userBox.get(profile); | |
if (data == null) { | |
return null; | |
} | |
return UserData.fromJson(data); | |
} | |
static Future<void> clear() async { | |
await _userBox.clear(); | |
} | |
static void clean(String key) { | |
_userBox.delete(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment