2022-10-04 13:12:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-03-09 19:47:50 +03:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-11-19 13:10:00 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2021-11-19 15:02:25 +03:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-11-19 13:10:00 +03:00
|
|
|
|
2023-05-03 22:20:08 +03:00
|
|
|
import '../app/models.dart';
|
|
|
|
|
2023-03-03 18:29:24 +03:00
|
|
|
bool get isDesktop {
|
|
|
|
return const [
|
|
|
|
TargetPlatform.windows,
|
|
|
|
TargetPlatform.macOS,
|
|
|
|
TargetPlatform.linux
|
|
|
|
].contains(defaultTargetPlatform);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get isAndroid {
|
|
|
|
return defaultTargetPlatform == TargetPlatform.android;
|
|
|
|
}
|
2022-03-09 19:47:50 +03:00
|
|
|
|
2021-11-19 15:02:25 +03:00
|
|
|
// This must be initialized before use, in main.dart.
|
|
|
|
final prefProvider = Provider<SharedPreferences>((ref) {
|
|
|
|
throw UnimplementedError();
|
|
|
|
});
|
|
|
|
|
2022-03-09 19:47:50 +03:00
|
|
|
abstract class ApplicationStateNotifier<T>
|
2023-05-03 22:20:08 +03:00
|
|
|
extends AutoDisposeFamilyAsyncNotifier<T, DevicePath> {
|
|
|
|
ApplicationStateNotifier() : super();
|
2022-03-09 19:47:50 +03:00
|
|
|
|
|
|
|
@protected
|
2022-03-24 00:55:18 +03:00
|
|
|
Future<void> updateState(Future<T> Function() guarded) async {
|
2023-05-03 22:20:08 +03:00
|
|
|
state = await AsyncValue.guard(guarded);
|
2022-03-09 19:47:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
2022-03-24 00:55:18 +03:00
|
|
|
void setData(T value) {
|
2023-05-03 22:20:08 +03:00
|
|
|
state = AsyncValue.data(value);
|
2022-03-09 19:47:50 +03:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 15:12:11 +03:00
|
|
|
|
|
|
|
// Feature flags
|
2023-10-04 12:08:02 +03:00
|
|
|
sealed class BaseFeature {
|
2023-09-29 15:12:11 +03:00
|
|
|
String get path;
|
2023-10-04 12:08:02 +03:00
|
|
|
String _subpath(String key);
|
2023-09-29 15:12:11 +03:00
|
|
|
|
|
|
|
Feature feature(String key, {bool enabled = true}) =>
|
2023-10-04 12:08:02 +03:00
|
|
|
Feature._(this, key, enabled: enabled);
|
2023-09-29 15:12:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class _RootFeature extends BaseFeature {
|
|
|
|
_RootFeature._();
|
|
|
|
@override
|
|
|
|
String get path => '';
|
2023-10-04 12:08:02 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
String _subpath(String key) => key;
|
2023-09-29 15:12:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class Feature extends BaseFeature {
|
|
|
|
final BaseFeature parent;
|
|
|
|
final String key;
|
|
|
|
final bool _defaultState;
|
|
|
|
|
2023-10-04 12:08:02 +03:00
|
|
|
Feature._(this.parent, this.key, {bool enabled = true})
|
2023-09-29 15:12:11 +03:00
|
|
|
: _defaultState = enabled;
|
|
|
|
|
|
|
|
@override
|
2023-10-04 12:08:02 +03:00
|
|
|
String get path => parent._subpath(key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
String _subpath(String key) => '$path.$key';
|
2023-09-29 15:12:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
final BaseFeature root = _RootFeature._();
|
|
|
|
|
|
|
|
typedef FeatureProvider = bool Function(Feature feature);
|
|
|
|
|
2023-10-04 12:08:02 +03:00
|
|
|
final featureFlagProvider =
|
|
|
|
StateNotifierProvider<FeatureFlagsNotifier, Map<String, bool>>(
|
|
|
|
(_) => FeatureFlagsNotifier());
|
|
|
|
|
|
|
|
class FeatureFlagsNotifier extends StateNotifier<Map<String, bool>> {
|
|
|
|
FeatureFlagsNotifier() : super({});
|
|
|
|
|
|
|
|
void loadConfig(Map<String, dynamic> config) {
|
|
|
|
const falsey = [0, false, null];
|
|
|
|
state = {for (final k in config.keys) k: !falsey.contains(config[k])};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 15:12:11 +03:00
|
|
|
final featureProvider = Provider<FeatureProvider>((ref) {
|
2023-10-04 12:08:02 +03:00
|
|
|
final featureMap = ref.watch(featureFlagProvider);
|
|
|
|
|
|
|
|
bool isEnabled(BaseFeature feature) => switch (feature) {
|
|
|
|
_RootFeature() => true,
|
|
|
|
Feature() => isEnabled(feature.parent) &&
|
|
|
|
(featureMap[feature.path] ?? feature._defaultState),
|
|
|
|
};
|
|
|
|
|
|
|
|
return isEnabled;
|
2023-09-29 15:12:11 +03:00
|
|
|
});
|