Skip to content

Commit

Permalink
change read architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
aidaiym committed Mar 4, 2024
1 parent 190062a commit 3ee23b1
Show file tree
Hide file tree
Showing 34 changed files with 212 additions and 83 deletions.
4 changes: 0 additions & 4 deletions app/lib/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ export 'hatim/hatim_read_model.dart';
export 'juz/juz_data.dart';
export 'juz/juz_model.dart';
export 'juz/juz_surah_model.dart';
export 'quran_page/filters.dart';
export 'quran_page/meta.dart';
export 'quran_page/quran_page.dart';
export 'quran_page/verse.dart';
export 'surah/surah_data.dart';
export 'surah/surah_model.dart';
export 'user/user_model.dart';
4 changes: 4 additions & 0 deletions app/lib/modules/read/data/data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'repository/read_repository_impl.dart';
export 'repository/read_theme_repository_impl.dart';
export 'source/source.dart';
export 'model/quran_page.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';

import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/models/models.dart';

import 'package:my_quran/modules/modules.dart';
part 'quran_page.g.dart';

@immutable
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions app/lib/modules/read/data/repository/read_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dart:developer';

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

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');
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:my_quran/modules/modules.dart';

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

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

@override
Future<void> saveThemeState(ReadThemeState themeState) async {
return localDataSource.saveThemeState(themeState);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/modules/modules.dart';
import 'dart:convert';

import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/modules/read/logic/read_theme_cubit.dart';

class ReadThemeService {
const ReadThemeService(this.storage);
class LocalThemeDataSource {
const LocalThemeDataSource(this.storage);

final PreferencesStorage storage;

ReadThemeState get init {
ReadThemeState getInitialThemeState() {
final value = storage.readString(key: AppConst.readThemeKey);
return value != null ? ReadThemeState.fromJson(json.decode(value) as Map<String, dynamic>) : const ReadThemeState();
}

Future<void> saveChanges(ReadThemeState theme) async {
final value = json.encode(theme.toJson());
Future<void> saveThemeState(ReadThemeState themeState) async {
final value = json.encode(themeState.toJson());
await storage.writeString(key: AppConst.readThemeKey, value: value);
}
}
26 changes: 26 additions & 0 deletions app/lib/modules/read/data/source/read_local_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:convert';

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

class ReadLocalDataSource {
const ReadLocalDataSource(this.storage);

final PreferencesStorage storage;

Future<QuranPage?> getPage(int page, String quranFmt) async {
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 null;
}

Future<void> cachePage(int page, String quranFmt, QuranPage pageData) async {
final key = 'quran-$quranFmt-$page';
await storage.writeString(key: key, value: jsonEncode(pageData.toJson()));
}
}
21 changes: 21 additions & 0 deletions app/lib/modules/read/data/source/read_remote_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';

class ReadRemoteDataSource {
const ReadRemoteDataSource(this.remoteClient);

final RemoteClient remoteClient;

Future<QuranPage?> fetchPage(int page, String quranFmt) async {
final remoteValue = await remoteClient.get<QuranPage>(
apiConst.verse(page, quranFmt),
fromJson: QuranPage.fromJson,
);

return remoteValue.fold(
(l) => throw Exception('Failed to fetch remote data $l'),
(r) => r,
);
}
}
3 changes: 3 additions & 0 deletions app/lib/modules/read/data/source/source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'read_local_data_source.dart';
export 'read_remote_data_source.dart';
export 'local_theme_data_source.dart';
4 changes: 4 additions & 0 deletions app/lib/modules/read/domain/domain.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'repository/read_repository.dart';
export 'repository/read_theme_repository.dart';
export 'usecase/usecase.dart';
export 'entity/entity.dart';
3 changes: 3 additions & 0 deletions app/lib/modules/read/domain/entity/entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'filters.dart';
export 'meta.dart';
export 'verse.dart';
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';

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

part 'meta.g.dart';

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions app/lib/modules/read/domain/repository/read_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:my_quran/modules/modules.dart';

abstract class ReadRepository {
Future<QuranPage?> getPage(int page, String quranFmt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:my_quran/modules/modules.dart';

abstract class ReadThemeRepository {
Future<ReadThemeState> getInitialThemeState();
Future<void> saveThemeState(ReadThemeState themeState);
}
10 changes: 10 additions & 0 deletions app/lib/modules/read/domain/usecase/get_initial_theme_usecase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:my_quran/modules/modules.dart';

class GetInitialThemeUseCase {
const GetInitialThemeUseCase(this.repository);
final ReadThemeRepository repository;

Future<ReadThemeState> execute() {
return repository.getInitialThemeState();
}
}
11 changes: 11 additions & 0 deletions app/lib/modules/read/domain/usecase/get_read_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:my_quran/modules/modules.dart';

class GetReadPageUseCase {
const GetReadPageUseCase(this.repository);

final ReadRepository repository;

Future<QuranPage?> getPage(int page, String quranFmt) {
return repository.getPage(page, quranFmt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:my_quran/modules/modules.dart';

class SaveThemeChangesUseCase {
const SaveThemeChangesUseCase(this.repository);
final ReadThemeRepository repository;

Future<void> execute(ReadThemeState themeState) {
return repository.saveThemeState(themeState);
}
}
3 changes: 3 additions & 0 deletions app/lib/modules/read/domain/usecase/usecase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'get_read_page.dart';
export 'get_initial_theme_usecase.dart';
export 'save_theme_changes_usecase.dart';
18 changes: 0 additions & 18 deletions app/lib/modules/read/logic/read_cubit.dart

This file was deleted.

15 changes: 15 additions & 0 deletions app/lib/modules/read/presentation/logic/read_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:my_quran/modules/modules.dart';

part 'read_state.dart';

class ReadCubit extends Cubit<ReadState> {
ReadCubit(this.getQuranPageUseCase) : super(const ReadState());

final GetReadPageUseCase getQuranPageUseCase;

Future<QuranPage?> fetchQuranPage(int page) async {
return getQuranPageUseCase.getPage(page, state.fmt.name);
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

part 'read_theme_state.dart';

class ReadThemeCubit extends Cubit<ReadThemeState> {
ReadThemeCubit(this.service) : super(service.init);
ReadThemeCubit({required this.getInitialThemeUseCase, required this.saveThemeChangesUseCase})
: super(const ReadThemeState());

final GetInitialThemeUseCase getInitialThemeUseCase;
final SaveThemeChangesUseCase saveThemeChangesUseCase;

final ReadThemeService service;
Future<void> initializeTheme() async {
final themeState = await getInitialThemeUseCase.execute();
emit(themeState);
}

void changeMode(int index) {
emit(state.copyWith(modeIndex: index));
Expand All @@ -27,6 +33,6 @@ class ReadThemeCubit extends Cubit<ReadThemeState> {
}

Future<void> saveChanges() async {
await service.saveChanges(state);
await saveThemeChangesUseCase.execute(state);
}
}
4 changes: 4 additions & 0 deletions app/lib/modules/read/presentation/presentation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'logic/read_cubit.dart';
export 'logic/read_theme_cubit.dart';
export 'view/read_view.dart';
export 'view/pagination_view.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class _PaginationViewState extends State<PaginationView> {
Future<void> _fetchPage(int i) async {
try {
final page = widget.pages[_index];
final newItems = await context.read<ReadCubit>().getPage(page);
final newItems = await context.read<ReadCubit>().fetchQuranPage(page);
_index++;
if (page == widget.pages.last && newItems != null) {
_pagingController.appendLastPage([newItems]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@ class ReadView extends StatelessWidget {
providers: [
BlocProvider(
create: (context) => ReadCubit(
ReadService(context.read<RemoteClient>(), context.read<PreferencesStorage>()),
pages,
GetReadPageUseCase(
ReadRepositoryImpl(
ReadRemoteDataSource(context.read<RemoteClient>()),
ReadLocalDataSource(context.read<PreferencesStorage>()),
),
),
),
),
BlocProvider(
create: (context) => ReadThemeCubit(
getInitialThemeUseCase: GetInitialThemeUseCase(
ReadThemeRepositoryImpl(
localDataSource: LocalThemeDataSource(context.read<PreferencesStorage>()),
),
),
saveThemeChangesUseCase: SaveThemeChangesUseCase(
ReadThemeRepositoryImpl(
localDataSource: LocalThemeDataSource(context.read<PreferencesStorage>()),
),
),
),
),
BlocProvider(create: (context) => ReadThemeCubit(ReadThemeService(context.read<PreferencesStorage>()))),
],
child: ReadUI(pages: pages, isHatim: isHatim),
);
Expand Down
9 changes: 3 additions & 6 deletions app/lib/modules/read/read.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export 'logic/read_cubit.dart';
export 'logic/read_theme_cubit.dart';
export 'services/read_service.dart';
export 'services/read_theme_service.dart';
export 'view/pagination_view.dart';
export 'view/read_view.dart';
export 'data/data.dart';
export 'domain/domain.dart';
export 'presentation/presentation.dart';
35 changes: 0 additions & 35 deletions app/lib/modules/read/services/read_service.dart

This file was deleted.

0 comments on commit 3ee23b1

Please sign in to comment.