checkAndRefresh static method

Future<bool> checkAndRefresh({
  1. bool silent = false,
  2. bool once = true,
  3. bool bustUrl = false,
  4. String? title,
  5. String? message,
  6. String? confirmText,
  7. String? cancelText,
})

Checks the server and, when a newer build is deployed, reloads into it. Returns whether a refresh started.

silent refreshes without asking, otherwise the user confirms first. once keeps a build the user declined from being offered again, and keeps a build that was already reloaded for from being reloaded for a second time, which is what stops an endless auto-refresh when a misconfigured host keeps handing out the old files.

Implementation

static Future<bool> checkAndRefresh({
  bool silent = false,
  bool once = true,
  bool bustUrl = false,
  String? title,
  String? message,
  String? confirmText,
  String? cancelText,
}) async {
  if (!UApp.isWeb || _checking) return false;
  _checking = true;
  try {
    if (!await hasUpdate()) return false;
    final String signature = (await serverBuild())?.signature ?? "";
    final bool guarded = once && signature.isNotEmpty;
    if (guarded && ULocalStorage.getString(_refreshedBuildKey) == signature) return false;

    if (!silent) {
      if (guarded && ULocalStorage.getString(_declinedBuildKey) == signature) return false;
      // No navigator yet, so there is nobody to ask; leave the build untouched and retry later.
      if (navigatorKey.currentContext == null) return false;
      final bool accepted = await UNavigator.confirmAsync(
        title: title ?? U.s.updateAvailable,
        message: message ?? U.s.aNewVersionIsAvailableReloadToGetIt,
        confirmText: confirmText ?? U.s.refresh,
        cancelText: cancelText ?? U.s.later,
        icon: Icons.system_update_alt_rounded,
      );
      if (!accepted) {
        if (signature.isNotEmpty) ULocalStorage.set(_declinedBuildKey, signature);
        return false;
      }
    }

    if (signature.isNotEmpty) ULocalStorage.set(_refreshedBuildKey, signature);
    await refresh(bustUrl: bustUrl);
    return true;
  } finally {
    _checking = false;
  }
}