Files
minstrel/flutter_client/lib/update/client_update_provider.dart
T
bvandeusen ab8a86e794 fix(flutter): update banner false positive + lock-screen control routing
Update banner showing on identical versions:
- pubspec.yaml was stuck at the placeholder 0.1.0+1, so
  PackageInfo.version returned "0.1.0" while the server reported the
  actual release tag (e.g. "2026.05.10.1"). Comparison correctly said
  "newer" → banner always showed.
- Bump pubspec to 2026.05.11.0+1 so the local default matches the
  release cadence even before CI overrides it.
- Update flutter.yml release step to pass --build-name="${TAG#v}" so
  every tagged APK reports the tag as its PackageInfo.version. Future
  releases stop drifting from pubspec.
- Rewrite isVersionNewer to do component-wise int comparison with
  zero-padding: pub_semver.Version.parse rejects 4-part date versions
  like "2026.05.10.1", at which point the old code fell back to
  string inequality and treated "2026.05.10" as newer than itself
  vs "2026.05.10.0". Drop the pub_semver import (no longer used).

Lock-screen play/pause not responding:
- PlaybackState only listed MediaAction.seek in systemActions, which
  on Android 13+ means tapping the lock-screen play/pause button
  doesn't route back to the AudioHandler. Add play, pause,
  skipToNext, skipToPrevious to the set.
- Add androidCompactActionIndices: [0, 1, 2] so the compact
  notification view explicitly maps the three buttons.

Album art being smaller than the lock-screen frame is upstream of
this commit — the cover-cache writes whatever pixel dimensions the
server returns. If the server's /api/albums/<id>/cover returns small
thumbnails for these albums, the lock screen renders them at that
size. Worth a separate look at the server cover-emit path.
2026-05-11 10:27:04 -04:00

128 lines
4.6 KiB
Dart

import 'dart:async';
import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../library/library_providers.dart' show dioProvider;
import 'installer.dart';
import 'update_info.dart';
/// Tracks the bundled-server APK version vs. the installed app version.
/// Polls /api/client/version on startup + every 24h. Returns null when
/// no update is available (or the channel is unreachable / disabled).
///
/// Server returns 404 when no APK is bundled (dev environments, pre-CI
/// images) — we treat that as "no update channel" and stay silent.
class ClientUpdateController extends AsyncNotifier<UpdateInfo?> {
static const Duration _pollInterval = Duration(hours: 24);
Timer? _pollTimer;
@override
Future<UpdateInfo?> build() async {
ref.onDispose(() {
_pollTimer?.cancel();
_pollTimer = null;
});
_pollTimer ??= Timer.periodic(_pollInterval, (_) => ref.invalidateSelf());
return _check();
}
Future<UpdateInfo?> _check() async {
final dio = await ref.read(dioProvider.future);
final Response<Map<String, dynamic>> r;
try {
r = await dio.get<Map<String, dynamic>>('/api/client/version');
} on DioException catch (e) {
// 404 = no APK bundled; any other error = treat as silent.
if (e.response?.statusCode == 404) return null;
return null;
}
if (r.data == null) return null;
final info = UpdateInfo.fromJson(r.data!);
final installed = (await PackageInfo.fromPlatform()).version;
if (!isVersionNewer(info.version, installed)) return null;
return info;
}
}
/// True when `serverVersion` is strictly newer than `installedVersion`.
///
/// Comparison strategy: split both strings on `.`, parse each component
/// as an int (non-numeric = 0), pad the shorter list with zeros, then
/// compare component-wise. This handles our date-style versions
/// (2026.05.10.1) which exceed the 3-part semver shape that
/// `pub_semver.Version.parse` accepts, and treats "2026.05.10" as
/// equal to "2026.05.10.0" rather than "different = newer".
///
/// Falls back to pub_semver as a secondary attempt when the components
/// look semver-like (handles pre-release suffixes, build metadata, etc).
///
/// Exposed for testing; the polling logic in ClientUpdateController
/// is the only production caller.
bool isVersionNewer(String serverVersion, String installedVersion) {
List<int> parts(String v) => v
.replaceFirst(RegExp(r'^v'), '')
.split('.')
.map((p) => int.tryParse(p) ?? 0)
.toList();
final svr = parts(serverVersion);
final ins = parts(installedVersion);
final n = svr.length > ins.length ? svr.length : ins.length;
while (svr.length < n) svr.add(0);
while (ins.length < n) ins.add(0);
for (var i = 0; i < n; i++) {
if (svr[i] > ins[i]) return true;
if (svr[i] < ins[i]) return false;
}
return false;
}
final clientUpdateProvider =
AsyncNotifierProvider<ClientUpdateController, UpdateInfo?>(
ClientUpdateController.new);
/// In-memory dismissed-versions set. Keyed by version string so a
/// later release's banner re-appears even if the operator dismissed
/// the previous one. Not persisted — restart re-shows the banner,
/// which is acceptable nudging for v1.
class _DismissedVersionsNotifier extends Notifier<Set<String>> {
@override
Set<String> build() => <String>{};
void add(String version) {
state = {...state, version};
}
}
final _dismissedVersionsProvider =
NotifierProvider<_DismissedVersionsNotifier, Set<String>>(
_DismissedVersionsNotifier.new);
/// True when the update banner should render: an UpdateInfo is
/// available AND the operator hasn't dismissed this specific version.
final shouldShowUpdateBannerProvider = Provider<UpdateInfo?>((ref) {
final info = ref.watch(clientUpdateProvider).value;
if (info == null) return null;
final dismissed = ref.watch(_dismissedVersionsProvider);
if (dismissed.contains(info.version)) return null;
return info;
});
/// Dismiss controller: marks the given version as dismissed so the
/// banner stops showing for this session.
final dismissUpdateProvider = Provider<void Function(String)>((ref) {
return (version) =>
ref.read(_dismissedVersionsProvider.notifier).add(version);
});
/// Installer provider — depends on dio so the install download uses
/// the same authenticated client (though /api/client/apk is unauthed,
/// reusing dio keeps configuration consistent).
final updateInstallerProvider = FutureProvider<UpdateInstaller>((ref) async {
return UpdateInstaller(await ref.watch(dioProvider.future));
});