From 15125dabbe0f24a985eb72b9a7679f015af32925 Mon Sep 17 00:00:00 2001 From: Aidaiym Date: Sat, 20 Jul 2024 13:30:18 +0600 Subject: [PATCH 1/2] Add mock data to home --- app/lib/app/presentation/view/app_view.dart | 6 ++-- app/lib/config/app_config.dart | 2 +- app/lib/modules/home/data/data.dart | 4 +++ .../data/source/home_local_data_source.dart | 26 ++--------------- .../data/source/home_remote_data_source.dart | 22 ++------------ .../local/home_local_data_source_impl.dart | 29 +++++++++++++++++++ .../mock/home_local_data_source_mock.dart | 15 ++++++++++ .../mock/home_remote_data_source_mock.dart | 16 ++++++++++ .../remote/home_remote_data_source_impl.dart | 24 +++++++++++++++ 9 files changed, 98 insertions(+), 46 deletions(-) create mode 100644 app/lib/modules/home/data/source/local/home_local_data_source_impl.dart create mode 100644 app/lib/modules/home/data/source/mock/home_local_data_source_mock.dart create mode 100644 app/lib/modules/home/data/source/mock/home_remote_data_source_mock.dart create mode 100644 app/lib/modules/home/data/source/remote/home_remote_data_source_impl.dart diff --git a/app/lib/app/presentation/view/app_view.dart b/app/lib/app/presentation/view/app_view.dart index 94d38484..a76da2ef 100644 --- a/app/lib/app/presentation/view/app_view.dart +++ b/app/lib/app/presentation/view/app_view.dart @@ -74,8 +74,10 @@ class MyApp extends StatelessWidget { create: (context) => HomeCubit( GetHomeDataUseCase( HomeRepositoryImpl( - HomeLocalDataSource(context.read()), - HomeRemoteDataSource(context.read()), + isMockData + ? const HomeLocalDataSourceMock() + : HomeLocalDataSourceImpl(context.read()), + isMockData ? const HomeRemoteDataSourceMock() : HomeRemoteDataSourceImpl(context.read()), ), ), ), diff --git a/app/lib/config/app_config.dart b/app/lib/config/app_config.dart index 3b335e3a..d0661ddc 100644 --- a/app/lib/config/app_config.dart +++ b/app/lib/config/app_config.dart @@ -7,7 +7,7 @@ final class AppConfig { const AppConfig({ this.storage, this.isIntegrationTest = false, - this.isMockData = false, + this.isMockData = true, }); final bool isIntegrationTest; diff --git a/app/lib/modules/home/data/data.dart b/app/lib/modules/home/data/data.dart index 5252bc35..c8238aa0 100644 --- a/app/lib/modules/home/data/data.dart +++ b/app/lib/modules/home/data/data.dart @@ -1,4 +1,8 @@ export 'model/home_model_response.dart'; export 'repository/home_repository_impl.dart'; +export 'source/local/home_local_data_source_impl.dart'; +export 'source/remote/home_remote_data_source_impl.dart'; +export 'source/mock/home_local_data_source_mock.dart'; +export 'source/mock/home_remote_data_source_mock.dart'; export 'source/home_local_data_source.dart'; export 'source/home_remote_data_source.dart'; diff --git a/app/lib/modules/home/data/source/home_local_data_source.dart b/app/lib/modules/home/data/source/home_local_data_source.dart index 059097d5..b3562d04 100644 --- a/app/lib/modules/home/data/source/home_local_data_source.dart +++ b/app/lib/modules/home/data/source/home_local_data_source.dart @@ -1,27 +1,7 @@ -import 'dart:convert'; -import 'package:flutter/material.dart'; -import 'package:mq_storage/mq_storage.dart'; import 'package:my_quran/modules/modules.dart'; -@immutable -final class HomeLocalDataSource { - const HomeLocalDataSource(this.storage); +abstract class HomeLocalDataSource { + HomeModelResponse getLocalData(); - final PreferencesStorage storage; - - static const _homeDataCacheKey = 'home-model'; - - HomeModelResponse getLocalData() { - final localValue = storage.readString(key: _homeDataCacheKey); - if (localValue != null) { - final data = jsonDecode(localValue); - return HomeModelResponse.fromJson(data as Map); - } else { - return const HomeModelResponse(allDoneHatims: 0, allDonePages: 0, donePages: 0); - } - } - - Future saveLocalData(HomeModelResponse data) async { - await storage.writeString(key: _homeDataCacheKey, value: jsonEncode(data.toJson())); - } + Future saveLocalData(HomeModelResponse data); } diff --git a/app/lib/modules/home/data/source/home_remote_data_source.dart b/app/lib/modules/home/data/source/home_remote_data_source.dart index 95fcb8b8..1a774d5c 100644 --- a/app/lib/modules/home/data/source/home_remote_data_source.dart +++ b/app/lib/modules/home/data/source/home_remote_data_source.dart @@ -1,23 +1,5 @@ -import 'package:flutter/material.dart'; -import 'package:my_quran/config/config.dart'; -import 'package:my_quran/core/core.dart'; import 'package:my_quran/modules/modules.dart'; -@immutable -final class HomeRemoteDataSource { - const HomeRemoteDataSource(this.remoteClient); - - final MqDio remoteClient; - - Future getRemoteData() async { - final remoteValue = await remoteClient.getType( - apiConst.hatimDashBoard, - fromJson: HomeModelResponse.fromJson, - ); - - return remoteValue.fold( - (left) => throw Exception('Failed to fetch remote data $left'), - (right) => right, - ); - } +abstract class HomeRemoteDataSource { + Future getRemoteData(); } diff --git a/app/lib/modules/home/data/source/local/home_local_data_source_impl.dart b/app/lib/modules/home/data/source/local/home_local_data_source_impl.dart new file mode 100644 index 00000000..a5f1da1f --- /dev/null +++ b/app/lib/modules/home/data/source/local/home_local_data_source_impl.dart @@ -0,0 +1,29 @@ +import 'dart:convert'; +import 'package:flutter/material.dart'; +import 'package:mq_storage/mq_storage.dart'; +import 'package:my_quran/modules/modules.dart'; + +@immutable +final class HomeLocalDataSourceImpl implements HomeLocalDataSource { + const HomeLocalDataSourceImpl(this.storage); + + final PreferencesStorage storage; + + static const _homeDataCacheKey = 'home-model'; + + @override + HomeModelResponse getLocalData() { + final localValue = storage.readString(key: _homeDataCacheKey); + if (localValue != null) { + final data = jsonDecode(localValue); + return HomeModelResponse.fromJson(data as Map); + } else { + return const HomeModelResponse(allDoneHatims: 0, allDonePages: 0, donePages: 0); + } + } + + @override + Future saveLocalData(HomeModelResponse data) async { + await storage.writeString(key: _homeDataCacheKey, value: jsonEncode(data.toJson())); + } +} diff --git a/app/lib/modules/home/data/source/mock/home_local_data_source_mock.dart b/app/lib/modules/home/data/source/mock/home_local_data_source_mock.dart new file mode 100644 index 00000000..4499a114 --- /dev/null +++ b/app/lib/modules/home/data/source/mock/home_local_data_source_mock.dart @@ -0,0 +1,15 @@ +import 'package:flutter/material.dart'; +import 'package:my_quran/modules/modules.dart'; + +@immutable +final class HomeLocalDataSourceMock implements HomeLocalDataSource { + const HomeLocalDataSourceMock(); + + @override + HomeModelResponse getLocalData() { + return const HomeModelResponse(allDoneHatims: 0, allDonePages: 0, donePages: 0); + } + + @override + Future saveLocalData(HomeModelResponse data) => Future.value(); +} diff --git a/app/lib/modules/home/data/source/mock/home_remote_data_source_mock.dart b/app/lib/modules/home/data/source/mock/home_remote_data_source_mock.dart new file mode 100644 index 00000000..b271bb76 --- /dev/null +++ b/app/lib/modules/home/data/source/mock/home_remote_data_source_mock.dart @@ -0,0 +1,16 @@ +import 'package:flutter/material.dart'; +import 'package:my_quran/modules/modules.dart'; + +@immutable +final class HomeRemoteDataSourceMock implements HomeRemoteDataSource { + const HomeRemoteDataSourceMock(); + + @override + Future getRemoteData() async { + return const HomeModelResponse( + allDoneHatims: 0, + allDonePages: 0, + donePages: 0, + ); + } +} diff --git a/app/lib/modules/home/data/source/remote/home_remote_data_source_impl.dart b/app/lib/modules/home/data/source/remote/home_remote_data_source_impl.dart new file mode 100644 index 00000000..41bf7b00 --- /dev/null +++ b/app/lib/modules/home/data/source/remote/home_remote_data_source_impl.dart @@ -0,0 +1,24 @@ +import 'package:flutter/material.dart'; +import 'package:my_quran/config/config.dart'; +import 'package:my_quran/core/core.dart'; +import 'package:my_quran/modules/modules.dart'; + +@immutable +final class HomeRemoteDataSourceImpl implements HomeRemoteDataSource { + const HomeRemoteDataSourceImpl(this.remoteClient); + + final MqDio remoteClient; + + @override + Future getRemoteData() async { + final remoteValue = await remoteClient.getType( + apiConst.hatimDashBoard, + fromJson: HomeModelResponse.fromJson, + ); + + return remoteValue.fold( + (left) => throw Exception('Failed to fetch remote data $left'), + (right) => right, + ); + } +} From 13604191b8f5326d8c6ccffc370c76d52553105f Mon Sep 17 00:00:00 2001 From: Aidaiym Date: Sat, 20 Jul 2024 20:39:07 +0600 Subject: [PATCH 2/2] Add mock data to home --- app/lib/config/app_config.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/config/app_config.dart b/app/lib/config/app_config.dart index d0661ddc..3b335e3a 100644 --- a/app/lib/config/app_config.dart +++ b/app/lib/config/app_config.dart @@ -7,7 +7,7 @@ final class AppConfig { const AppConfig({ this.storage, this.isIntegrationTest = false, - this.isMockData = true, + this.isMockData = false, }); final bool isIntegrationTest;