-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
25972f1
03ebb11
fa45d81
568e3c0
1f0496f
9d60673
21fa34c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export 'model/home_model_response.dart'; | ||
export 'repository/home_repository.dart'; | ||
export 'source/home_local_data_source.dart'; | ||
export 'source/home_remote_data_source.dart'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'home_model_response.g.dart'; | ||
|
||
@JsonSerializable() | ||
class HomeModelResponse { | ||
const HomeModelResponse({ | ||
required this.allDoneHatims, | ||
required this.allDonePages, | ||
required this.donePages, | ||
}); | ||
|
||
factory HomeModelResponse.fromJson(Map<String, dynamic> json) => _$HomeModelResponseFromJson(json); | ||
Map<String, dynamic> toJson() => _$HomeModelResponseToJson(this); | ||
|
||
final int allDoneHatims; | ||
final int allDonePages; | ||
final int donePages; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:my_quran/modules/modules.dart'; | ||
|
||
class HomeRepositoryImpl implements HomeRepository { | ||
const HomeRepositoryImpl( | ||
this.localDataSource, | ||
this.remoteDataSource, | ||
); | ||
|
||
final HomeLocalDataSource localDataSource; | ||
final HomeRemoteDataSource remoteDataSource; | ||
|
||
@override | ||
Future<HomeModel> getData(String token) async { | ||
try { | ||
final remoteData = await remoteDataSource.getRemoteData(token); | ||
await localDataSource.saveLocalData(remoteData); | ||
return _convertData(remoteData); | ||
} catch (e) { | ||
log('HomeRepositoryImpl, getData error: $e'); | ||
return _convertData(localDataSource.getLocalData()); | ||
} | ||
} | ||
|
||
HomeModel _convertData(HomeModelResponse response) { | ||
return HomeModel( | ||
allDoneHatims: response.allDoneHatims, | ||
allDonePages: response.allDonePages, | ||
donePages: response.donePages, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dart:convert'; | ||
import 'package:mq_storage/mq_storage.dart'; | ||
import 'package:my_quran/modules/modules.dart'; | ||
|
||
class HomeLocalDataSource { | ||
const HomeLocalDataSource(this.storage); | ||
|
||
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())); | ||
} | ||
} |
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<HomeModelResponse> getRemoteData(String token) async { | ||
final remoteValue = await remoteClient.get<HomeModelResponse>( | ||
apiConst.home, | ||
fromJson: HomeModelResponse.fromJson, | ||
token: token, | ||
); | ||
|
||
return remoteValue.fold( | ||
(left) => throw Exception('Failed to fetch remote data $left'), | ||
(right) => right, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'entity/home_model.dart'; | ||
export 'repository/home_repository.dart'; | ||
export 'usecase/get_data_usecase.dart'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class HomeModel { | ||
const HomeModel({ | ||
required this.allDoneHatims, | ||
required this.allDonePages, | ||
required this.donePages, | ||
}); | ||
|
||
final int allDoneHatims; | ||
final int allDonePages; | ||
final int donePages; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'package:my_quran/modules/modules.dart'; | ||
|
||
abstract class HomeRepository { | ||
Future<HomeModel> getData(String token); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:my_quran/modules/modules.dart'; | ||
|
||
class GetHomeDataUseCase { | ||
const GetHomeDataUseCase(this.repository); | ||
|
||
final HomeRepository repository; | ||
|
||
Future<HomeModel> execute(String token) { | ||
return repository.getData(token); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export 'logic/home_cubit.dart'; | ||
export 'view/home_view.dart'; | ||
export 'widgets/home_card.dart'; | ||
export 'service/home_service.dart'; | ||
export 'data/data.dart'; | ||
export 'domain/domain.dart'; | ||
export 'presentation/presentation.dart'; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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()); | ||
|
||
final GetHomeDataUseCase getHomeDataUseCase; | ||
|
||
Future<void> getData(String token) async { | ||
try { | ||
final homeModel = await getHomeDataUseCase.execute(token); | ||
emit(HomeState(status: FetchStatus.success, homeModel: homeModel)); | ||
} catch (_) { | ||
emit(const HomeState(status: FetchStatus.error)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
part of 'home_cubit.dart'; | ||
|
||
class HomeState extends Equatable { | ||
const HomeState(this.status, {this.homeModel}); | ||
const HomeState({ | ||
this.status = FetchStatus.loading, | ||
this.homeModel, | ||
}); | ||
|
||
final HomeModel? homeModel; | ||
final FetchStatus status; | ||
|
||
@override | ||
List<Object?> get props => [homeModel]; | ||
List<Object?> get props => [homeModel, status]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! |
||
} |
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'; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow 🤩