Skip to content

Commit

Permalink
optimised initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
DattatreyaReddy committed Nov 21, 2024
1 parent 543478c commit 44dd24b
Show file tree
Hide file tree
Showing 29 changed files with 303 additions and 344 deletions.
5 changes: 5 additions & 0 deletions lib/src/common/constants/enums/generic_entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import '../../utils/json_abstract.dart';

abstract class GenericEntity<T> extends JsonAbstract {
T get id;
}
4 changes: 4 additions & 0 deletions lib/src/common/utils/app_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ abstract class AppUtils {
(List<RecordSnapshot<T, Map<String, Object?>>> event) =>
event.map(convertSnapNonNull(constructor)).toList();

static T? Function(JsonObject? value) convertGet<T>(
T Function(JsonObject) fromJson) =>
(value) => value != null ? fromJson(value) : null;

static int currentDay(AsyncValue<DateTime?> dob) {
if (dob.valueOrNull == null) return 0;
return DateTime.now().difference(dob.valueOrNull!).inDays;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/common/utils/custom_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

import 'dart:async';

import '../constants/enums/generic_entity.dart';

typedef ConvertFunction<S, R> = R Function(S);

typedef JsonObject = Map<String, Object?>;

typedef AsyncConvertFunction<S, R> = FutureOr<R> Function(S event);

typedef GenericFromJson<T extends GenericEntity> = T Function(
Map<String, dynamic>);

This file was deleted.

2 changes: 0 additions & 2 deletions lib/src/common/utils/json_abstract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

abstract class JsonAbstract {
Map<String, dynamic> toJson();

JsonAbstract.fromJson(Map<String, dynamic> json);
}
53 changes: 53 additions & 0 deletions lib/src/common/utils/repository/generic_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:tekartik_app_flutter_sembast/setup/sembast_flutter.dart';

import '../../constants/enums/generic_entity.dart';
import '../app_utils.dart';
import '../custom_types.dart';

abstract class GenericRepository<S, T extends GenericEntity<S>> {
final Database database;
final StoreRef<S, JsonObject> store;
final GenericFromJson<T> fromJson;

GenericRepository(this.database, String _storeName, this.fromJson)
: store = StoreRef(_storeName);

Stream<List<T>> watchAll() =>
store.query().onSnapshots(database).map(AppUtils.convertSnaps(fromJson));

Stream<T?> watchById(S id) =>
store.record(id).onSnapshot(database).map(AppUtils.convertSnap(fromJson));

Future<int> getCount([DatabaseClient? dbClient]) =>
wrap(dbClient, (txn) => store.count(txn));

Future<T?> getById(S id, [DatabaseClient? dbClient]) async {
return wrap(
dbClient,
(txn) => store.record(id).get(txn).then(AppUtils.convertGet(fromJson)),
);
}

Future<void> save(T entity, [DatabaseClient? dbClient]) => wrap(
dbClient,
(txn) => store.record(entity.id).put(txn, entity.toJson()),
);

Future<void> saveAll(List<T> entities, [DatabaseClient? dbClient]) async {
if (entities.isEmpty) return;
return wrap(
dbClient,
(txn) => Future.wait([for (final entry in entities) save(entry, txn)]),
);
}

Future<U> wrap<U>(
DatabaseClient? dbClient,
Future<U> Function(DatabaseClient) call,
) async {
if (dbClient == null) {
return await database.transaction((txn) => call(txn));
}
return await call(dbClient);
}
}
6 changes: 3 additions & 3 deletions lib/src/features/home/controller/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ Stream<LinkedHashMap<int, List<DayBox>>?> dayBoxMap(Ref ref) =>

@riverpod
Stream<DayBox?> dayBox(Ref ref, int boxNumber) =>
ref.watch(dayBoxRepositoryProvider).getDayBoxByNumber(boxNumber);
ref.watch(dayBoxRepositoryProvider).watchById(boxNumber);

@riverpod
Stream<WeekBox?> weekBox(Ref ref, int boxNumber) =>
ref.watch(weekBoxRepositoryProvider).getWeekBoxByWeekNumber(boxNumber);
ref.watch(weekBoxRepositoryProvider).watchById(boxNumber);

@riverpod
Stream<YearBox?> yearBox(Ref ref, int boxNumber) =>
ref.watch(yearBoxRepositoryProvider).getYearBoxByYearNumber(boxNumber);
ref.watch(yearBoxRepositoryProvider).watchById(boxNumber);
@riverpod
Stream<LinkedHashMap<int, List<WeekBox>>?> weekBoxMap(Ref ref) =>
ref.watch(weekBoxRepositoryProvider).getWeekBoxListGroupByYearNumber();
Expand Down
6 changes: 3 additions & 3 deletions lib/src/features/home/controller/home_controller.g.dart

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

22 changes: 11 additions & 11 deletions lib/src/features/home/domain/box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

import '../../../common/constants/enums/generic_entity.dart';
import '../../../common/utils/converter/epoch_date_time_converter.dart';
import '../../../common/utils/extensions/custom_extensions.dart';
import '../../../common/utils/json_abstract.dart';
import '../enums/box_status.dart';

part 'box.freezed.dart';
Expand Down Expand Up @@ -42,7 +42,7 @@ enum YearBoxFields {
}

sealed class Box {
int get boxNumber;
int get id;
BoxStatus get boxStatus;
bool get hasLandMark;
String get chatGroupId;
Expand All @@ -51,10 +51,10 @@ sealed class Box {
}

@freezed
class DayBox with _$DayBox implements JsonAbstract, Box {
class DayBox with _$DayBox implements GenericEntity<int>, Box {
const DayBox._();
factory DayBox({
required int boxNumber,
@JsonKey(name: 'boxNumber') required int id,
@EpochDateTimeConverter() required DateTime date,
required BoxStatus boxStatus,
required int weekNumber,
Expand All @@ -70,14 +70,14 @@ class DayBox with _$DayBox implements JsonAbstract, Box {

/// For Chat group name
@override
String get chatGroupId => "DAY_$boxNumber";
String get chatGroupId => "DAY_$id";
}

@freezed
class WeekBox with _$WeekBox implements JsonAbstract, Box {
class WeekBox with _$WeekBox implements GenericEntity<int>, Box {
const WeekBox._();
factory WeekBox({
required int boxNumber,
@JsonKey(name: 'boxNumber') required int id,
@EpochDateTimeConverter() required DateTime startDate,
@EpochDateTimeConverter() required DateTime endDate,
required BoxStatus boxStatus,
Expand All @@ -93,15 +93,15 @@ class WeekBox with _$WeekBox implements JsonAbstract, Box {

/// For Chat group name
@override
String get chatGroupId => "WEEK_$boxNumber";
String get chatGroupId => "WEEK_$id";
}

@freezed
class YearBox with _$YearBox implements JsonAbstract, Box {
class YearBox with _$YearBox implements GenericEntity<int>, Box {
const YearBox._();

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

/// For Chat group name
@override
String get chatGroupId => "YEAR_$boxNumber";
String get chatGroupId => "YEAR_$id";
}
Loading

0 comments on commit 44dd24b

Please sign in to comment.