|
1 | | -// import 'package:hive/hive.dart'; |
2 | | -// import 'package:lam7a/core/hive_types.dart'; |
| 1 | +import 'package:hive/hive.dart'; |
| 2 | +import 'package:lam7a/core/models/user_model.dart'; |
3 | 3 |
|
4 | | -// class RecentSearchesService { |
5 | | -// static const String _key = 'items'; |
| 4 | +class UserModelAdapter extends TypeAdapter<UserModel> { |
| 5 | + @override |
| 6 | + final int typeId = 3; |
6 | 7 |
|
7 | | -// Future<Box> _box() async { |
8 | | -// return HiveTypes().openBoxIfNeeded(HiveTypes.recentSearchesBox); |
9 | | -// } |
| 8 | + @override |
| 9 | + UserModel read(BinaryReader reader) { |
| 10 | + // id (int?) |
| 11 | + final isIdNull = reader.readBool(); |
| 12 | + final int? id = isIdNull ? null : reader.readInt(); |
10 | 13 |
|
11 | | -// Future<List<String>> getSearches() async { |
12 | | -// final box = await _box(); |
13 | | -// return List<String>.from(box.get(_key, defaultValue: [])); |
14 | | -// } |
| 14 | + // name (String?) |
| 15 | + final isNameNull = reader.readBool(); |
| 16 | + final String? name = isNameNull ? null : reader.readString(); |
15 | 17 |
|
16 | | -// Future<void> addSearch(String query) async { |
17 | | -// final box = await _box(); |
18 | | -// final list = List<String>.from(box.get(_key, defaultValue: [])); |
| 18 | + // username (String?) |
| 19 | + final isUsernameNull = reader.readBool(); |
| 20 | + final String? username = isUsernameNull ? null : reader.readString(); |
19 | 21 |
|
20 | | -// if (list.contains(query)) list.remove(query); |
21 | | -// list.insert(0, query); |
| 22 | + // profileImageUrl (String?) |
| 23 | + final isProfileUrlNull = reader.readBool(); |
| 24 | + final String? profileImageUrl = isProfileUrlNull |
| 25 | + ? null |
| 26 | + : reader.readString(); |
22 | 27 |
|
23 | | -// await box.put(_key, list.take(20).toList()); // keep 20 max |
24 | | -// } |
| 28 | + return UserModel( |
| 29 | + id: id, |
| 30 | + name: name, |
| 31 | + username: username, |
| 32 | + profileImageUrl: profileImageUrl, |
| 33 | + ); |
| 34 | + } |
25 | 35 |
|
26 | | -// Future<void> clear() async { |
27 | | -// final box = await _box(); |
28 | | -// await box.delete(_key); |
29 | | -// } |
30 | | -// } |
| 36 | + @override |
| 37 | + void write(BinaryWriter writer, UserModel obj) { |
| 38 | + // id |
| 39 | + if (obj.id == null) { |
| 40 | + writer.writeBool(true); |
| 41 | + } else { |
| 42 | + writer.writeBool(false); |
| 43 | + writer.writeInt(obj.id!); |
| 44 | + } |
31 | 45 |
|
32 | | -// class RecentProfilesService { |
33 | | -// static const String _key = 'profiles'; |
| 46 | + // name |
| 47 | + if (obj.name == null) { |
| 48 | + writer.writeBool(true); |
| 49 | + } else { |
| 50 | + writer.writeBool(false); |
| 51 | + writer.writeString(obj.name!); |
| 52 | + } |
34 | 53 |
|
35 | | -// Future<Box> _box() async { |
36 | | -// return HiveTypes().openBoxIfNeeded(HiveTypes.recentProfilesBox); |
37 | | -// } |
| 54 | + // username |
| 55 | + if (obj.username == null) { |
| 56 | + writer.writeBool(true); |
| 57 | + } else { |
| 58 | + writer.writeBool(false); |
| 59 | + writer.writeString(obj.username!); |
| 60 | + } |
38 | 61 |
|
39 | | -// Future<List<String>> getProfiles() async { |
40 | | -// final box = await _box(); |
41 | | -// return List<String>.from(box.get(_key, defaultValue: [])); |
42 | | -// } |
43 | | - |
44 | | -// Future<void> addProfile(String userId) async { |
45 | | -// final box = await _box(); |
46 | | -// final list = List<String>.from(box.get(_key, defaultValue: [])); |
47 | | - |
48 | | -// if (list.contains(userId)) list.remove(userId); |
49 | | -// list.insert(0, userId); |
50 | | - |
51 | | -// await box.put(_key, list.take(20).toList()); |
52 | | -// } |
53 | | - |
54 | | -// Future<void> clear() async { |
55 | | -// final box = await _box(); |
56 | | -// await box.delete(_key); |
57 | | -// } |
58 | | -// } |
| 62 | + // profileImageUrl |
| 63 | + if (obj.profileImageUrl == null) { |
| 64 | + writer.writeBool(true); |
| 65 | + } else { |
| 66 | + writer.writeBool(false); |
| 67 | + writer.writeString(obj.profileImageUrl!); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments