Skip to content

Commit 44dd24b

Browse files
optimised initial setup
1 parent 543478c commit 44dd24b

29 files changed

+303
-344
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import '../../utils/json_abstract.dart';
2+
3+
abstract class GenericEntity<T> extends JsonAbstract {
4+
T get id;
5+
}

lib/src/common/utils/app_utils.dart

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ abstract class AppUtils {
9191
(List<RecordSnapshot<T, Map<String, Object?>>> event) =>
9292
event.map(convertSnapNonNull(constructor)).toList();
9393

94+
static T? Function(JsonObject? value) convertGet<T>(
95+
T Function(JsonObject) fromJson) =>
96+
(value) => value != null ? fromJson(value) : null;
97+
9498
static int currentDay(AsyncValue<DateTime?> dob) {
9599
if (dob.valueOrNull == null) return 0;
96100
return DateTime.now().difference(dob.valueOrNull!).inDays;

lib/src/common/utils/custom_types.dart

+5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77
import 'dart:async';
88

9+
import '../constants/enums/generic_entity.dart';
10+
911
typedef ConvertFunction<S, R> = R Function(S);
1012

1113
typedef JsonObject = Map<String, Object?>;
1214

1315
typedef AsyncConvertFunction<S, R> = FutureOr<R> Function(S event);
16+
17+
typedef GenericFromJson<T extends GenericEntity> = T Function(
18+
Map<String, dynamic>);

lib/src/common/utils/extensions/custom_extensions/store_ref_extensions.dart

-42
This file was deleted.

lib/src/common/utils/json_abstract.dart

-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
abstract class JsonAbstract {
88
Map<String, dynamic> toJson();
9-
10-
JsonAbstract.fromJson(Map<String, dynamic> json);
119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:tekartik_app_flutter_sembast/setup/sembast_flutter.dart';
2+
3+
import '../../constants/enums/generic_entity.dart';
4+
import '../app_utils.dart';
5+
import '../custom_types.dart';
6+
7+
abstract class GenericRepository<S, T extends GenericEntity<S>> {
8+
final Database database;
9+
final StoreRef<S, JsonObject> store;
10+
final GenericFromJson<T> fromJson;
11+
12+
GenericRepository(this.database, String _storeName, this.fromJson)
13+
: store = StoreRef(_storeName);
14+
15+
Stream<List<T>> watchAll() =>
16+
store.query().onSnapshots(database).map(AppUtils.convertSnaps(fromJson));
17+
18+
Stream<T?> watchById(S id) =>
19+
store.record(id).onSnapshot(database).map(AppUtils.convertSnap(fromJson));
20+
21+
Future<int> getCount([DatabaseClient? dbClient]) =>
22+
wrap(dbClient, (txn) => store.count(txn));
23+
24+
Future<T?> getById(S id, [DatabaseClient? dbClient]) async {
25+
return wrap(
26+
dbClient,
27+
(txn) => store.record(id).get(txn).then(AppUtils.convertGet(fromJson)),
28+
);
29+
}
30+
31+
Future<void> save(T entity, [DatabaseClient? dbClient]) => wrap(
32+
dbClient,
33+
(txn) => store.record(entity.id).put(txn, entity.toJson()),
34+
);
35+
36+
Future<void> saveAll(List<T> entities, [DatabaseClient? dbClient]) async {
37+
if (entities.isEmpty) return;
38+
return wrap(
39+
dbClient,
40+
(txn) => Future.wait([for (final entry in entities) save(entry, txn)]),
41+
);
42+
}
43+
44+
Future<U> wrap<U>(
45+
DatabaseClient? dbClient,
46+
Future<U> Function(DatabaseClient) call,
47+
) async {
48+
if (dbClient == null) {
49+
return await database.transaction((txn) => call(txn));
50+
}
51+
return await call(dbClient);
52+
}
53+
}

lib/src/features/home/controller/home_controller.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Stream<LinkedHashMap<int, List<DayBox>>?> dayBoxMap(Ref ref) =>
2222

2323
@riverpod
2424
Stream<DayBox?> dayBox(Ref ref, int boxNumber) =>
25-
ref.watch(dayBoxRepositoryProvider).getDayBoxByNumber(boxNumber);
25+
ref.watch(dayBoxRepositoryProvider).watchById(boxNumber);
2626

2727
@riverpod
2828
Stream<WeekBox?> weekBox(Ref ref, int boxNumber) =>
29-
ref.watch(weekBoxRepositoryProvider).getWeekBoxByWeekNumber(boxNumber);
29+
ref.watch(weekBoxRepositoryProvider).watchById(boxNumber);
3030

3131
@riverpod
3232
Stream<YearBox?> yearBox(Ref ref, int boxNumber) =>
33-
ref.watch(yearBoxRepositoryProvider).getYearBoxByYearNumber(boxNumber);
33+
ref.watch(yearBoxRepositoryProvider).watchById(boxNumber);
3434
@riverpod
3535
Stream<LinkedHashMap<int, List<WeekBox>>?> weekBoxMap(Ref ref) =>
3636
ref.watch(weekBoxRepositoryProvider).getWeekBoxListGroupByYearNumber();

lib/src/features/home/controller/home_controller.g.dart

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/features/home/domain/box.dart

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import 'package:flutter/material.dart';
88
import 'package:freezed_annotation/freezed_annotation.dart';
99

10+
import '../../../common/constants/enums/generic_entity.dart';
1011
import '../../../common/utils/converter/epoch_date_time_converter.dart';
1112
import '../../../common/utils/extensions/custom_extensions.dart';
12-
import '../../../common/utils/json_abstract.dart';
1313
import '../enums/box_status.dart';
1414

1515
part 'box.freezed.dart';
@@ -42,7 +42,7 @@ enum YearBoxFields {
4242
}
4343

4444
sealed class Box {
45-
int get boxNumber;
45+
int get id;
4646
BoxStatus get boxStatus;
4747
bool get hasLandMark;
4848
String get chatGroupId;
@@ -51,10 +51,10 @@ sealed class Box {
5151
}
5252

5353
@freezed
54-
class DayBox with _$DayBox implements JsonAbstract, Box {
54+
class DayBox with _$DayBox implements GenericEntity<int>, Box {
5555
const DayBox._();
5656
factory DayBox({
57-
required int boxNumber,
57+
@JsonKey(name: 'boxNumber') required int id,
5858
@EpochDateTimeConverter() required DateTime date,
5959
required BoxStatus boxStatus,
6060
required int weekNumber,
@@ -70,14 +70,14 @@ class DayBox with _$DayBox implements JsonAbstract, Box {
7070

7171
/// For Chat group name
7272
@override
73-
String get chatGroupId => "DAY_$boxNumber";
73+
String get chatGroupId => "DAY_$id";
7474
}
7575

7676
@freezed
77-
class WeekBox with _$WeekBox implements JsonAbstract, Box {
77+
class WeekBox with _$WeekBox implements GenericEntity<int>, Box {
7878
const WeekBox._();
7979
factory WeekBox({
80-
required int boxNumber,
80+
@JsonKey(name: 'boxNumber') required int id,
8181
@EpochDateTimeConverter() required DateTime startDate,
8282
@EpochDateTimeConverter() required DateTime endDate,
8383
required BoxStatus boxStatus,
@@ -93,15 +93,15 @@ class WeekBox with _$WeekBox implements JsonAbstract, Box {
9393

9494
/// For Chat group name
9595
@override
96-
String get chatGroupId => "WEEK_$boxNumber";
96+
String get chatGroupId => "WEEK_$id";
9797
}
9898

9999
@freezed
100-
class YearBox with _$YearBox implements JsonAbstract, Box {
100+
class YearBox with _$YearBox implements GenericEntity<int>, Box {
101101
const YearBox._();
102102

103103
factory YearBox({
104-
required int boxNumber,
104+
@JsonKey(name: 'boxNumber') required int id,
105105
@EpochDateTimeConverter() required DateTime startDate,
106106
@EpochDateTimeConverter() required DateTime endDate,
107107
required BoxStatus boxStatus,
@@ -116,5 +116,5 @@ class YearBox with _$YearBox implements JsonAbstract, Box {
116116

117117
/// For Chat group name
118118
@override
119-
String get chatGroupId => "YEAR_$boxNumber";
119+
String get chatGroupId => "YEAR_$id";
120120
}

0 commit comments

Comments
 (0)