arblant
firefox-vpn-client / app / lib / ui / about_page.dart
about_page.dart308 lines · 10.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';

import '../app_info.dart';
import '../core/core_controller.dart';
import '../core/updates.dart';
import '../l10n/app_localizations.dart';
import '../platform.dart';
import 'widgets.dart';

/// The versions, the update check, the source link, and the data folder.
class AboutPage extends StatefulWidget {
  const AboutPage({super.key, required this.core});

  final CoreController core;

  @override
  State<AboutPage> createState() => _AboutPageState();
}

class _AboutPageState extends State<AboutPage> {
  bool _checking = false;
  String? _result;
  Release? _release;

  Future<void> _check() async {
    final l = AppLocalizations.of(context);
    if (githubRepo.isEmpty) {
      setState(() => _result = l.repoNotSet);
      return;
    }
    setState(() {
      _checking = true;
      _result = null;
      _release = null;
    });
    try {
      final release = await checkForUpdate(
        proxy: updateProxy(widget.core.status),
      );
      Updates.found(release);
      if (!mounted) return;
      setState(() {
        _release = release;
        _result = release == null
            ? l.upToDate
            : l.updateAvailable(release.version);
      });
    } on Exception catch (e) {
      if (mounted) setState(() => _result = l.updateCheckFailed('$e'));
    } finally {
      if (mounted) setState(() => _checking = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context);
    final theme = Theme.of(context);
    final status = widget.core.status;
    final muted = theme.textTheme.bodySmall?.copyWith(
      color: theme.colorScheme.onSurfaceVariant,
    );
    final github = githubRepo.isEmpty ? '' : 'https://github.com/$githubRepo';
    return ListView(
      padding: pagePadding(context),
      children: [
        PageTitle(l.navAbout),
        const SizedBox(height: 20),
        Panel(
          child: Column(
            children: [
              Text(l.appTitle, style: theme.textTheme.titleLarge),
              const SizedBox(height: 4),
              Text(l.unofficialNote, style: muted, textAlign: TextAlign.center),
              const SizedBox(height: 20),
              _Line(label: l.appVersionLabel, value: appVersion),
              const Divider(height: 24),
              _Line(label: l.coreVersionLabel, value: status?.version ?? '—'),
              const Divider(height: 24),
              const _Line(label: 'Creators', value: 'muse & arblant'),
              const Divider(height: 24),
              // The result and the Download button under the title, so a
              // phone's narrow screen keeps the text readable.
              Row(
                children: [
                  Expanded(
                    child: Text(
                      l.checkUpdates,
                      style: theme.textTheme.titleSmall,
                    ),
                  ),
                  OutlinedButton.icon(
                    onPressed: _checking ? null : _check,
                    icon: _checking
                        ? const SizedBox.square(
                            dimension: 16,
                            child: CircularProgressIndicator(strokeWidth: 2),
                          )
                        : const Icon(Icons.update_rounded, size: 18),
                    label: Text(_checking ? l.checkingUpdates : l.checkUpdates),
                  ),
                ],
              ),
              if (_result != null) ...[
                const SizedBox(height: 8),
                Align(
                  alignment: AlignmentDirectional.centerStart,
                  child: Text(_result!, style: muted),
                ),
              ],
              if (_release case final release?) ...[
                const SizedBox(height: 10),
                _UpdateAction(core: widget.core, release: release),
              ],
              const Divider(height: 24),
              Row(
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(l.sourceCode, style: theme.textTheme.titleSmall),
                        const SizedBox(height: 2),
                        Text(
                          github.isEmpty ? l.repoNotSet : github,
                          style: muted,
                        ),
                      ],
                    ),
                  ),
                  OutlinedButton.icon(
                    onPressed: github.isEmpty ? null : () => openUrl(github),
                    icon: const Icon(Icons.open_in_new_rounded, size: 18),
                    label: const Text('GitHub'),
                  ),
                ],
              ),
              if (Platform.isAndroid) ...[
                const Divider(height: 24),
                Row(
                  children: [
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            l.webDashboard,
                            style: theme.textTheme.titleSmall,
                          ),
                          const SizedBox(height: 2),
                          Text(l.webDashboardHint, style: muted),
                        ],
                      ),
                    ),
                    OutlinedButton.icon(
                      onPressed: () => runShowingErrors(
                        context,
                        () async =>
                            openUrl('${await widget.core.dashboardLink()}'),
                      ),
                      icon: const Icon(Icons.open_in_new_rounded, size: 18),
                      label: Text(l.webDashboardOpen),
                    ),
                  ],
                ),
              ],
              // The data folder: not on a phone, where it is the app's
              // private storage, which nobody can open.
              if (!Platform.isAndroid &&
                  status != null &&
                  status.dataDir.isNotEmpty) ...[
                const Divider(height: 24),
                Row(
                  children: [
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(l.dataFolder, style: theme.textTheme.titleSmall),
                          const SizedBox(height: 2),
                          Text(
                            status.dataDir,
                            style: muted,
                            textDirection: TextDirection.ltr,
                          ),
                        ],
                      ),
                    ),
                    OutlinedButton.icon(
                      onPressed: () =>
                          runShowingErrors(context, widget.core.openFolder),
                      icon: const Icon(Icons.folder_open_rounded, size: 18),
                      label: Text(l.openFolder),
                    ),
                  ],
                ),
              ],
            ],
          ),
        ),
      ],
    );
  }
}

/// The found version's button: Update where the app installs it itself
/// (Windows), with the download's progress, why it failed, and Try again;
/// else Download.
class _UpdateAction extends StatelessWidget {
  const _UpdateAction({required this.core, required this.release});

  final CoreController core;
  final Release release;

  @override
  Widget build(BuildContext context) => ListenableBuilder(
    listenable: Listenable.merge([core, Updates.failure]),
    builder: (context, _) {
      final l = AppLocalizations.of(context);
      final muted = Theme.of(context).textTheme.bodySmall
          ?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant);
      final update = core.status?.update;
      Widget button(String label, IconData icon, VoidCallback? onPressed) =>
          SizedBox(
            width: double.infinity,
            child: FilledButton.icon(
              onPressed: onPressed,
              icon: Icon(icon, size: 18),
              label: Text(label),
            ),
          );
      if (update == null) {
        return release.url.isEmpty
            ? const SizedBox.shrink()
            : button(
                l.download,
                Icons.download_rounded,
                () => openDownload(release),
              );
      }
      final mine = update.version == release.version;
      if (mine && (update.downloading || update.installing)) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              update.downloading
                  ? l.updateDownloading(release.version)
                  : l.updateInstalling(release.version),
              style: muted,
            ),
            const SizedBox(height: 8),
            LinearProgressIndicator(
              value: update.downloading ? update.progress : null,
            ),
          ],
        );
      }
      final failure =
          Updates.failure.value ??
          (mine && update.failed ? update.error : null);
      return Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          if (failure != null) ...[
            Tooltip(
              message: failure,
              child: Text(l.updateFailed(release.version), style: muted),
            ),
            const SizedBox(height: 8),
          ],
          button(
            failure != null ? l.tryAgain : l.updateNow,
            failure != null
                ? Icons.refresh_rounded
                : Icons.system_update_alt_rounded,
            () => Updates.update(release),
          ),
        ],
      );
    },
  );
}

class _Line extends StatelessWidget {
  const _Line({required this.label, required this.value});

  final String label;
  final String value;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Row(
      children: [
        Expanded(child: Text(label, style: theme.textTheme.titleSmall)),
        Text(
          value,
          style: theme.textTheme.bodyMedium,
          textDirection: TextDirection.ltr,
        ),
      ],
    );
  }
}