Line data Source code
1 : import 'dart:typed_data'; 2 : 3 : import 'package:flutter/widgets.dart'; 4 : import 'package:mvvm_builder/mvvm_builder.dart'; 5 : import 'package:pal/src/services/editor/project/app_icon_grabber_delegate.dart'; 6 : import 'package:pal/src/services/editor/project/project_editor_service.dart'; 7 : import 'package:pal/src/services/package_version.dart'; 8 : import 'package:pal/src/ui/editor/pages/app_settings/app_settings.dart'; 9 : import 'package:pal/src/ui/editor/pages/app_settings/app_settings_viewmodel.dart'; 10 : 11 : class AppSettingsPresenter 12 : extends Presenter<AppSettingsModel, AppSettingsView> { 13 : final PackageVersionReader packageVersionReader; 14 : final AppIconGrabberDelegate appIconGrabberDelegate; 15 : final ProjectEditorService projectEditorService; 16 : 17 1 : AppSettingsPresenter( 18 : AppSettingsView viewInterface, { 19 : @required this.packageVersionReader, 20 : @required this.projectEditorService, 21 : @required this.appIconGrabberDelegate, 22 2 : }) : super(AppSettingsModel(), viewInterface); 23 : 24 : @override 25 1 : Future onInit() async { 26 2 : this.viewModel.appIconAnimation = false; 27 2 : this.viewModel.isSendingAppIcon = false; 28 2 : this.viewModel.isLoadingAppInfo = false; 29 2 : await this.getAppIcon(); 30 : 31 1 : this.readAppInfo(); 32 1 : startAnimation(); 33 : 34 3 : WidgetsBinding.instance.addPostFrameCallback(afterLayout); 35 : } 36 : 37 1 : afterLayout(Duration duration) { 38 4 : this.viewModel.headerSize = this.viewInterface.getHeaderSize(); 39 1 : this.refreshView(); 40 : } 41 : 42 1 : startAnimation() async { 43 4 : await Future.delayed(Duration(milliseconds: 350), () { 44 2 : this.viewModel.appIconAnimation = true; 45 1 : this.refreshAnimations(); 46 : }); 47 : } 48 : 49 1 : onAppIconAnimationEnd() { 50 2 : this.viewModel.appIconAnimation = false; 51 : } 52 : 53 1 : readAppInfo() async { 54 2 : this.viewModel.isLoadingAppInfo = true; 55 1 : this.refreshView(); 56 : 57 3 : await this.packageVersionReader.init(); 58 4 : this.viewModel.appVersion = this.packageVersionReader.version; 59 4 : this.viewModel.appName = this.packageVersionReader.appName; 60 : 61 2 : this.viewModel.isLoadingAppInfo = false; 62 1 : this.refreshView(); 63 : } 64 : 65 1 : refreshAppIcon() async { 66 2 : this.viewModel.isSendingAppIcon = true; 67 1 : this.refreshView(); 68 : 69 3 : Uint8List appIcon = await this.appIconGrabberDelegate.getClientAppIcon(); 70 2 : if (this.viewModel.appIconId == null) { 71 : //Create app icon 72 1 : this._createAppIcon(appIcon); 73 : } else { 74 : // update app icon 75 3 : this._updateAppIcon(appIcon, this.viewModel.appIconId); 76 : } 77 : } 78 : 79 1 : void _createAppIcon(Uint8List appIcon) { 80 4 : this.projectEditorService.sendAppIcon(appIcon, "png").then((appIcon) { 81 3 : this.viewModel.appIconId = appIcon.id; 82 3 : this.viewModel.appIconUrl = appIcon.url; 83 2 : this.viewModel.isSendingAppIcon = false; 84 1 : this.refreshView(); 85 2 : this.viewInterface.showMessage('App icon updated successfully', true); 86 1 : }).catchError((onError) { 87 0 : this.viewModel.isSendingAppIcon = false; 88 0 : this.refreshView(); 89 0 : this.viewInterface.showMessage('error while saving app Icon', false); 90 : }); 91 : } 92 : 93 1 : void _updateAppIcon(Uint8List appIcon, String appIconId) { 94 : this 95 1 : .projectEditorService 96 1 : .updateAppIcon(appIconId, appIcon, "png") 97 2 : .then((appIcon) { 98 3 : this.viewModel.appIconId = appIcon.id; 99 3 : this.viewModel.appIconUrl = appIcon.url; 100 2 : this.viewModel.isSendingAppIcon = false; 101 1 : this.refreshView(); 102 2 : this.viewInterface.showMessage('App icon updated successfully', true); 103 1 : }).catchError((onError) { 104 0 : this.viewModel.isSendingAppIcon = false; 105 0 : this.refreshView(); 106 0 : this.viewInterface.showMessage('error while saving app Icon', false); 107 : }); 108 : } 109 : 110 1 : Future getAppIcon() { 111 4 : return this.projectEditorService.getAppIcon().then((appIcon) { 112 3 : this.viewModel.appIconId = appIcon.id; 113 3 : this.viewModel.appIconUrl = appIcon.url; 114 : }); 115 : } 116 : }