Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mock data to home #234

Merged
merged 2 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
}
}
Loading