keyboard_method_handler.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'dart:convert';
  2. import 'package:flutter/services.dart';
  3. import 'package:keyboard/data/repository/characters_repository.dart';
  4. import 'package:keyboard/data/repository/keyboard_repository.dart';
  5. import '../utils/mmkv_util.dart';
  6. class KeyboardMethodHandler {
  7. final KeyboardRepository keyboardRepository;
  8. final CharactersRepository charactersRepository;
  9. static const String keyboardSelect = 'keyboard_select';
  10. KeyboardMethodHandler(this.keyboardRepository, this.charactersRepository);
  11. Future<dynamic> handleMethodCall(MethodCall call) async {
  12. switch (call.method) {
  13. case 'getKeyboardList':
  14. return await _handleGetKeyboardList(call);
  15. case 'selectedKeyboard':
  16. return _handleSelectedKeyboard(call);
  17. case 'getCharacterList':
  18. return await _handleGetCharacterList(call);
  19. case 'getCurrentKeyboardId':
  20. return await _handleGetCurrentKeyboardId(call);
  21. default:
  22. throw MissingPluginException('Not implemented: ${call.method}');
  23. }
  24. }
  25. Future<String> _handleGetKeyboardList(MethodCall call) async {
  26. String? type = call.arguments?['type'] as String?;
  27. final keyboardList = await keyboardRepository.getKeyboardList(type: type);
  28. final selectKeyboardId = KVUtil.getString(keyboardSelect, null);
  29. if (selectKeyboardId != null) {
  30. for (var element in keyboardList.keyboardInfos) {
  31. if (element.id == selectKeyboardId) {
  32. element.isSelect = true;
  33. }
  34. }
  35. }
  36. return jsonEncode(keyboardList.toJson());
  37. }
  38. Future<String> _handleSelectedKeyboard(MethodCall call) async {
  39. final String keyboardId = call.arguments['keyboardId'];
  40. KVUtil.putString(keyboardSelect, keyboardId);
  41. return "{}";
  42. }
  43. Future<String> _handleGetCurrentKeyboardId(MethodCall call) async {
  44. String? keyboardId = KVUtil.getString(keyboardSelect, null);
  45. if (keyboardId == null) {
  46. return "{}";
  47. }
  48. return jsonEncode({"keyboardId": keyboardId});
  49. }
  50. Future<String> _handleGetCharacterList(MethodCall call) async {
  51. final String keyboardId = call.arguments['keyboardId'];
  52. final characterList = await keyboardRepository.getKeyboardCharacterList(
  53. keyboardId: keyboardId,
  54. );
  55. return jsonEncode(characterList.toJson());
  56. }
  57. }