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

Change home architecture #190

Merged
merged 7 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions app/lib/app/view/app_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class MyApp extends StatelessWidget {
),
BlocProvider(
create: (context) => HomeCubit(
HomeService(
context.read<PreferencesStorage>(),
context.read<RemoteClient>(),
GetHomeDataUseCase(
HomeRepositoryImpl(
HomeLocalDataSource(context.read<PreferencesStorage>()),
HomeRemoteDataSource(context.read<RemoteClient>()),
),
Comment on lines +34 to +38
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow 🤩

),
),
),
Expand Down
2 changes: 0 additions & 2 deletions app/lib/models/models.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export 'hatim/hatim_juz.dart';
export 'home/home_model.dart';
export 'home/hatim_read_model.dart';
export 'hatim/hatim_page.dart';
export 'juz/juz_data.dart';
export 'juz/juz_model.dart';
Expand Down
2 changes: 0 additions & 2 deletions app/lib/modules/hatim/logic/hatim_read/hatim_read_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

part 'hatim_read_state.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/modules/hatim/service/hatim_read_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/models/models.dart';
import 'package:my_quran/modules/modules.dart';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! 👌


class HatimReadService {
const HatimReadService(this.client);
Expand Down
3 changes: 3 additions & 0 deletions app/lib/modules/home/data/data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'source/source.dart';
export 'repository/home_repository.dart';
export 'model/hatim_read_model.dart';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super!

24 changes: 24 additions & 0 deletions app/lib/modules/home/data/repository/home_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:developer';

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

class HomeRepositoryImpl {
Eldar2021 marked this conversation as resolved.
Show resolved Hide resolved
const HomeRepositoryImpl(
this.localDataSource,
this.remoteDataSource,
);

final HomeLocalDataSource localDataSource;
final HomeRemoteDataSource remoteDataSource;

Future<HomeModel> getData(String token) async {
try {
final remoteData = await remoteDataSource.getRemoteData(token);
await localDataSource.saveLocalData(remoteData);
return remoteData;
} catch (e) {
log('HomeRepositoryImpl, getData error: $e');
return localDataSource.getLocalData();
}
}
}
34 changes: 34 additions & 0 deletions app/lib/modules/home/data/source/home_local_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'dart:convert';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/modules/modules.dart';

class HomeLocalDataSource {
HomeLocalDataSource(this.storage);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use const


final PreferencesStorage storage;
HomeModel getLocalData() {
const key = 'home-model';
final localValue = storage.readString(key: key);
if (localValue != null) {
final data = jsonDecode(localValue);
return HomeModel.fromJson(data as Map<String, dynamic>);
} else {
return const HomeModel(allDoneHatims: 0, allDonePages: 0, donePages: 0);
}
}
// Future<HomeModel> getLocalData() async {
// const key = 'home-model';
// final localValue = storage.readString(key: key);
// if (localValue != null) {
// final data = jsonDecode(localValue);
// return HomeModel.fromJson(data as Map<String, dynamic>);
// } else {
// return const HomeModel(allDoneHatims: 0, allDonePages: 0, donePages: 0);
// }
// }
Eldar2021 marked this conversation as resolved.
Show resolved Hide resolved

Future<void> saveLocalData(HomeModel data) async {
const key = 'home-model';
await storage.writeString(key: key, value: jsonEncode(data.toJson()));
}
}
22 changes: 22 additions & 0 deletions app/lib/modules/home/data/source/home_remote_data_source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';

class HomeRemoteDataSource {
const HomeRemoteDataSource(this.remoteClient);
final RemoteClient remoteClient;

Future<HomeModel> getRemoteData(String token) async {
final remoteValue = await remoteClient.get<HomeModel>(
apiConst.home,
fromJson: HomeModel.fromJson,
token: token,
);
return remoteValue.fold(
(left) {
throw Exception('Failed to fetch remote data');
},
(right) => right,
);
}
}
2 changes: 2 additions & 0 deletions app/lib/modules/home/data/source/source.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'home_local_data_source.dart';
export 'home_remote_data_source.dart';
2 changes: 2 additions & 0 deletions app/lib/modules/home/domain/domain.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'usecase/usecase.dart';
export 'entity/entity.dart';
1 change: 1 addition & 0 deletions app/lib/modules/home/domain/entity/entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'home_model.dart';
10 changes: 10 additions & 0 deletions app/lib/modules/home/domain/usecase/get_data_usecase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:my_quran/modules/modules.dart';

class GetHomeDataUseCase {
GetHomeDataUseCase(this.repository);
Eldar2021 marked this conversation as resolved.
Show resolved Hide resolved
final HomeRepositoryImpl repository;

Future<HomeModel> execute(String token) async {
return repository.getData(token);
}
}
10 changes: 10 additions & 0 deletions app/lib/modules/home/domain/usecase/usecase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'package:my_quran/modules/modules.dart';

class GetHomeDataUseCase {
GetHomeDataUseCase(this.repository);
Eldar2021 marked this conversation as resolved.
Show resolved Hide resolved
final HomeRepositoryImpl repository;

Future<HomeModel> execute(String token) async {
return repository.getData(token);
}
}
8 changes: 4 additions & 4 deletions app/lib/modules/home/home.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export 'logic/home_cubit.dart';
export 'view/home_view.dart';
export 'widgets/home_card.dart';
export 'service/home_service.dart';
export 'presentation/presentation.dart';
export 'data/repository/home_repository.dart';
export 'data/data.dart';
export 'domain/domain.dart';
19 changes: 0 additions & 19 deletions app/lib/modules/home/logic/home_cubit.dart

This file was deleted.

20 changes: 20 additions & 0 deletions app/lib/modules/home/presentation/cubit/home_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';

part 'home_state.dart';

class HomeCubit extends Cubit<HomeState> {
HomeCubit(this.getHomeDataUseCase) : super(const HomeState(FetchStatus.loading));
final GetHomeDataUseCase getHomeDataUseCase;

Future<void> getData(String token) async {
try {
final homeModel = await getHomeDataUseCase.execute(token);
emit(HomeState(FetchStatus.success, homeModel: homeModel));
} catch (_) {
emit(const HomeState(FetchStatus.error));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class HomeState extends Equatable {
final FetchStatus status;

@override
List<Object?> get props => [homeModel];
List<Object?> get props => [homeModel, status];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

}
3 changes: 3 additions & 0 deletions app/lib/modules/home/presentation/presentation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'cubit/home_cubit.dart';
export 'view/home_view.dart';
export 'widgets/home_card.dart';
37 changes: 0 additions & 37 deletions app/lib/modules/home/service/home_service.dart

This file was deleted.

Loading
Loading