saveStateInSharedPreferences method
Save the state of the app in shared preferences, with encryption if required.
Implementation
Future<bool> saveStateInSharedPreferences() async {
try {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
String result = '';
if (encryptionKey != null && encryptionIV != null) {
final _salsaKEY = encrypt.Key.fromUtf8(encryptionKey!);
final _salsaIV = encrypt.IV.fromUtf8(encryptionIV!);
/// Encrypter to protected saved states of the game and
/// make hacking a lil bit harder.
final _encrypter = encrypt.Encrypter(encrypt.Salsa20(_salsaKEY));
final plainText = this.getAppStateAsJson() ?? '';
result = _encrypter.encrypt(plainText, iv: _salsaIV).base64;
} else
result = this.getAppStateAsJson() ?? '';
return await prefs.setString('state', result);
} catch (e) {
return false;
}
}