Skip to content

Commit

Permalink
Merge pull request #234 from Eldar2021/ai/mock-data-sources
Browse files Browse the repository at this point in the history
Add mock data to home
  • Loading branch information
Eldar2021 authored Jul 21, 2024
2 parents eb47307 + 1360419 commit 58e2bfb
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 45 deletions.
6 changes: 4 additions & 2 deletions app/lib/app/presentation/view/app_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ class MyApp extends StatelessWidget {
create: (context) => HomeCubit(
GetHomeDataUseCase(
HomeRepositoryImpl(
HomeLocalDataSource(context.read<PreferencesStorage>()),
HomeRemoteDataSource(context.read<MqDio>()),
isMockData
? const HomeLocalDataSourceMock()
: HomeLocalDataSourceImpl(context.read<PreferencesStorage>()),
isMockData ? const HomeRemoteDataSourceMock() : HomeRemoteDataSourceImpl(context.read<MqDio>()),
),
),
),
Expand Down
4 changes: 4 additions & 0 deletions app/lib/modules/home/data/data.dart
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 app/lib/modules/home/data/source/home_local_data_source.dart
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 app/lib/modules/home/data/source/home_remote_data_source.dart
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();
}
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()));
}
}
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();
}
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,
);
}
}
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,
);
}
}

0 comments on commit 58e2bfb

Please sign in to comment.