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/services/pal/pal_state_service.dart'; 8 : import 'package:pal/src/ui/editor/pages/helper_editor/font_editor/font_editor_viewmodel.dart'; 9 : import 'package:pal/src/ui/editor/pages/helper_editor/helper_editor.dart'; 10 : import 'package:pal/src/ui/editor/pages/helper_editor/helper_editor_factory.dart'; 11 : import 'package:pal/src/ui/editor/pages/helper_editor/helper_editor_notifiers.dart'; 12 : import 'package:pal/src/ui/editor/pages/helper_editor/widgets/editor_sending_overlay.dart'; 13 : 14 : import 'editor_fullscreen_helper.dart'; 15 : import 'editor_fullscreen_helper_viewmodel.dart'; 16 : 17 : class EditorFullScreenHelperPresenter extends Presenter<FullscreenHelperViewModel, EditorFullScreenHelperView> { 18 : 19 : final EditorHelperService editorHelperService; 20 : 21 : final HelperEditorPageArguments parameters; 22 : 23 1 : EditorFullScreenHelperPresenter( 24 : EditorFullScreenHelperView viewInterface, 25 : FullscreenHelperViewModel viewModel, 26 : this.editorHelperService, 27 : this.parameters, 28 1 : ) : super(viewModel, viewInterface) { 29 2 : this.viewModel.helperOpacity = 1; 30 3 : this.viewModel.canValidate = new ValueNotifier(false); 31 3 : this.viewModel.editableTextFieldController = StreamController<bool>.broadcast(); 32 : } 33 : 34 1 : @override 35 : void onInit() { 36 1 : super.onInit(); 37 : } 38 : 39 1 : Future onValidate() async { 40 1 : ValueNotifier<SendingStatus> status = new ValueNotifier(SendingStatus.SENDING); 41 4 : final config = CreateHelperConfig.from(parameters.pageId, viewModel); 42 : try { 43 3 : await viewInterface.showLoadingScreen(status); 44 3 : await Future.delayed(Duration(seconds: 1)); 45 2 : await editorHelperService 46 3 : .saveFullScreenHelper(EditorEntityFactory.buildFullscreenArgs(config, viewModel)); 47 1 : status.value = SendingStatus.SENT; 48 : } catch(error) { 49 2 : print("error occured $error"); 50 1 : status.value = SendingStatus.ERROR; 51 : } finally { 52 3 : await Future.delayed(Duration(seconds: 2)); 53 2 : viewInterface.closeLoadingScreen(); 54 3 : await Future.delayed(Duration(milliseconds: 100)); 55 1 : status.dispose(); 56 2 : viewInterface.closeEditor(); 57 : } 58 : } 59 : 60 0 : onCancel() { 61 0 : viewInterface.closeEditor(); 62 : } 63 : 64 : // Title 65 1 : onTitleChanged(String id, String newValue) 66 3 : => _onTextChanged(viewModel.titleField, newValue); 67 : 68 0 : onTitleTextStyleChanged(String id, TextStyle newTextStyle, FontKeys fontKeys) 69 0 : => _onStyleChanged(viewModel.titleField, newTextStyle, fontKeys); 70 : 71 : // Description field 72 1 : onDescriptionChanged(String id, String newValue) 73 3 : => _onTextChanged(viewModel.descriptionField, newValue); 74 : 75 0 : onDescriptionTextStyleChanged(String id, TextStyle newTextStyle, FontKeys fontKeys) 76 0 : => _onStyleChanged(viewModel.descriptionField, newTextStyle, fontKeys); 77 : 78 : // Positiv button 79 0 : onPositivTextChanged(String id, String newValue) 80 0 : => _onTextChanged(viewModel.positivButtonField, newValue); 81 : 82 0 : onPositivTextStyleChanged(String id, TextStyle newTextStyle, FontKeys fontKeys) 83 0 : => _onStyleChanged(viewModel.positivButtonField, newTextStyle, fontKeys); 84 : 85 : // Negativ button 86 0 : onNegativTextChanged(String id, String newValue) 87 0 : => _onTextChanged(viewModel.negativButtonField, newValue); 88 : 89 0 : onNegativTextStyleChanged(String id, TextStyle newTextStyle, FontKeys fontKeys) 90 0 : => _onStyleChanged(viewModel.negativButtonField, newTextStyle, fontKeys); 91 : 92 : 93 : @override 94 1 : Future onDestroy() async { 95 3 : this.viewModel.editableTextFieldController.close(); 96 1 : super.onDestroy(); 97 : // this.viewModel.canValidate.dispose(); 98 : // this.viewModel.canValidate = null; 99 : } 100 : 101 : //TODO move to view 102 3 : TextStyle googleCustomFont(String fontFamily) => this.viewInterface.googleCustomFont(fontFamily); 103 : 104 0 : onOutsideTap() { 105 0 : this.viewModel.editableTextFieldController.add(true); 106 : } 107 : 108 0 : String validateTitleTextField(String currentValue) { 109 0 : if (currentValue.length <= 0) { 110 : return 'Please enter some text'; 111 : } 112 0 : if (currentValue.length > 45) { 113 : return 'Maximum 45 characters'; 114 : } 115 : return null; 116 : } 117 : 118 4 : changeBackgroundColor() => this.viewInterface.showColorPickerDialog(viewModel, this); 119 : 120 1 : updateBackgroundColor(Color aColor) { 121 4 : viewModel.bodyBox.backgroundColor.value = aColor; 122 2 : this.viewInterface.closeColorPickerDialog(); 123 1 : _updateValidState(); 124 1 : this.refreshView(); 125 : } 126 : 127 0 : cancelUpdateBackgroundColor() => this.viewInterface.closeColorPickerDialog(); 128 : 129 0 : editMedia() async { 130 0 : final selectedMedia = await this 131 0 : .viewInterface 132 0 : .pushToMediaGallery(this.viewModel.media?.uuid); 133 : 134 0 : this.viewModel.media?.url?.value = selectedMedia?.url; 135 0 : this.viewModel.media?.uuid = selectedMedia?.id; 136 0 : this.refreshView(); 137 : } 138 : 139 : 140 : // ---------------------------------- 141 : // PRIVATES 142 : // ---------------------------------- 143 : 144 1 : _onTextChanged(TextFormFieldNotifier textNotifier, String newValue) { 145 2 : textNotifier.text.value = newValue; 146 1 : _updateValidState(); 147 : } 148 : 149 5 : _updateValidState() => viewModel.canValidate.value = isValid(); 150 : 151 0 : _onStyleChanged(TextFormFieldNotifier textNotifier, TextStyle newTextStyle, FontKeys fontKeys) { 152 0 : textNotifier?.fontColor?.value = newTextStyle?.color; 153 0 : textNotifier?.fontSize?.value = newTextStyle?.fontSize?.toInt(); 154 : if (fontKeys != null) { 155 0 : textNotifier?.fontWeight?.value = fontKeys.fontWeightNameKey; 156 0 : textNotifier?.fontFamily?.value = fontKeys.fontFamilyNameKey; 157 : } 158 : } 159 : 160 6 : bool isValid() => viewModel.positivButtonField.text.value.isNotEmpty 161 5 : && viewModel.negativButtonField.text.value.isNotEmpty 162 5 : && viewModel.titleField.text.value.isNotEmpty 163 5 : && viewModel.descriptionField.text.value.isNotEmpty; 164 : }