-
-
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 1 commit
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
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'; | ||
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! 👌 |
||
|
||
class HatimReadService { | ||
const HatimReadService(this.client); | ||
|
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'; | ||
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. Super! |
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(); | ||
} | ||
} | ||
} |
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); | ||
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. 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())); | ||
} | ||
} |
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, | ||
); | ||
} | ||
} |
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'usecase/usecase.dart'; | ||
export 'entity/entity.dart'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'home_model.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); | ||
} | ||
} |
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); | ||
} | ||
} |
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'; |
This file was deleted.
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 |
---|---|---|
|
@@ -7,5 +7,5 @@ class HomeState extends Equatable { | |
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 🤩