|
| 1 | +/// Screen orientation of the generated app. |
| 2 | +enum AppOrientation { |
| 3 | + auto('Automatic'), |
| 4 | + portrait('Portrait'), |
| 5 | + landscape('Landscape'); |
| 6 | + |
| 7 | + const AppOrientation(this.label); |
| 8 | + final String label; |
| 9 | +} |
| 10 | + |
| 11 | +/// When the generated app shows websites dark (Android 13 and newer). |
| 12 | +enum AppDarkMode { |
| 13 | + off('Off'), |
| 14 | + system('Follow the system'), |
| 15 | + always('Always'); |
| 16 | + |
| 17 | + const AppDarkMode(this.label); |
| 18 | + final String label; |
| 19 | +} |
| 20 | + |
| 21 | +/// The advanced settings of a generated app. Everything but [versionName] |
| 22 | +/// goes to the template through `assets/config.json`. |
| 23 | +class AppOptions { |
| 24 | + final AppOrientation orientation; |
| 25 | + final AppDarkMode darkMode; |
| 26 | + |
| 27 | + /// Links to other sites open in the browser instead of the app. |
| 28 | + final bool externalLinks; |
| 29 | + final bool progressBar; |
| 30 | + |
| 31 | + /// Show a page with a Retry button when a page cannot be loaded. |
| 32 | + final bool offlinePage; |
| 33 | + |
| 34 | + /// A reload button in the title bar, so it needs the title bar. |
| 35 | + final bool reloadButton; |
| 36 | + final bool hideSystemBars; |
| 37 | + final bool keepScreenOn; |
| 38 | + final bool pinchZoom; |
| 39 | + |
| 40 | + /// Show the app color instead of white until the first page is drawn. |
| 41 | + final bool loadingColor; |
| 42 | + final bool desktopMode; |
| 43 | + |
| 44 | + /// Replaces the WebView's user agent; empty keeps it. |
| 45 | + final String userAgent; |
| 46 | + |
| 47 | + /// Version name shown in Android's app info; empty keeps the template's. |
| 48 | + final String versionName; |
| 49 | + |
| 50 | + const AppOptions({ |
| 51 | + this.orientation = AppOrientation.auto, |
| 52 | + this.darkMode = AppDarkMode.off, |
| 53 | + this.externalLinks = false, |
| 54 | + this.progressBar = false, |
| 55 | + this.offlinePage = true, |
| 56 | + this.reloadButton = false, |
| 57 | + this.hideSystemBars = false, |
| 58 | + this.keepScreenOn = false, |
| 59 | + this.pinchZoom = false, |
| 60 | + this.loadingColor = false, |
| 61 | + this.desktopMode = false, |
| 62 | + this.userAgent = '', |
| 63 | + this.versionName = '', |
| 64 | + }); |
| 65 | + |
| 66 | + AppOptions copyWith({ |
| 67 | + AppOrientation? orientation, |
| 68 | + AppDarkMode? darkMode, |
| 69 | + bool? externalLinks, |
| 70 | + bool? progressBar, |
| 71 | + bool? offlinePage, |
| 72 | + bool? reloadButton, |
| 73 | + bool? hideSystemBars, |
| 74 | + bool? keepScreenOn, |
| 75 | + bool? pinchZoom, |
| 76 | + bool? loadingColor, |
| 77 | + bool? desktopMode, |
| 78 | + String? userAgent, |
| 79 | + String? versionName, |
| 80 | + }) => AppOptions( |
| 81 | + orientation: orientation ?? this.orientation, |
| 82 | + darkMode: darkMode ?? this.darkMode, |
| 83 | + externalLinks: externalLinks ?? this.externalLinks, |
| 84 | + progressBar: progressBar ?? this.progressBar, |
| 85 | + offlinePage: offlinePage ?? this.offlinePage, |
| 86 | + reloadButton: reloadButton ?? this.reloadButton, |
| 87 | + hideSystemBars: hideSystemBars ?? this.hideSystemBars, |
| 88 | + keepScreenOn: keepScreenOn ?? this.keepScreenOn, |
| 89 | + pinchZoom: pinchZoom ?? this.pinchZoom, |
| 90 | + loadingColor: loadingColor ?? this.loadingColor, |
| 91 | + desktopMode: desktopMode ?? this.desktopMode, |
| 92 | + userAgent: userAgent ?? this.userAgent, |
| 93 | + versionName: versionName ?? this.versionName, |
| 94 | + ); |
| 95 | + |
| 96 | + /// Reads [toJson]'s output. Missing keys, as in apps saved by older |
| 97 | + /// versions, and unknown enum values fall back to the defaults. |
| 98 | + factory AppOptions.fromJson(Map<String, dynamic> json) { |
| 99 | + const d = AppOptions(); |
| 100 | + T pick<T extends Enum>(List<T> values, String key, T fallback) => |
| 101 | + values.asNameMap()[json[key]] ?? fallback; |
| 102 | + return AppOptions( |
| 103 | + orientation: pick(AppOrientation.values, 'orientation', d.orientation), |
| 104 | + darkMode: pick(AppDarkMode.values, 'darkMode', d.darkMode), |
| 105 | + externalLinks: json['externalLinks'] as bool? ?? d.externalLinks, |
| 106 | + progressBar: json['progressBar'] as bool? ?? d.progressBar, |
| 107 | + offlinePage: json['offlinePage'] as bool? ?? d.offlinePage, |
| 108 | + reloadButton: json['reloadButton'] as bool? ?? d.reloadButton, |
| 109 | + hideSystemBars: json['hideSystemBars'] as bool? ?? d.hideSystemBars, |
| 110 | + keepScreenOn: json['keepScreenOn'] as bool? ?? d.keepScreenOn, |
| 111 | + pinchZoom: json['pinchZoom'] as bool? ?? d.pinchZoom, |
| 112 | + loadingColor: json['loadingColor'] as bool? ?? d.loadingColor, |
| 113 | + desktopMode: json['desktopMode'] as bool? ?? d.desktopMode, |
| 114 | + userAgent: json['userAgent'] as String? ?? d.userAgent, |
| 115 | + versionName: json['versionName'] as String? ?? d.versionName, |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + Map<String, dynamic> toJson() => { |
| 120 | + 'orientation': orientation.name, |
| 121 | + 'darkMode': darkMode.name, |
| 122 | + 'externalLinks': externalLinks, |
| 123 | + 'progressBar': progressBar, |
| 124 | + 'offlinePage': offlinePage, |
| 125 | + 'reloadButton': reloadButton, |
| 126 | + 'hideSystemBars': hideSystemBars, |
| 127 | + 'keepScreenOn': keepScreenOn, |
| 128 | + 'pinchZoom': pinchZoom, |
| 129 | + 'loadingColor': loadingColor, |
| 130 | + 'desktopMode': desktopMode, |
| 131 | + 'userAgent': userAgent.trim(), |
| 132 | + 'versionName': versionName.trim(), |
| 133 | + }; |
| 134 | +} |
0 commit comments