init method
Implementation
Future<void> init() async =>
await Hive.openBox<String>(_USER_TELEMETRY_BOX_NAME)
.then((Box<String> e) {
awardsTelemetryBox = e;
Debug().info(
"Loading Awards Telemetry. Box content is: ${awardsTelemetryBox.values.toString()}");
if (!awardsTelemetryBox.containsKey(awardsDBName)) {
Debug()
.warn("COULD NOT FIND USER_AWARDS, CREATING NEW MODEL");
reset();
save();
} else {
Debug().info("FOUND USER_AWARDS, LOADING MODEL");
_currentModel = AwardsModel.fromJson(
jsonDecode(awardsTelemetryBox.get(awardsDBName)!));
}
Debug().info(
"USER_AWARDS:\nUnlocked Awards: ${_currentModel.unlockedAwards.length}\nLocked Awards: ${_currentModel.lockedAwards.length}");
Debug().info(
"Validating loaded awards against ${Award.allAwards.length} registered awards");
if (Award.allAwards.isNotEmpty ||
(currentModel.unlockedAwards.isNotEmpty &&
currentModel.lockedAwards.isNotEmpty)) {
for (String unlocked in currentModel.unlockedAwards) {
if (Award.allAwards.entries
.toList()
.where(
(MapEntry<String, Award> e) => e.key == unlocked)
.isEmpty) {
Debug().warn(
"Award $unlocked is not registered, removing from unlocked list");
currentModel.unlockedAwards.remove(unlocked);
}
}
for (String locked in currentModel.lockedAwards) {
if (Award.allAwards.entries
.toList()
.where((MapEntry<String, Award> e) => e.key == locked)
.isEmpty) {
Debug().warn(
"Award $locked is not registered, removing from locked list");
currentModel.lockedAwards.remove(locked);
}
}
} else {
Debug().warn(
"Validation for awards is not possible. Skipping.");
}
Timer.periodic(
const Duration(seconds: Shared.USER_TELEMETRY_SAVE_CYCLE),
(Timer _) async => await save());
});