track method
Start tracking versions and builds
Implementation
Future<void> track(
{int? versionHistoryMaxLength, int? buildHistoryMaxLength}) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
Map<String, List<String?>> historyTrail = Map<String, List<String?>>();
_isFirstLaunchEver = !sharedPreferences.containsKey(_versionsKey) ||
!sharedPreferences.containsKey(_buildsKey);
if (_isFirstLaunchEver!) {
historyTrail.addAll({_versionsKey: [], _buildsKey: []});
} else {
historyTrail.addAll({
_versionsKey: _readHistory(sharedPreferences, _versionsKey).toList(),
_buildsKey: _readHistory(sharedPreferences, _buildsKey).toList()
});
}
// Handle versions
_currentVersion = packageInfo.version;
_isFirstLaunchForCurrentVersion =
!historyTrail[_versionsKey]!.contains(_currentVersion);
if (_isFirstLaunchForCurrentVersion!)
historyTrail[_versionsKey]!.add(_currentVersion);
if (versionHistoryMaxLength != null && versionHistoryMaxLength > 0) {
var versionsToRemove =
historyTrail[_versionsKey]!.length - versionHistoryMaxLength;
if (versionsToRemove > 0)
historyTrail[_versionsKey]!.removeRange(1, versionsToRemove + 1);
}
_previousVersion = _getPrevious(historyTrail, _versionsKey);
_firstInstalledVersion = historyTrail[_versionsKey]!.first;
_versionHistory = historyTrail[_versionsKey]!.toList();
// Handle builds
_currentBuild = packageInfo.buildNumber;
_isFirstLaunchForCurrentBuild =
!historyTrail[_buildsKey]!.contains(_currentBuild);
if (_isFirstLaunchForCurrentBuild!)
historyTrail[_buildsKey]!.add(_currentBuild);
if (buildHistoryMaxLength != null && buildHistoryMaxLength > 0) {
var buildsToRemove =
historyTrail[_buildsKey]!.length - buildHistoryMaxLength;
if (buildsToRemove > 0)
historyTrail[_buildsKey]!.removeRange(1, buildsToRemove + 1);
}
if (_isFirstLaunchForCurrentVersion! || _isFirstLaunchForCurrentBuild!) {
_writeHistory(
sharedPreferences, _versionsKey, historyTrail[_versionsKey]!);
_writeHistory(sharedPreferences, _buildsKey, historyTrail[_buildsKey]!);
}
_previousBuild = _getPrevious(historyTrail, _buildsKey);
_firstInstalledBuild = historyTrail[_buildsKey]!.first;
_buildHistory = historyTrail[_buildsKey]!.toList();
}