-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from Eldar2021/ai/mock-data-sources
Add mock data to home
- Loading branch information
Showing
8 changed files
with
97 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
26 changes: 3 additions & 23 deletions
26
app/lib/modules/home/data/source/home_local_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, dynamic>); | ||
} else { | ||
return const HomeModelResponse(allDoneHatims: 0, allDonePages: 0, donePages: 0); | ||
} | ||
} | ||
|
||
Future<void> saveLocalData(HomeModelResponse data) async { | ||
await storage.writeString(key: _homeDataCacheKey, value: jsonEncode(data.toJson())); | ||
} | ||
Future<void> saveLocalData(HomeModelResponse data); | ||
} |
22 changes: 2 additions & 20 deletions
22
app/lib/modules/home/data/source/home_remote_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HomeModelResponse> getRemoteData() async { | ||
final remoteValue = await remoteClient.getType<HomeModelResponse>( | ||
apiConst.hatimDashBoard, | ||
fromJson: HomeModelResponse.fromJson, | ||
); | ||
|
||
return remoteValue.fold( | ||
(left) => throw Exception('Failed to fetch remote data $left'), | ||
(right) => right, | ||
); | ||
} | ||
abstract class HomeRemoteDataSource { | ||
Future<HomeModelResponse> getRemoteData(); | ||
} |
29 changes: 29 additions & 0 deletions
29
app/lib/modules/home/data/source/local/home_local_data_source_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, dynamic>); | ||
} else { | ||
return const HomeModelResponse(allDoneHatims: 0, allDonePages: 0, donePages: 0); | ||
} | ||
} | ||
|
||
@override | ||
Future<void> saveLocalData(HomeModelResponse data) async { | ||
await storage.writeString(key: _homeDataCacheKey, value: jsonEncode(data.toJson())); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
app/lib/modules/home/data/source/mock/home_local_data_source_mock.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> saveLocalData(HomeModelResponse data) => Future.value(); | ||
} |
16 changes: 16 additions & 0 deletions
16
app/lib/modules/home/data/source/mock/home_remote_data_source_mock.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HomeModelResponse> getRemoteData() async { | ||
return const HomeModelResponse( | ||
allDoneHatims: 0, | ||
allDonePages: 0, | ||
donePages: 0, | ||
); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/lib/modules/home/data/source/remote/home_remote_data_source_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HomeModelResponse> getRemoteData() async { | ||
final remoteValue = await remoteClient.getType<HomeModelResponse>( | ||
apiConst.hatimDashBoard, | ||
fromJson: HomeModelResponse.fromJson, | ||
); | ||
|
||
return remoteValue.fold( | ||
(left) => throw Exception('Failed to fetch remote data $left'), | ||
(right) => right, | ||
); | ||
} | ||
} |