checkAndShow static method

Future<void> checkAndShow(
  1. UUpdateResponse serverData,
  2. VoidCallback onSkipOrNotAvailable
)

Implementation

static Future<void> checkAndShow(
  UUpdateResponse serverData,
  VoidCallback onSkipOrNotAvailable,
) async {
  final Os? info = _platformUpdate(serverData);
  if (info == null) {
    onSkipOrNotAvailable();
    return;
  }

  final UpdateType type = await _checkUpdate(info);

  if (type == UpdateType.none) {
    onSkipOrNotAvailable();
    return;
  }

  if (type == UpdateType.optional) {
    final int? skipped = ULocalStorage.getInt(_skipKey);
    if (skipped == info.current) {
      onSkipOrNotAvailable();
      return;
    }
  }

  await showDialog(
    barrierDismissible: type == UpdateType.optional,
    context: navigatorKey.currentContext!,
    builder: (_) => PopScope(
      canPop: type == UpdateType.optional,
      child: AlertDialog(
        title: Text(type == UpdateType.force ? "Update Required" : "Update Available"),
        content: UColumn(
          width: 200,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              type == UpdateType.force ? "You must update to continue using the app." : "A newer version of the app is available.",
            ),
            if (info.link1 != null && info.link1Title != null)
              UButton(
                width: MediaQuery.sizeOf(navigatorKey.currentContext!).width,
                onTap: () => ULaunch.launchURL(info.link1!),
                title: info.link1Title ?? "---",
              ).pOnly(top: 8),
            if (info.link2 != null && info.link2Title != null)
              UButton(
                width: MediaQuery.sizeOf(navigatorKey.currentContext!).width,
                onTap: () => ULaunch.launchURL(info.link2!),
                title: info.link2Title ?? "",
              ).pOnly(top: 8),
            if (type == UpdateType.optional)
              UButton(
                width: MediaQuery.sizeOf(navigatorKey.currentContext!).width,
                type: UButtonType.text,
                textStyle: Theme.of(navigatorKey.currentContext!).textTheme.bodyMedium!.copyWith(color: Colors.red),
                onTap: () => exit(0),
                title: "Exit",
              ).pOnly(top: 8)
            else
              UButton(
                width: MediaQuery.sizeOf(navigatorKey.currentContext!).width,
                type: UButtonType.text,
                title: "Later",
                onTap: () {
                  if (info.current != null) {
                    ULocalStorage.set(_skipKey, info.current);
                  }
                  UNavigator.back();
                  onSkipOrNotAvailable();
                },
              ).pOnly(top: 8),
          ],
        ),
      ),
    ),
  );
}