Skip to content

Commit

Permalink
finish changing read module architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
aidaiym committed Mar 12, 2024
1 parent 3ee23b1 commit c7e9101
Show file tree
Hide file tree
Showing 45 changed files with 263 additions and 176 deletions.
2 changes: 1 addition & 1 deletion app/lib/modules/home/data/data.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export 'model/home_model_response.dart';
export 'repository/home_repository.dart';
export 'repository/home_repository_impl.dart';
export 'source/home_local_data_source.dart';
export 'source/home_remote_data_source.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HomeRepositoryImpl implements HomeRepository {
final HomeRemoteDataSource remoteDataSource;

@override
Future<HomeModel> getData(String token) async {
Future<HomeEntity> getData(String token) async {
try {
final remoteData = await remoteDataSource.getRemoteData(token);
await localDataSource.saveLocalData(remoteData);
Expand All @@ -23,8 +23,8 @@ class HomeRepositoryImpl implements HomeRepository {
}
}

HomeModel _convertData(HomeModelResponse response) {
return HomeModel(
HomeEntity _convertData(HomeModelResponse response) {
return HomeEntity(
allDoneHatims: response.allDoneHatims,
allDonePages: response.allDonePages,
donePages: response.donePages,
Expand Down
2 changes: 1 addition & 1 deletion app/lib/modules/home/domain/domain.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export 'entity/home_model.dart';
export 'entity/home_entity.dart';
export 'repository/home_repository.dart';
export 'usecase/get_data_usecase.dart';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class HomeModel {
const HomeModel({
class HomeEntity {
const HomeEntity({
required this.allDoneHatims,
required this.allDonePages,
required this.donePages,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:my_quran/modules/modules.dart';

abstract class HomeRepository {
Future<HomeModel> getData(String token);
Future<HomeEntity> getData(String token);
}
2 changes: 1 addition & 1 deletion app/lib/modules/home/domain/usecase/get_data_usecase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class GetHomeDataUseCase {

final HomeRepository repository;

Future<HomeModel> execute(String token) {
Future<HomeEntity> execute(String token) {
return repository.getData(token);
}
}
2 changes: 1 addition & 1 deletion app/lib/modules/home/presentation/cubit/home_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HomeState extends Equatable {
this.homeModel,
});

final HomeModel? homeModel;
final HomeEntity? homeModel;
final FetchStatus status;

@override
Expand Down
9 changes: 7 additions & 2 deletions app/lib/modules/read/data/data.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export 'repository/read_repository_impl.dart';
export 'repository/read_theme_repository_impl.dart';
export 'source/source.dart';
export 'model/quran_page.dart';
export 'source/local_theme_data_source.dart';
export 'source/read_local_data_source.dart';
export 'source/read_remote_data_source.dart';
export 'model/filters_response.dart';
export 'model/meta_response.dart';
export 'model/quran_page_response.dart';
export 'model/verse_response.dart';
16 changes: 16 additions & 0 deletions app/lib/modules/read/data/model/filters_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';

part 'filters_response.g.dart';

@immutable
@JsonSerializable()
final class FiltersResponse {
const FiltersResponse({required this.pageNumber});

factory FiltersResponse.fromJson(Map<String, dynamic> json) => _$FiltersResponseFromJson(json);
Map<String, dynamic> toJson() => _$FiltersResponseToJson(this);

@JsonKey(name: 'page_number')
final String pageNumber;
}

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

16 changes: 16 additions & 0 deletions app/lib/modules/read/data/model/meta_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:my_quran/modules/modules.dart';

part 'meta_response.g.dart';

@immutable
@JsonSerializable()
final class MetaResponse {
const MetaResponse({required this.filters});

factory MetaResponse.fromJson(Map<String, dynamic> json) => _$MetaResponseFromJson(json);
Map<String, dynamic> toJson() => _$MetaResponseToJson(this);

final FiltersResponse filters;
}

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

20 changes: 20 additions & 0 deletions app/lib/modules/read/data/model/quran_page_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:my_quran/modules/modules.dart';

part 'quran_page_response.g.dart';

@immutable
@JsonSerializable()
final class QuranPageResponse {
const QuranPageResponse({
required this.verses,
required this.meta,
});

factory QuranPageResponse.fromJson(Map<String, dynamic> json) => _$QuranPageResponseFromJson(json);
Map<String, dynamic> toJson() => _$QuranPageResponseToJson(this);

final List<VerseResponse> verses;
final MetaResponse meta;
}

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

23 changes: 23 additions & 0 deletions app/lib/modules/read/data/model/verse_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';

part 'verse_response.g.dart';

@immutable
@JsonSerializable()
final class VerseResponse {
const VerseResponse({
required this.id,
required this.verseKey,
required this.textUthmani,
});

factory VerseResponse.fromJson(Map<String, dynamic> json) => _$VerseResponseFromJson(json);
Map<String, dynamic> toJson() => _$VerseResponseToJson(this);

final int id;
@JsonKey(name: 'verse_key')
final String verseKey;
@JsonKey(name: 'text_imlaei')
final String textUthmani;
}

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

50 changes: 32 additions & 18 deletions app/lib/modules/read/data/repository/read_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import 'dart:developer';

import 'package:meta/meta.dart';
import 'package:my_quran/modules/modules.dart';

class ReadRepositoryImpl implements ReadRepository {
const ReadRepositoryImpl(this.remoteDataSource, this.localDataSource);
@immutable
final class ReadRepositoryImpl implements ReadRepository {
const ReadRepositoryImpl(
this.remoteDataSource,
this.localDataSource,
);

final ReadRemoteDataSource remoteDataSource;
final ReadLocalDataSource localDataSource;

@override
Future<QuranPage?> getPage(int page, String quranFmt) async {
try {
final localPage = await localDataSource.getPage(page, quranFmt);
final remotePage = await remoteDataSource.fetchPage(page, quranFmt);
if (remotePage != null) {
await localDataSource.cachePage(page, quranFmt, remotePage);
return remotePage;
}
if (localPage != null) {
return localPage;
}
} catch (e) {
log('Error fetching page locally and remotely: $e');
Future<QuranPageEntity?> getPage(int page, String quranFmt) async {
final cachedData = localDataSource.getPage(page, quranFmt);
if (cachedData != null) return _convertData(cachedData);

final data = await remoteDataSource.fetchPage(page, quranFmt);
if (data != null) {
await localDataSource.cachePage(page, quranFmt, data);
return _convertData(data);
} else {
return null;
}
return null;
}

QuranPageEntity _convertData(QuranPageResponse response) {
return QuranPageEntity(
meta: MetaEntity(FiltersEnity(response.meta.filters.pageNumber)),
verses: response.verses
.map(
(e) => VerseEnity(
id: e.id,
verseKey: e.verseKey,
textUthmani: e.textUthmani,
),
)
.toList(),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import 'package:meta/meta.dart';
import 'package:my_quran/modules/modules.dart';

class ReadThemeRepositoryImpl implements ReadThemeRepository {
const ReadThemeRepositoryImpl({required this.localDataSource});
final LocalThemeDataSource localDataSource;
@immutable
final class ReadThemeRepositoryImpl implements ReadThemeRepository {
const ReadThemeRepositoryImpl(this.localThemeDataSource);

final LocalThemeDataSource localThemeDataSource;

@override
Future<ReadThemeState> getInitialThemeState() async {
return localDataSource.getInitialThemeState();
ReadThemeState get getInitialThemeState {
return localThemeDataSource.initialTheme;
}

@override
Future<void> saveThemeState(ReadThemeState themeState) async {
return localDataSource.saveThemeState(themeState);
return localThemeDataSource.saveThemeState(themeState);
}
}
10 changes: 6 additions & 4 deletions app/lib/modules/read/data/source/local_theme_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/modules/modules.dart';
import 'dart:convert';
import 'package:meta/meta.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/modules/modules.dart';

class LocalThemeDataSource {
@immutable
final class LocalThemeDataSource {
const LocalThemeDataSource(this.storage);

final PreferencesStorage storage;

ReadThemeState getInitialThemeState() {
ReadThemeState get initialTheme {
final value = storage.readString(key: AppConst.readThemeKey);
return value != null ? ReadThemeState.fromJson(json.decode(value) as Map<String, dynamic>) : const ReadThemeState();
}
Expand Down
11 changes: 6 additions & 5 deletions app/lib/modules/read/data/source/read_local_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import 'dart:convert';

import 'package:meta/meta.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/modules/modules.dart';

class ReadLocalDataSource {
@immutable
final class ReadLocalDataSource {
const ReadLocalDataSource(this.storage);

final PreferencesStorage storage;

Future<QuranPage?> getPage(int page, String quranFmt) async {
QuranPageResponse? getPage(int page, String quranFmt) {
final key = 'quran-$quranFmt-$page';
final localValue = storage.readString(key: key);

if (localValue != null) {
final data = jsonDecode(localValue);
return QuranPage.fromJson(data as Map<String, dynamic>);
return QuranPageResponse.fromJson(data as Map<String, dynamic>);
}
return null;
}

Future<void> cachePage(int page, String quranFmt, QuranPage pageData) async {
Future<void> cachePage(int page, String quranFmt, QuranPageResponse pageData) async {
final key = 'quran-$quranFmt-$page';
await storage.writeString(key: key, value: jsonEncode(pageData.toJson()));
}
Expand Down
Loading

0 comments on commit c7e9101

Please sign in to comment.