-
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.
[feature] hive and cache method completed
- Loading branch information
1 parent
014e6bb
commit 6594cf1
Showing
16 changed files
with
543 additions
and
20 deletions.
There are no files selected for viewing
Binary file not shown.
Empty file.
Binary file not shown.
Empty file.
Binary file not shown.
Empty file.
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,72 @@ | ||
import 'package:flutter_clean_app_examples/feature/user/model/user_model.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
abstract class ICacheManager<T> { | ||
final String key; | ||
Box<User>? _box; | ||
|
||
ICacheManager(this.key); | ||
Future<void> init() async { | ||
registerAdapters(); | ||
if (!(_box?.isOpen ?? false)) { | ||
_box = await Hive.openBox(key); | ||
} | ||
} | ||
|
||
void registerAdapters(); | ||
|
||
Future<void> clearAll() async { | ||
await _box?.clear(); | ||
} | ||
|
||
Future<void> addItems(List<T> items); | ||
Future<void> putItems(List<T> items); | ||
|
||
T? getItem(String key); | ||
List<T>? getValues(); | ||
|
||
Future<void> putItem(String key, T item); | ||
Future<void> removeItem(String key); | ||
} | ||
|
||
class UserCacheManager extends ICacheManager<User> { | ||
UserCacheManager(String key) : super(key); | ||
|
||
@override | ||
Future<void> addItems(List<User> items) async { | ||
await _box?.addAll(items); | ||
} | ||
|
||
@override | ||
Future<void> putItems(List<User> items) async { | ||
await _box?.putAll(Map.fromEntries(items.map((e) => MapEntry(e.id, e)))); | ||
} | ||
|
||
@override | ||
User? getItem(String key) { | ||
return _box?.get(key); | ||
} | ||
|
||
@override | ||
Future<void> putItem(String key, User item) async { | ||
await _box?.put(key, item); | ||
} | ||
|
||
@override | ||
Future<void> removeItem(String key) async { | ||
await _box?.delete(key); | ||
} | ||
|
||
@override | ||
List<User>? getValues() { | ||
return _box?.values.toList(); | ||
} | ||
|
||
@override | ||
void registerAdapters() { | ||
if (!Hive.isAdapterRegistered(1)) { | ||
Hive.registerAdapter(UserAdapter()); | ||
Hive.registerAdapter(CompanyAdapter()); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,101 @@ | ||
|
||
|
||
|
||
import 'package:hive_flutter/hive_flutter.dart'; | ||
import 'package:json_annotation/json_annotation.dart'; | ||
part 'user_model.g.dart'; | ||
@HiveType(typeId: 1) | ||
|
||
@JsonSerializable() | ||
class User { | ||
@HiveField(0) | ||
int? id; | ||
@HiveField(1) | ||
String? name; | ||
@HiveField(2) | ||
String? username; | ||
@HiveField(3) | ||
String? email; | ||
Address? address; | ||
@HiveField(4) | ||
String? phone; | ||
@HiveField(5) | ||
String? website; | ||
@HiveField(6) | ||
Company? company; | ||
|
||
User({this.id, this.name, this.username, this.email}); | ||
User( | ||
{this.id, | ||
this.name, | ||
this.username, | ||
this.email, | ||
this.address, | ||
this.phone, | ||
this.website, | ||
this.company}); | ||
|
||
factory User.fromJson(Map<String, dynamic> json) { | ||
return _$UserFromJson(json); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return _$UserToJson(this); | ||
return _$UserToJson(this); | ||
} | ||
} | ||
} | ||
@JsonSerializable() | ||
class Address { | ||
String? street; | ||
String? suite; | ||
String? city; | ||
String? zipcode; | ||
Geo? geo; | ||
|
||
Address({this.street, this.suite, this.city, this.zipcode, this.geo}); | ||
|
||
factory Address.fromJson(Map<String, dynamic> json) { | ||
return _$AddressFromJson(json); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return _$AddressToJson(this); | ||
} | ||
} | ||
@JsonSerializable() | ||
class Geo { | ||
String? lat; | ||
String? lng; | ||
|
||
Geo({this.lat, this.lng}); | ||
|
||
factory Geo.fromJson(Map<String, dynamic> json) { | ||
return _$GeoFromJson(json); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return _$GeoToJson(this); | ||
} | ||
} | ||
@HiveType(typeId: 2) | ||
|
||
@JsonSerializable() | ||
class Company { | ||
@HiveField(0) | ||
|
||
String? name; | ||
@HiveField(1) | ||
|
||
String? catchPhrase; | ||
@HiveField(2) | ||
|
||
String? bs; | ||
|
||
Company({this.name, this.catchPhrase, this.bs}); | ||
|
||
factory Company.fromJson(Map<String, dynamic> json) { | ||
return _$CompanyFromJson(json); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return _$CompanyToJson(this); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.