checkAppStatus method

Future<OptimusAppStatus> checkAppStatus()

Checks if the app is newly installed or updated

Implementation

Future<OptimusAppStatus> checkAppStatus() async {
  // Initialize sesion
  final session = await InternalSessionUtils.instance.getOrCreateSession();
  if (session.freshInstall ?? false) {
    session.appVersion = null;
  }
  // Get the current version of the app
  final packageInfo = await PackageInfo.fromPlatform();
  final currentVersion = packageInfo.buildNumber;

  // Retrieve the stored version from localstore
  final lastVersion = session.appVersion;

  // Case 1: First time install (no version stored)
  if (lastVersion == null) {
    // Save the current version as the installed version
    session.appVersion = currentVersion;
    await InternalSessionUtils.instance.saveSession(session, merge: true);
    return OptimusAppStatus.install; // App is newly installed
  }

  // Case 2: Version has changed (update)
  if (lastVersion != currentVersion) {
    // Update the stored version
    session.appVersion = currentVersion;
    await InternalSessionUtils.instance.saveSession(session, merge: true);
    return OptimusAppStatus.update; // App is updated
  }

  // Case 3: Version has not changed (normal launch)
  return OptimusAppStatus.open; // App is launched normally
}