Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:flutter/material.dart'; 4 : import 'package:mvvm_builder/mvvm_builder.dart'; 5 : import 'package:pal/src/services/editor/helper/helper_editor_models.dart'; 6 : import 'package:pal/src/services/editor/helper/helper_editor_service.dart'; 7 : import 'package:pal/src/ui/editor/pages/helper_editor/font_editor/font_editor_viewmodel.dart'; 8 : import 'package:pal/src/ui/editor/pages/helper_editor/helper_editor.dart'; 9 : import 'package:pal/src/ui/editor/pages/helper_editor/helper_editor_factory.dart'; 10 : import 'package:pal/src/ui/editor/pages/helper_editor/helper_editor_notifiers.dart'; 11 : import 'package:pal/src/ui/editor/pages/helper_editor/widgets/editor_sending_overlay.dart'; 12 : 13 : import 'editor_update_helper.dart'; 14 : import 'editor_update_helper_viewmodel.dart'; 15 : 16 : class EditorUpdateHelperPresenter extends Presenter<UpdateHelperViewModel, EditorUpdateHelperView> { 17 : 18 : final EditorHelperService editorHelperService; 19 : 20 : final HelperEditorPageArguments parameters; 21 : 22 : final StreamController<bool> editableTextFieldController; 23 : 24 1 : EditorUpdateHelperPresenter( 25 : EditorUpdateHelperView viewInterface, 26 : UpdateHelperViewModel updateHelperViewModel, 27 : this.editorHelperService, 28 : this.parameters 29 1 : ) : editableTextFieldController = StreamController<bool>.broadcast(), 30 1 : super(updateHelperViewModel, viewInterface) { 31 3 : viewModel.canValidate = new ValueNotifier(false); 32 : } 33 : 34 1 : @override 35 : void onInit() { 36 2 : this.viewModel.isKeyboardVisible = false; 37 : } 38 : 39 1 : @override 40 : void afterViewInit() { 41 2 : this.viewInterface.hidePalBubble(); 42 : } 43 : 44 0 : onKeyboardVisibilityChange(bool visible) { 45 0 : this.viewModel.isKeyboardVisible = visible; 46 0 : this.refreshView(); 47 : } 48 : 49 1 : Future addChangelogNote() async { 50 3 : await viewInterface.scrollToBottomChangelogList(); 51 2 : viewModel.addChangelog(); 52 1 : this.refreshView(); 53 : } 54 : 55 3 : void onCancel() => viewInterface.closeEditor(); 56 : 57 1 : Future<void> onValidate() async { 58 1 : ValueNotifier<SendingStatus> status = new ValueNotifier(SendingStatus.SENDING); 59 4 : final config = CreateHelperConfig.from(parameters.pageId, viewModel); 60 : try { 61 3 : await viewInterface.showLoadingScreen(status); 62 3 : await Future.delayed(Duration(seconds: 1)); 63 2 : await editorHelperService 64 3 : .saveUpdateHelper(EditorEntityFactory.buildUpdateArgs(config, viewModel)); 65 1 : status.value = SendingStatus.SENT; 66 : } catch(error) { 67 0 : status.value = SendingStatus.ERROR; 68 : } finally { 69 3 : await Future.delayed(Duration(seconds: 2)); 70 2 : viewInterface.closeLoadingScreen(); 71 3 : await Future.delayed(Duration(milliseconds: 100)); 72 1 : status.dispose(); 73 2 : viewInterface.closeEditor(); 74 : } 75 : } 76 : 77 1 : onTitleFieldChanged(String id, String newValue) 78 3 : => _onTextChanged(viewModel.titleField, newValue); 79 : 80 1 : onThanksFieldChanged(String id, String newValue) 81 3 : => _onTextChanged(viewModel.thanksButton, newValue); 82 : 83 0 : onTitleTextStyleChanged(String id, TextStyle newTextStyle, FontKeys fontKeys) 84 0 : => _onStyleChanged(viewModel.titleField, newTextStyle, fontKeys); 85 : 86 0 : onThanksTextStyleFieldChanged(String id, TextStyle newTextStyle, FontKeys fontKeys) 87 0 : => _onStyleChanged(viewModel.thanksButton, newTextStyle, fontKeys); 88 : 89 1 : onChangelogTextChanged(String id, String newValue) 90 4 : => _onTextChanged(viewModel.changelogsFields[id], newValue); 91 : 92 0 : onChangelogTextStyleFieldChanged(String id, TextStyle newTextStyle, FontKeys fontKeys) 93 0 : => _onStyleChanged(viewModel.changelogsFields[id], newTextStyle, fontKeys); 94 : 95 1 : changeBackgroundColor() { 96 2 : this.viewInterface.showColorPickerDialog( 97 4 : viewModel?.bodyBox?.backgroundColor?.value, 98 1 : updateBackgroundColor, 99 0 : () => viewInterface.closeColorPickerDialog() 100 : ); 101 : } 102 : 103 1 : updateBackgroundColor(Color aColor) { 104 4 : viewModel.bodyBox.backgroundColor.value = aColor; 105 1 : this.refreshView(); 106 : } 107 : 108 0 : onOutsideTap() => this.editableTextFieldController.add(true); 109 : 110 0 : editMedia() async { 111 0 : final selectedMedia = await this 112 0 : .viewInterface 113 0 : .pushToMediaGallery(viewModel.media?.uuid); 114 0 : viewModel.media?.url?.value = selectedMedia?.url; 115 0 : viewModel.media?.uuid = selectedMedia?.id; 116 0 : this.refreshView(); 117 : } 118 : 119 0 : String validateTitleTextField(String currentValue) { 120 0 : if (currentValue.length <= 0) { 121 : return 'Please enter some text'; 122 : } 123 0 : if (currentValue.length > 45) { 124 : return 'Maximum 45 characters'; 125 : } 126 : return null; 127 : } 128 : 129 0 : String validateChangelogTextField(String currentValue) { 130 0 : if (currentValue.length <= 0) { 131 : return 'Please enter some text'; 132 : } 133 0 : if (currentValue.length > 45) { 134 : return 'Maximum 45 characters'; 135 : } 136 : return null; 137 : } 138 : 139 : // ---------------------------------- 140 : // PRIVATES 141 : // ---------------------------------- 142 : 143 1 : _onTextChanged(TextFormFieldNotifier textNotifier, String newValue) { 144 2 : textNotifier.text.value = newValue; 145 4 : viewModel.canValidate.value = isValid(); 146 : } 147 : 148 0 : _onStyleChanged(TextFormFieldNotifier textNotifier, TextStyle newTextStyle, FontKeys fontKeys) { 149 0 : textNotifier?.fontColor?.value = newTextStyle?.color; 150 0 : textNotifier?.fontSize?.value = newTextStyle?.fontSize?.toInt(); 151 : if (fontKeys != null) { 152 0 : textNotifier?.fontWeight?.value = fontKeys.fontWeightNameKey; 153 0 : textNotifier?.fontFamily?.value = fontKeys.fontFamilyNameKey; 154 : } 155 : } 156 : 157 6 : bool isValid() => viewModel.titleField.text.value.isNotEmpty 158 4 : && viewModel.changelogsFields.length > 0; 159 : }