arblant
firefox-vpn-client / app / lib / ui / zed_pixel_home.dart
zed_pixel_home.dart374 lines · 11.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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
import 'package:flutter/material.dart';

import '../app_settings.dart';
import '../core/core_controller.dart';
import '../core/models.dart';
import '../l10n/app_localizations.dart';
import 'accounts_page.dart' show accountReason;
import 'format.dart';
import 'widgets.dart';
import 'zed_home_common.dart';

/// The Pixel design, ported from FoxyVPN's Android app: a hero card
/// whose color follows the connection, the morphing core and the state
/// inside it; fact chips under it; then the server, accounts and speed
/// rows. Tapping a chip opens the connection details.
class ZedPixelHome extends StatefulWidget {
  const ZedPixelHome({
    super.key,
    required this.core,
    required this.settings,
    this.onOpenServers,
    this.onOpenAccounts,
  });

  final CoreController core;
  final AppSettings settings;
  final VoidCallback? onOpenServers;
  final VoidCallback? onOpenAccounts;

  @override
  State<ZedPixelHome> createState() => _ZedPixelHomeState();
}

class _ZedPixelHomeState extends State<ZedPixelHome> {
  bool _detailsOpen = false;

  CoreController get core => widget.core;

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context);
    final status = core.status!;
    final d = ZedHomeData(core, status, l);
    return ZedHomeScaffold(
      children: [
        const SizedBox(height: 8),
        _HeroCard(
          data: d,
          onToggle: () => d.toggle(context, widget.onOpenAccounts),
        ),
        const SizedBox(height: 10),
        _FactChips(
          data: d,
          onTap: () => setState(() => _detailsOpen = !_detailsOpen),
        ),
        if (_detailsOpen && d.live) ...[
          const SizedBox(height: 10),
          _DetailsPanel(data: d),
        ],
        if (d.hasError) ...[
          const SizedBox(height: 10),
          ZedErrorCard(text: d.errorText),
        ],
        const SizedBox(height: 10),
        ZedServerRow(
          data: d,
          onTap: () => widget.onOpenServers?.call(),
        ),
        const SizedBox(height: 10),
        ZedAccountsRow(
          data: d,
          onTap: () => d.openAccounts(context, widget.onOpenAccounts),
        ),
        const SizedBox(height: 10),
        ZedSpeedRow(data: d),
        const SizedBox(height: 16),
      ],
    );
  }
}

/// The hero card: surface color at rest, the primary container's when
/// connected, both cross-faded; the state word slides and fades between
/// states; a progress line while connecting.
class _HeroCard extends StatelessWidget {
  const _HeroCard({required this.data, required this.onToggle});

  final ZedHomeData data;
  final VoidCallback onToggle;

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context);
    final theme = Theme.of(context);
    final scheme = theme.colorScheme;
    final live = data.live;
    final busy = data.busy;
    final ink = live ? scheme.onPrimaryContainer : scheme.onSurface;
    final sub = live
        ? scheme.onPrimaryContainer.withValues(alpha: 0.78)
        : scheme.onSurfaceVariant;
    final title = live
        ? l.stateConnected
        : busy
        ? l.stateConnecting
        : l.stateOff;
    return AnimatedContainer(
      duration: const Duration(milliseconds: 500),
      decoration: BoxDecoration(
        color: live ? scheme.primaryContainer : scheme.surfaceContainer,
        borderRadius: BorderRadius.circular(44),
      ),
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 26),
      child: Column(
        children: [
          LayoutBuilder(
            builder: (context, box) => ZedConnectCore(
              live: live,
              busy: busy,
              since: data.since,
              size: (box.maxWidth * 0.52).clamp(116.0, 208.0),
              onToggle: onToggle,
            ),
          ),
          const SizedBox(height: 16),
          AnimatedSwitcher(
            duration: const Duration(milliseconds: 280),
            reverseDuration: const Duration(milliseconds: 160),
            transitionBuilder: (child, animation) => FadeTransition(
              opacity: animation,
              child: SlideTransition(
                position:
                    Tween<Offset>(
                      begin: const Offset(0, 1 / 3),
                      end: Offset.zero,
                    ).animate(animation),
                child: child,
              ),
            ),
            child: AnimatedDefaultTextStyle(
              key: ValueKey(title),
              duration: const Duration(milliseconds: 500),
              style:
                  (theme.textTheme.headlineSmall ?? const TextStyle()).copyWith(
                    fontWeight: FontWeight.w800,
                    color: ink,
                  ),
              child: Text(title, textAlign: TextAlign.center),
            ),
          ),
          if (busy) ...[
            const SizedBox(height: 10),
            SizedBox(
              width: 156,
              child: LinearProgressIndicator(
                color: scheme.primary,
                backgroundColor: scheme.primary.withValues(alpha: 0.18),
              ),
            ),
            const SizedBox(height: 6),
            Text(
              l.zedTapCoreCancel,
              style: theme.textTheme.bodySmall?.copyWith(color: sub),
            ),
          ] else ...[
            const SizedBox(height: 4),
            Text(
              live ? l.zedTrafficProtected : l.zedTapCoreConnect,
              style: theme.textTheme.bodySmall?.copyWith(color: sub),
            ),
          ],
        ],
      ),
    );
  }
}

/// The fact chips: the place; while connected, the ping and the
/// accounts in use. Every chip toggles the details panel.
class _FactChips extends StatelessWidget {
  const _FactChips({required this.data, required this.onTap});

  final ZedHomeData data;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context);
    final place = data.place;
    return Wrap(
      alignment: WrapAlignment.center,
      spacing: 8,
      runSpacing: 8,
      children: [
        _FactChip(
          onTap: onTap,
          children: [
            if (place.flag.length == 2) ...[
              CountryFlag(code: place.flag, width: 20),
              const SizedBox(width: 6),
            ] else ...[
              const Icon(Icons.public_rounded, size: 16),
              const SizedBox(width: 6),
            ],
            Text(place.name, maxLines: 1, overflow: TextOverflow.ellipsis),
          ],
        ),
        if (data.live)
          _FactChip(
            onTap: onTap,
            children: [
              Text(
                data.pingMs == null
                    ? '${l.zedPing} …'
                    : '${data.pingMs} ms',
                maxLines: 1,
              ),
            ],
          ),
        if (data.live)
          _FactChip(
            onTap: onTap,
            children: [
              const Icon(Icons.account_circle_rounded, size: 16),
              const SizedBox(width: 6),
              Text('${data.ready}/${data.total}', maxLines: 1),
            ],
          ),
      ],
    );
  }
}

class _FactChip extends StatelessWidget {
  const _FactChip({required this.onTap, required this.children});

  final VoidCallback onTap;
  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final scheme = theme.colorScheme;
    return Material(
      color: scheme.surfaceContainerHigh,
      shape: const StadiumBorder(),
      child: InkWell(
        onTap: onTap,
        customBorder: const StadiumBorder(),
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
          child: DefaultTextStyle.merge(
            style: theme.textTheme.labelLarge?.copyWith(
              color: scheme.onSurfaceVariant,
            ),
            child: IconTheme.merge(
              data: IconThemeData(color: scheme.onSurfaceVariant),
              child: Row(mainAxisSize: MainAxisSize.min, children: children),
            ),
          ),
        ),
      ),
    );
  }
}

/// The connection details the chips open: location, ping, and every
/// account with what it is doing; the refresh button runs a new test.
class _DetailsPanel extends StatelessWidget {
  const _DetailsPanel({required this.data});

  final ZedHomeData data;

  String _accountValue(AppLocalizations l, Account a) {
    if (!a.enabled) return l.zedAccountOff;
    final usage = a.usage;
    if (usage != null &&
        !usage.unlimited &&
        usage.limitBytes > 0 &&
        usage.usedBytes >= usage.limitBytes) {
      return l.zedQuotaExhausted;
    }
    if (a.active && data.live) {
      final traffic = data.status.traffic;
      if (traffic != null) {
        return '↓ ${formatRate(traffic.downRate)} · ↑ ${formatRate(traffic.upRate)}';
      }
    }
    if (a.status == AccountStatus.ready) return l.statusActive;
    return accountReason(l, a, data.status.thresholdPercent) ?? l.zedIdle;
  }

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context);
    final theme = Theme.of(context);
    final scheme = theme.colorScheme;
    final accounts = data.accounts;
    Widget row(String label, String value) => Row(
      children: [
        Expanded(
          child: Text(
            label,
            style: theme.textTheme.bodySmall?.copyWith(
              color: scheme.onSurfaceVariant,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        Text(
          value,
          style: theme.textTheme.labelLarge,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ],
    );
    return Container(
      decoration: BoxDecoration(
        color: scheme.surfaceContainerHigh,
        borderRadius: BorderRadius.circular(24),
      ),
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Row(
            children: [
              Expanded(
                child: Text(
                  l.zedConnectionDetails,
                  style: theme.textTheme.titleSmall?.copyWith(
                    fontWeight: FontWeight.w700,
                  ),
                ),
              ),
              IconButton(
                onPressed: () =>
                    runShowingErrors(context, () => data.core.runTest()),
                iconSize: 18,
                icon: const Icon(Icons.refresh_rounded),
                color: scheme.onSurfaceVariant,
                tooltip: l.testConnection,
              ),
            ],
          ),
          const SizedBox(height: 8),
          row(l.homeLocation, data.place.name),
          const SizedBox(height: 8),
          row(
            l.zedPing,
            data.pingMs == null
                ? l.zedPingUnavailable
                : '${data.pingMs} ms',
          ),
          const SizedBox(height: 8),
          if (accounts.isEmpty)
            Text(
              l.waitingForAccount,
              style: theme.textTheme.bodySmall?.copyWith(
                color: scheme.onSurfaceVariant,
              ),
            )
          else
            for (final a in accounts) ...[
              row(a.email.isEmpty ? l.activeAccount : a.email, _accountValue(l, a)),
              const SizedBox(height: 8),
            ],
        ],
      ),
    );
  }
}