Skip to content

Commit

Permalink
[feature] hive and cache method completed
Browse files Browse the repository at this point in the history
  • Loading branch information
cihangirtuncer committed Jun 27, 2022
1 parent 014e6bb commit 6594cf1
Show file tree
Hide file tree
Showing 16 changed files with 543 additions and 20 deletions.
Binary file added database/examples.hive
Binary file not shown.
Empty file added database/examples.lock
Empty file.
Binary file added database/examples2.hive
Binary file not shown.
Empty file added database/examples2.lock
Empty file.
Binary file added database/theme.hive
Binary file not shown.
Empty file added database/theme.lock
Empty file.
72 changes: 72 additions & 0 deletions lib/core/manager/cache/user_cache_manager.dart
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());
}
}
}
84 changes: 81 additions & 3 deletions lib/feature/user/model/user_model.dart
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);
}
}
148 changes: 148 additions & 0 deletions lib/feature/user/model/user_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6594cf1

Please sign in to comment.