checkInactivityTime function

UserInactivityStatus checkInactivityTime(
  1. String? inActivitySetInTime,
  2. String? expiresAt
)

check inactivity time if inactivity period is less than an half an hour --- just resume if inactivity time is greater than 1 and less than 12 hours --- require pin if inactivity period is greater than 12 hours --- require login inActivitySetInTime represents the last time the user was active in the app. when user logs in or creates an account, this value is set to the current timestamp. It's is then update every often when the app moves back to the foreground.

Implementation

UserInactivityStatus checkInactivityTime(
  String? inActivitySetInTime,
  String? expiresAt,
) {
  if (inActivitySetInTime == null) {
    return UserInactivityStatus.okay;
  }

  final DateTime? lastActivityTime = DateTime.tryParse(inActivitySetInTime);
  if (lastActivityTime == null) {
    // we can't determine last activity time, so login is required
    return UserInactivityStatus.requiresLogin;
  }

  final int timeDiff = DateTime.now().difference(lastActivityTime).inHours;

  if (timeDiff < 1) {
    // check if token has expired or is about to and require pin if so
    lastActivityTime.add(Duration(seconds: int.tryParse(expiresAt!)!));

    final int tokenAge = DateTime.now()
        .difference(
            lastActivityTime.add(Duration(seconds: int.tryParse(expiresAt)!)))
        .inMinutes;

    // require pin login if token is about to expire
    if (tokenAge > -10) {
      return UserInactivityStatus.requiresPin;
    }

    return UserInactivityStatus.okay;
  }

  if (timeDiff > 1 && timeDiff < 12) {
    return UserInactivityStatus.requiresPin;
  }

  return UserInactivityStatus.requiresLogin;
}