-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from E-MRE/feature/locale-caching-hive
LocaleCachingService created using Hive.
- Loading branch information
Showing
11 changed files
with
320 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// ignore_for_file: avoid_print | ||
|
||
import 'package:guki_core/guki_core.dart'; | ||
|
||
class HiveCachingManagerExamples { | ||
// HiveCachingManager implements HiveService. | ||
// HiveService implements LocaleCachingService | ||
// Which mean you can use hive spesific functions or locale caching functions | ||
|
||
///[_cachingService] can use only [LocaleCachingService] methods. | ||
final LocaleCachingService _cachingService = HiveCachingManager.instance; | ||
|
||
///[_hiveService] can use [LocaleCachingService] methods and [HiveService] spesific methods. | ||
final HiveService _hiveService = HiveCachingManager.instance; | ||
|
||
Future<void> examples() async { | ||
//Init operation important for Hive. | ||
//You have to call it one time. | ||
await _cachingService.init(); | ||
|
||
var isSaved = await _cachingService.setValue<String>('Test', 'My data'); | ||
print(isSaved.toString()); | ||
|
||
var myDataCache = | ||
await _cachingService.getValue<String>('Test', defaultValue: ''); | ||
var myDataHive = | ||
await _hiveService.getValue<String>('Test', defaultValue: ''); | ||
|
||
print(myDataCache?.toString() ?? 'error cache'); | ||
print(myDataHive?.toString() ?? 'error hive'); | ||
|
||
var isOpen = _hiveService.isBoxOpen('Test'); | ||
if (isOpen) { | ||
await _hiveService.close<String>('Test'); | ||
} | ||
|
||
var isDeleted = await _cachingService.deleteValue('Test'); | ||
print('Is Deleted: $isDeleted'); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:guki_core/src/caching/abstract/locale_caching_service.dart'; | ||
import 'package:guki_core/src/models/hive_registration_model.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
abstract class HiveService implements LocaleCachingService { | ||
@override | ||
Future<void> init({ | ||
VoidCallback? customInit, | ||
List<HiveRegistrationModel>? adapters, | ||
}); | ||
|
||
///Register your adapter to hive. | ||
void registerAdapter<T>( | ||
TypeAdapter<T> adapter, { | ||
bool internal = false, | ||
bool override = false, | ||
int? typeId, | ||
}); | ||
|
||
void registerAdapterList({List<HiveRegistrationModel>? adapters}); | ||
|
||
Future<void> close<T>(String key); | ||
|
||
///Check your adapter is registered. | ||
bool isAdapterRegistered(int typeId); | ||
|
||
///Checks selected box is open. | ||
bool isBoxOpen(String key); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
|
||
///Save your data to the cache. Active CacheServices: | ||
/// | ||
/// * [HiveCachingManager] | ||
abstract class LocaleCachingService { | ||
///Initialize your caching package. | ||
///Also add your initialize operations using [customInit]. | ||
/// | ||
///Example: | ||
/// | ||
///```dart | ||
///void main(){ | ||
/// await _cachingService.init(customInit: () => myInit()); | ||
///} | ||
///``` | ||
Future<void> init({VoidCallback? customInit}); | ||
|
||
///Get value using [String] key and also you can set default value. | ||
///You should define value type | ||
/// | ||
///Example: | ||
/// | ||
///```dart | ||
///var myData = await getValue<String>('token', '-'); | ||
/// | ||
///if((myData?.isEmpty ?? true) || myData == -){ | ||
/// return false; | ||
///} | ||
///``` | ||
Future<T?> getValue<T>(String key, {T? defaultValue}); | ||
|
||
///Set value with [key] and [data]. | ||
///You should set data type. | ||
/// | ||
///Example: | ||
/// | ||
///```dart | ||
///var isSaved = await setValue<String>('token', 'Qwewss123dasdasw_swws'); | ||
/// | ||
///if(!isSaved){ | ||
/// print('Data didn't saved to the cache'); | ||
///} | ||
///``` | ||
Future<bool> setValue<T>(String key, T data); | ||
|
||
///Delete cached data using [key]. | ||
///If operation is successful then returns true if not returns false. | ||
/// | ||
///Example: | ||
/// | ||
///```dart | ||
///var isDeleted = await deleteValue('token'); | ||
/// | ||
///if(!isDeleted){ | ||
/// print('An error occured while delete value on cache'); | ||
///} | ||
///``` | ||
Future<bool> deleteValue<T>(String key); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:guki_core/src/caching/abstract/hive_service.dart'; | ||
import 'package:guki_core/src/models/hive_registration_model.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
class HiveCachingManager implements HiveService { | ||
static HiveCachingManager? _instance; | ||
|
||
static HiveCachingManager get instance { | ||
_instance ??= HiveCachingManager._init(); | ||
return _instance!; | ||
} | ||
|
||
final String _key = 'Key'; | ||
|
||
HiveCachingManager._init(); | ||
|
||
@override | ||
Future<void> init({ | ||
VoidCallback? customInit, | ||
List<HiveRegistrationModel>? adapters, | ||
}) async { | ||
try { | ||
await Hive.initFlutter('document'); | ||
if (adapters?.isNotEmpty ?? false) { | ||
registerAdapterList(adapters: adapters); | ||
} | ||
|
||
customInit?.call(); | ||
} catch (error) { | ||
return; | ||
} | ||
} | ||
|
||
@override | ||
Future<T?> getValue<T>(String key, {T? defaultValue}) async { | ||
try { | ||
var box = await _openBox<T>(key); | ||
return box.get(_generateKey(key), defaultValue: defaultValue); | ||
} catch (error) { | ||
return defaultValue; | ||
} | ||
} | ||
|
||
@override | ||
Future<bool> setValue<T>(String key, T data) async { | ||
try { | ||
var box = await _openBox<T>(key); | ||
await box.put(_generateKey(key), data); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
@override | ||
Future<bool> deleteValue<T>(String key) async { | ||
try { | ||
var box = await _openBox<T>(key); | ||
box.delete(_generateKey(key)); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
@override | ||
Future<void> close<T>(String key) async { | ||
try { | ||
var box = await _openBox<T>(key); | ||
box.close(); | ||
} catch (error) { | ||
return; | ||
} | ||
} | ||
|
||
@override | ||
void registerAdapter<T>( | ||
TypeAdapter<T> adapter, { | ||
bool internal = false, | ||
bool override = false, | ||
int? typeId, | ||
}) { | ||
bool isRegistered = false; | ||
if (typeId != null) { | ||
isRegistered = isAdapterRegistered(typeId); | ||
} | ||
|
||
if (!isRegistered) { | ||
Hive.registerAdapter<T>(adapter, internal: internal, override: override); | ||
} | ||
} | ||
|
||
@override | ||
void registerAdapterList({List<HiveRegistrationModel>? adapters}) { | ||
adapters?.map((item) => registerAdapter( | ||
item.adapter, | ||
internal: item.internal, | ||
override: item.override, | ||
typeId: item.typeId, | ||
)); | ||
} | ||
|
||
@override | ||
bool isAdapterRegistered(int typeId) { | ||
return Hive.isAdapterRegistered(typeId); | ||
} | ||
|
||
@override | ||
bool isBoxOpen(String key) { | ||
return Hive.isBoxOpen(_generateKey(key)); | ||
} | ||
|
||
Future<Box<T>> _openBox<T>(String key) => Hive.openBox<T>(_generateKey(key)); | ||
|
||
String _generateKey(String key) => '$key$_key'; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export './abstract/hive_service.dart'; | ||
export './abstract/locale_caching_service.dart'; | ||
export './concrete/hive_caching_manager.dart'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:hive_flutter/adapters.dart'; | ||
|
||
class HiveRegistrationModel<T> { | ||
TypeAdapter<T> adapter; | ||
int typeId; | ||
bool internal; | ||
bool override; | ||
|
||
HiveRegistrationModel({ | ||
required this.adapter, | ||
required this.typeId, | ||
this.internal = false, | ||
this.override = false, | ||
}); | ||
} |
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