Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make KeyValueStore NNBD + add tryGet methods. #157

Merged
merged 16 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/lib/blocs/sharezone_bloc_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'package:flutter/material.dart';
import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik_lehrer.dart';
import 'package:hausaufgabenheft_logik/hausaufgabenheft_logik_setup.dart';
import 'package:http/http.dart' as http;
import 'package:key_value_store/in_memory_key_value_store.dart';
import 'package:sharezone/account/account_page_bloc_factory.dart';
import 'package:sharezone/account/features/feature_gateway.dart';
import 'package:sharezone/account/features/features_bloc.dart';
Expand Down
93 changes: 6 additions & 87 deletions app/lib/util/cache/key_value_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
// SPDX-License-Identifier: EUPL-1.2

// @dart=2.14
import 'dart:async';

import 'package:key_value_store/key_value_store.dart';
Expand Down Expand Up @@ -34,24 +35,24 @@ class FlutterKeyValueStore extends KeyValueStore {
Future<bool> clear() => preferences.clear();

@override
bool getBool(String key) => preferences.getBool(key);
bool? getBool(String key) => preferences.getBool(key);

@override
double getDouble(String key) => preferences.getDouble(key);
double? getDouble(String key) => preferences.getDouble(key);

/// Reads a value from persistent storage, throwing an exception if it's not an int.
@override
int getInt(String key) => preferences.getInt(key);
int? getInt(String key) => preferences.getInt(key);

/// Returns all keys in the persistent storage.
@override
Set<String> getKeys() => preferences.getKeys();

@override
String getString(String key) => preferences.getString(key);
String? getString(String key) => preferences.getString(key);

@override
List<String> getStringList(String key) => preferences.getStringList(key);
List<String>? getStringList(String key) => preferences.getStringList(key);

@override
Future<bool> remove(String key) => preferences.remove(key);
Expand Down Expand Up @@ -81,85 +82,3 @@ class FlutterKeyValueStore extends KeyValueStore {
@override
bool containsKey(String key) => preferences.containsKey(key);
}

class InMemoryKeyValueStore extends KeyValueStore {
Map<String, dynamic> storedValues;

InMemoryKeyValueStore([this.storedValues]) {
storedValues ??= {};
}

@override
Future<bool> clear() {
storedValues.clear();
return null;
}

@override
bool getBool(String key) {
return storedValues[key] as bool;
}

@override
double getDouble(String key) {
return storedValues[key] as double;
}

@override
int getInt(String key) {
return storedValues[key] as int;
}

@override
Set<String> getKeys() {
return storedValues.keys.toSet();
}

@override
String getString(String key) {
return storedValues[key] as String;
}

@override
List<String> getStringList(String key) {
return storedValues[key] as List<String>;
}

@override
Future<bool> remove(String key) {
return Future.value(storedValues.remove(key) != null);
}

@override
Future<bool> setBool(String key, bool value) {
storedValues[key] = value;
return Future.value(true);
}

@override
Future<bool> setDouble(String key, double value) {
storedValues[key] = value;
return Future.value(true);
}

@override
Future<bool> setInt(String key, int value) {
storedValues[key] = value;
return Future.value(true);
}

@override
Future<bool> setString(String key, String value) {
storedValues[key] = value;
return Future.value(true);
}

@override
Future<bool> setStringList(String key, List<String> values) {
storedValues[key] = values;
return Future.value(true);
}

@override
bool containsKey(String key) => storedValues.containsKey(key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:key_value_store/in_memory_key_value_store.dart';
import 'package:rxdart/subjects.dart';
import 'package:sharezone/navigation/scaffold/portable/bottom_navigation_bar/tutorial/bnb_tutorial_bloc.dart';
import 'package:sharezone/navigation/scaffold/portable/bottom_navigation_bar/tutorial/bnb_tutorial_cache.dart';
import 'package:sharezone/util/cache/key_value_store.dart';
import 'package:test/test.dart';

import 'mock_bnb_tutorial_analytics.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/test/feedback/feedback_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
// SPDX-License-Identifier: EUPL-1.2

import 'package:flutter_test/flutter_test.dart';
import 'package:key_value_store/in_memory_key_value_store.dart';
import 'package:key_value_store/key_value_store.dart';
import 'package:random_string/random_string.dart' as random;
import 'package:sharezone/feedback/src/analytics/feedback_analytics.dart';
import 'package:sharezone/feedback/src/bloc/feedback_bloc.dart';
import 'package:sharezone/feedback/src/cache/cooldown_exception.dart';
import 'package:sharezone/feedback/src/cache/feedback_cache.dart';
import 'package:sharezone/feedback/src/models/user_feedback.dart';
import 'package:sharezone/util/cache/key_value_store.dart';

import 'mock_feedback_api.dart';
import 'mock_platform_information_retreiver.dart';
Expand Down
4 changes: 2 additions & 2 deletions app/test/feedback/feedback_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//
// SPDX-License-Identifier: EUPL-1.2

import 'package:key_value_store/in_memory_key_value_store.dart';
import 'package:sharezone/feedback/src/cache/feedback_cache.dart';
import 'package:sharezone/util/cache/key_value_store.dart';
import 'package:test/test.dart';

void main() {
Expand Down Expand Up @@ -55,4 +55,4 @@ void main() {
expect(onCooldown, true);
});
});
}
}
2 changes: 1 addition & 1 deletion app/test/feedback/feedback_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import 'package:bloc_provider/bloc_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:key_value_store/in_memory_key_value_store.dart';
import 'package:random_string/random_string.dart' as random;
import 'package:sharezone/feedback/feedback_box_page.dart';
import 'package:sharezone/feedback/src/bloc/feedback_bloc.dart';
import 'package:sharezone/feedback/src/cache/cooldown_exception.dart';
import 'package:sharezone/feedback/src/cache/feedback_cache.dart';
import 'package:sharezone/feedback/src/models/user_feedback.dart';
import 'package:sharezone/util/cache/key_value_store.dart';

import 'feedback_bloc_test.dart';
import 'mock_feedback_api.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/test/holidays/holiday_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// SPDX-License-Identifier: EUPL-1.2

import 'package:built_collection/built_collection.dart';
import 'package:key_value_store/in_memory_key_value_store.dart';
import 'package:sharezone/models/extern_apis/holiday.dart';
import 'package:sharezone/util/cache/key_value_store.dart';
import 'package:sharezone/util/holidays/holiday_cache.dart';
import 'package:sharezone/util/holidays/state.dart';
import "package:test/test.dart";
Expand Down
2 changes: 1 addition & 1 deletion app/test/holidays/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import 'package:async/async.dart';
import 'package:http/http.dart' as http;
import 'package:key_value_store/in_memory_key_value_store.dart';
import 'package:mockito/mockito.dart';
import 'package:sharezone/blocs/dashbord_widgets_blocs/holiday_bloc.dart';
import 'package:sharezone/models/extern_apis/holiday.dart';
import 'package:sharezone/util/cache/key_value_store.dart';
import 'package:sharezone/util/holidays/api_cache_manager.dart';
import 'package:sharezone/util/holidays/holiday_api.dart';
import 'package:sharezone/util/holidays/holiday_cache.dart';
Expand Down
6 changes: 3 additions & 3 deletions lib/key_value_store/lib/in_memory_key_value_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import 'dart:async';
import 'package:key_value_store/key_value_store.dart';

class InMemoryKeyValueStore extends KeyValueStore {
Map<String, dynamic> storedValues;
late Map<String, dynamic> storedValues;

InMemoryKeyValueStore([this.storedValues]) {
storedValues ??= {};
InMemoryKeyValueStore([Map<String, dynamic>? storedValues]) {
this.storedValues = storedValues ?? {};
}

@override
Expand Down
56 changes: 37 additions & 19 deletions lib/key_value_store/lib/key_value_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,51 @@ abstract class KeyValueStore {
Set<String> getKeys();

/// Reads a value from persistent storage, throwing an exception if it's not a bool.
bool getBool(String key);
// Reads a value from persistent storage, throwing an exception if it's not an int.
int getInt(String key);
// Reads a value from persistent storage, throwing an exception if it's not a double.
double getDouble(String key);
// Reads a value from persistent storage, throwing an exception if it's not an String.
String getString(String key);
bool? getBool(String key);

/// Reads a value from persistent storage, returning null if it's not a bool.
bool? tryGetBool(String key) => _guard(() => getBool(key));

/// Reads a value from persistent storage, throwing an exception if it's not an int.
int? getInt(String key);

/// Reads a value from persistent storage, returning null if it's not an int.
int? tryGetInt(String key) => _guard(() => getInt(key));

/// Reads a value from persistent storage, throwing an exception if it's not a double.
double? getDouble(String key);

/// Reads a value from persistent storage, returning null if it's not a double.
double? tryGetDouble(String key) => _guard(() => getDouble(key));

/// Reads a value from persistent storage, throwing an exception if it's not an String.
String? getString(String key);

/// Reads a value from persistent storage, returning null if it's not an String.
String? tryGetString(String key) => _guard(() => getString(key));

/// Reads a set of string values from persistent storage, throwing an exception if it's not a string set.
List<String> getStringList(String key);
List<String>? getStringList(String key);

/// Reads a set of string values from persistent storage, returning null if it's not a string set.
List<String>? tryGetStringList(String key) =>
_guard(() => getStringList(key));

/// Saves a boolean [value] to persistent storage in the background.
///
/// If [value] is null, this is equivalent to calling [remove()] on the [key].
Future<bool> setBool(String key, bool value);

/// Reads a value from persistent storage, throwing an exception if it's not
/// an int.
/// Saves an int [value] to persistent storage in the background.
Future<bool> setInt(String key, int value);

/// Saves a double [value] to persistent storage in the background.
///
/// Android doesn't support storing doubles, so it will be stored as a float.
///
/// If [value] is null, this is equivalent to calling [remove()] on the [key].
Future<bool> setDouble(String key, double value);

/// Saves a string [value] to persistent storage in the background.
///
/// If [value] is null, this is equivalent to calling [remove()] on the [key].
Future<bool> setString(String key, String value);

/// Saves a list of strings [value] to persistent storage in the background.
///
/// If [value] is null, this is equivalent to calling [remove()] on the [key].
Future<bool> setStringList(String key, List<String> values);

/// Removes an entry from persistent storage.
Expand All @@ -56,4 +66,12 @@ abstract class KeyValueStore {

/// Completes with true once the user preferences for the app has been cleared
Future<bool> clear();
}
}

T? _guard<T>(T? Function() f) {
try {
return f();
} catch (_) {
return null;
}
}
Loading