-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from Eldar2021/ai/mock-data-sources
Add mock to read and hatim
- Loading branch information
Showing
18 changed files
with
359 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 12 additions & 99 deletions
111
app/lib/modules/hatim/data/source/hatim_remote_data_source.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
90 changes: 90 additions & 0 deletions
90
app/lib/modules/hatim/data/source/mock/hatim_remote_data_source_mock.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
113 changes: 113 additions & 0 deletions
113
app/lib/modules/hatim/data/source/remote/hatim_remote_data_source_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/lib/modules/read/data/source/local/local_theme_data_source_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.