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 to read and hatim #235

Merged
merged 1 commit 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
2 changes: 2 additions & 0 deletions app/lib/modules/hatim/data/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ export 'model/hatim_read_model.dart';
export 'model/hatim_juz.dart';
export 'model/hatim_page.dart';
export 'model/hatim_base_response.dart';
export 'source/remote/hatim_remote_data_source_impl.dart';
export 'source/hatim_remote_data_source.dart';
export 'source/mock/hatim_remote_data_source_mock.dart';
export 'repository/hatim_read_repository_impl.dart';
111 changes: 12 additions & 99 deletions app/lib/modules/hatim/data/source/hatim_remote_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,102 +1,15 @@
import 'dart:async';
import 'dart:convert';

import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class HatimRemoteDataSource {
HatimRemoteDataSource({
required this.remoteClient,
});

final MqDio remoteClient;

late final WebSocketChannel channel;
bool isInitilized = false;

Future<HatimReadModel> getHatim() async {
try {
final res = await remoteClient.postType(
apiConst.joinToHatim,
fromJson: HatimReadModel.fromJson,
);

return res.fold((l) => throw l, (r) => r);
} catch (e, s) {
MqCrashlytics.report(e, s);
throw Exception(e);
}
}

void connectToSocket(String token) {
if (!isInitilized) {
channel = WebSocketChannel.connect(
Uri.parse(apiConst.getSocket(token)),
);
isInitilized = true;
}
}

void sinkHatimJuzs(String hatimId) {
final data = {
'type': 'list_of_juz',
'hatim_id': hatimId,
};
channel.sink.add(json.encode(data));
}

void sinkHatimUserPages() {
final data = {
'type': 'user_pages',
};
channel.sink.add(jsonEncode(data));
}

void sinkHatimJuzPages(String juzId) {
final data = {
'type': 'list_of_page',
'juz_id': juzId,
};
channel.sink.add(jsonEncode(data));
}

void sinkSelectPage(String pageId) {
final data = {
'type': 'book',
'pageId': pageId,
};
channel.sink.add(jsonEncode(data));
}

void sinkUnSelectPage(String pageId) {
final data = {
'type': 'to_do',
'pageId': pageId,
};
channel.sink.add(jsonEncode(data));
}

void sinkInProgressPages(List<String> pageIds) {
final data = {
'type': 'in_progress',
'pageIds': pageIds,
};
channel.sink.add(jsonEncode(data));
}

void sinkDonePages(List<String> pageIds) {
final data = {
'type': 'done',
'pageIds': pageIds,
};
channel.sink.add(jsonEncode(data));
}

Future<void> close() async {
await channel.sink.close();
}

Stream<dynamic> get stream => channel.stream;
abstract class HatimRemoteDataSource {
Future<HatimReadModel> getHatim();
void connectToSocket(String token);
void sinkHatimJuzs(String hatimId);
void sinkHatimUserPages();
void sinkHatimJuzPages(String juzId);
void sinkSelectPage(String pageId);
void sinkUnSelectPage(String pageId);
void sinkInProgressPages(List<String> pageIds);
void sinkDonePages(List<String> pageIds);
Future<void> close();
Stream<dynamic> get stream;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'dart:async';
import 'dart:convert';

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

class HatimRemoteDataSourceMock implements HatimRemoteDataSource {
final StreamController<dynamic> _controller = StreamController<dynamic>();

@override
Future<HatimReadModel> getHatim() async {
return const HatimReadModel(
id: '1',
status: 'done',
type: 'user_pages',
);
}

@override
void connectToSocket(String token) {}

@override
void sinkHatimJuzs(String hatimId) {
final data = {
'type': 'list_of_juz',
'hatim_id': hatimId,
};
_controller.add(json.encode(data));
}

@override
void sinkHatimUserPages() {
final data = {
'type': 'user_pages',
};
_controller.add(jsonEncode(data));
}

@override
void sinkHatimJuzPages(String juzId) {
final data = {
'type': 'list_of_page',
'juz_id': juzId,
};
_controller.add(jsonEncode(data));
}

@override
void sinkSelectPage(String pageId) {
final data = {
'type': 'book',
'pageId': pageId,
};
_controller.add(jsonEncode(data));
}

@override
void sinkUnSelectPage(String pageId) {
final data = {
'type': 'to_do',
'pageId': pageId,
};
_controller.add(jsonEncode(data));
}

@override
void sinkInProgressPages(List<String> pageIds) {
final data = {
'type': 'in_progress',
'pageIds': pageIds,
};
_controller.add(jsonEncode(data));
}

@override
void sinkDonePages(List<String> pageIds) {
final data = {
'type': 'done',
'pageIds': pageIds,
};
_controller.add(jsonEncode(data));
}

@override
Future<void> close() async {
await _controller.close();
}

@override
Stream<dynamic> get stream => _controller.stream;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'dart:async';
import 'dart:convert';

import 'package:my_quran/config/config.dart';
import 'package:my_quran/core/core.dart';
import 'package:my_quran/modules/modules.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class HatimRemoteDataSourceImpl implements HatimRemoteDataSource {
HatimRemoteDataSourceImpl({
required this.remoteClient,
});

final MqDio remoteClient;

late final WebSocketChannel channel;
bool isInitilized = false;

@override
Future<HatimReadModel> getHatim() async {
try {
final res = await remoteClient.postType(
apiConst.joinToHatim,
fromJson: HatimReadModel.fromJson,
);

return res.fold((l) => throw l, (r) => r);
} catch (e, s) {
MqCrashlytics.report(e, s);
throw Exception(e);
}
}

@override
void connectToSocket(String token) {
if (!isInitilized) {
channel = WebSocketChannel.connect(
Uri.parse(apiConst.getSocket(token)),
);
isInitilized = true;
}
}

@override
void sinkHatimJuzs(String hatimId) {
final data = {
'type': 'list_of_juz',
'hatim_id': hatimId,
};
channel.sink.add(json.encode(data));
}

@override
void sinkHatimUserPages() {
final data = {
'type': 'user_pages',
};
channel.sink.add(jsonEncode(data));
}

@override
void sinkHatimJuzPages(String juzId) {
final data = {
'type': 'list_of_page',
'juz_id': juzId,
};
channel.sink.add(jsonEncode(data));
}

@override
void sinkSelectPage(String pageId) {
final data = {
'type': 'book',
'pageId': pageId,
};
channel.sink.add(jsonEncode(data));
}

@override
void sinkUnSelectPage(String pageId) {
final data = {
'type': 'to_do',
'pageId': pageId,
};
channel.sink.add(jsonEncode(data));
}

@override
void sinkInProgressPages(List<String> pageIds) {
final data = {
'type': 'in_progress',
'pageIds': pageIds,
};
channel.sink.add(jsonEncode(data));
}

@override
void sinkDonePages(List<String> pageIds) {
final data = {
'type': 'done',
'pageIds': pageIds,
};
channel.sink.add(jsonEncode(data));
}

@override
Future<void> close() async {
await channel.sink.close();
}

@override
Stream<dynamic> get stream => channel.stream;
}
6 changes: 3 additions & 3 deletions app/lib/modules/hatim/presentation/view/hatim_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class HatimView extends StatelessWidget {
return BlocProvider(
create: (context) => HatimBloc(
repo: HatimReadRepositoryImpl(
dataSource: HatimRemoteDataSource(
remoteClient: context.read<MqDio>(),
),
dataSource: context.read<AppConfig>().isMockData
? HatimRemoteDataSourceMock()
: HatimRemoteDataSourceImpl(remoteClient: context.read<MqDio>()),
),
token: context.read<AuthCubit>().state.user!.accessToken,
)..add(const GetInitailDataEvent()),
Expand Down
6 changes: 6 additions & 0 deletions app/lib/modules/read/data/data.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
export 'repository/read_repository_impl.dart';
export 'repository/read_theme_repository_impl.dart';
export 'source/local/local_theme_data_source_impl.dart';
export 'source/local/read_local_data_source_impl.dart';
export 'source/remote/read_remote_data_source_impl.dart';
export 'source/local_theme_data_source.dart';
export 'source/read_local_data_source.dart';
export 'source/read_remote_data_source.dart';
export 'source/mock/local_theme_data_source_mock.dart';
export 'source/mock/read_local_data_source_mock.dart';
export 'source/mock/read_remote_data_source_mock.dart';
export 'model/filters_response.dart';
export 'model/meta_response.dart';
export 'model/quran_page_response.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class ReadRepositoryImpl implements ReadRepository {
);

final ReadRemoteDataSource remoteDataSource;
final ReadLocalDataSource localDataSource;
final ReadLocalDataSourceImpl localDataSource;

@override
Future<QuranPageEntity?> getPage(int page, String quranFmt) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:my_quran/modules/modules.dart';
final class ReadThemeRepositoryImpl implements ReadThemeRepository {
const ReadThemeRepositoryImpl(this.localThemeDataSource);

final LocalThemeDataSource localThemeDataSource;
final LocalThemeDataSourceImpl localThemeDataSource;

@override
ReadThemeState get getInitialThemeState {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dart:convert';
import 'package:meta/meta.dart';
import 'package:mq_storage/mq_storage.dart';
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/modules/modules.dart';

@immutable
final class LocalThemeDataSourceImpl implements LocalThemeDataSource {
const LocalThemeDataSourceImpl(this.storage);

final PreferencesStorage storage;

@override
ReadThemeState get initialTheme {
final value = storage.readString(key: StorageKeys.readThemeKey);
return value != null ? ReadThemeState.fromJson(json.decode(value) as Map<String, dynamic>) : const ReadThemeState();
}

@override
Future<void> saveThemeState(ReadThemeState themeState) async {
final value = json.encode(themeState.toJson());
await storage.writeString(key: StorageKeys.readThemeKey, value: value);
}
}
Loading
Loading