Files

369 lines
9.2 KiB
Dart
Raw Permalink Normal View History

2023-11-14 11:40:18 +08:00
import 'package:flutter_hbb/common.dart';
import 'package:get/get.dart';
import '../consts.dart';
2022-09-06 21:20:53 -07:00
// TODO: A lot of dup code.
class PrivacyModeState {
static String tag(String id) => 'privacy_mode_$id';
static void init(String id) {
2022-09-03 10:39:33 +08:00
final key = tag(id);
if (!Get.isRegistered<RxString>(tag: key)) {
2023-11-14 12:11:38 +08:00
final RxString state = ''.obs;
Get.put<RxString>(state, tag: key);
2022-09-03 10:39:33 +08:00
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxString>(tag: key)) {
Get.delete<RxString>(tag: key);
2023-03-03 11:51:33 +08:00
} else {
2023-11-14 12:11:38 +08:00
Get.find<RxString>(tag: key).value = '';
2022-09-03 10:39:33 +08:00
}
}
2023-11-14 12:11:38 +08:00
static RxString find(String id) => Get.find<RxString>(tag: tag(id));
}
class BlockInputState {
static String tag(String id) => 'block_input_$id';
static void init(String id) {
2022-09-03 10:39:33 +08:00
final key = tag(id);
if (!Get.isRegistered<RxBool>(tag: key)) {
2022-09-03 10:39:33 +08:00
final RxBool state = false.obs;
Get.put<RxBool>(state, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxBool>(tag: key).value = false;
2022-09-03 10:39:33 +08:00
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxBool>(tag: key)) {
Get.delete<RxBool>(tag: key);
2022-09-03 10:39:33 +08:00
}
}
static RxBool find(String id) => Get.find<RxBool>(tag: tag(id));
}
class CurrentDisplayState {
static String tag(String id) => 'current_display_$id';
static void init(String id) {
2022-09-03 10:39:33 +08:00
final key = tag(id);
if (!Get.isRegistered<RxInt>(tag: key)) {
2022-09-03 10:39:33 +08:00
final RxInt state = RxInt(0);
Get.put<RxInt>(state, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxInt>(tag: key).value = 0;
2022-09-03 10:39:33 +08:00
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxInt>(tag: key)) {
Get.delete<RxInt>(tag: key);
2022-09-03 10:39:33 +08:00
}
}
static RxInt find(String id) => Get.find<RxInt>(tag: tag(id));
}
class ConnectionType {
final Rx<String> _secure = kInvalidValueStr.obs;
final Rx<String> _direct = kInvalidValueStr.obs;
2025-08-11 16:13:31 +08:00
final Rx<String> _stream_type = kInvalidValueStr.obs;
Rx<String> get secure => _secure;
Rx<String> get direct => _direct;
2025-08-11 16:13:31 +08:00
Rx<String> get stream_type => _stream_type;
static String get strSecure => 'secure';
static String get strInsecure => 'insecure';
static String get strDirect => '';
static String get strIndirect => '_relay';
void setSecure(bool v) {
_secure.value = v ? strSecure : strInsecure;
}
void setDirect(bool v) {
_direct.value = v ? strDirect : strIndirect;
}
2025-08-11 16:13:31 +08:00
void setStreamType(String v) {
_stream_type.value = v;
}
bool isValid() {
return _secure.value != kInvalidValueStr &&
2025-08-11 16:13:31 +08:00
_direct.value != kInvalidValueStr &&
_stream_type.value != kInvalidValueStr;
}
}
class ConnectionTypeState {
static String tag(String id) => 'connection_type_$id';
static void init(String id) {
final key = tag(id);
if (!Get.isRegistered<ConnectionType>(tag: key)) {
final ConnectionType collectionType = ConnectionType();
Get.put<ConnectionType>(collectionType, tag: key);
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<ConnectionType>(tag: key)) {
Get.delete<ConnectionType>(tag: key);
}
}
static ConnectionType find(String id) =>
Get.find<ConnectionType>(tag: tag(id));
}
2022-09-03 10:39:33 +08:00
2023-04-19 14:39:22 +08:00
class FingerprintState {
static String tag(String id) => 'fingerprint_$id';
static void init(String id) {
final key = tag(id);
if (!Get.isRegistered<RxString>(tag: key)) {
2023-04-19 14:39:22 +08:00
final RxString state = ''.obs;
Get.put<RxString>(state, tag: key);
2023-04-19 14:39:22 +08:00
} else {
Get.find<RxString>(tag: key).value = '';
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxString>(tag: key)) {
Get.delete<RxString>(tag: key);
2023-04-19 14:39:22 +08:00
}
}
static RxString find(String id) => Get.find<RxString>(tag: tag(id));
}
2022-09-03 10:39:33 +08:00
class ShowRemoteCursorState {
static String tag(String id) => 'show_remote_cursor_$id';
static void init(String id) {
final key = tag(id);
if (!Get.isRegistered<RxBool>(tag: key)) {
2022-09-03 10:39:33 +08:00
final RxBool state = false.obs;
Get.put<RxBool>(state, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxBool>(tag: key).value = false;
2022-09-03 10:39:33 +08:00
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxBool>(tag: key)) {
Get.delete<RxBool>(tag: key);
2022-09-03 10:39:33 +08:00
}
}
static RxBool find(String id) => Get.find<RxBool>(tag: tag(id));
}
class ShowRemoteCursorLockState {
static String tag(String id) => 'show_remote_cursor_lock_$id';
static void init(String id) {
final key = tag(id);
if (!Get.isRegistered<RxBool>(tag: key)) {
final RxBool state = false.obs;
Get.put<RxBool>(state, tag: key);
} else {
Get.find<RxBool>(tag: key).value = false;
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxBool>(tag: key)) {
Get.delete<RxBool>(tag: key);
}
}
static RxBool find(String id) => Get.find<RxBool>(tag: tag(id));
}
2022-09-03 10:39:33 +08:00
class KeyboardEnabledState {
static String tag(String id) => 'keyboard_enabled_$id';
static void init(String id) {
final key = tag(id);
if (!Get.isRegistered<RxBool>(tag: key)) {
2022-09-03 10:39:33 +08:00
// Server side, default true
final RxBool state = true.obs;
Get.put<RxBool>(state, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxBool>(tag: key).value = true;
2022-09-03 10:39:33 +08:00
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxBool>(tag: key)) {
Get.delete<RxBool>(tag: key);
2022-09-03 10:39:33 +08:00
}
}
static RxBool find(String id) => Get.find<RxBool>(tag: tag(id));
}
2022-09-06 21:20:53 -07:00
class RemoteCursorMovedState {
static String tag(String id) => 'remote_cursor_moved_$id';
static void init(String id) {
final key = tag(id);
if (!Get.isRegistered<RxBool>(tag: key)) {
2022-09-06 21:20:53 -07:00
final RxBool state = false.obs;
Get.put<RxBool>(state, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxBool>(tag: key).value = false;
2022-09-06 21:20:53 -07:00
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxBool>(tag: key)) {
Get.delete<RxBool>(tag: key);
2022-09-06 21:20:53 -07:00
}
}
static RxBool find(String id) => Get.find<RxBool>(tag: tag(id));
}
2022-10-08 17:27:30 +08:00
class RemoteCountState {
static String tag() => 'remote_count_';
static void init() {
final key = tag();
if (!Get.isRegistered<RxInt>(tag: key)) {
2022-10-08 17:27:30 +08:00
final RxInt state = 1.obs;
Get.put<RxInt>(state, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxInt>(tag: key).value = 1;
2022-10-08 17:27:30 +08:00
}
}
static void delete() {
final key = tag();
if (Get.isRegistered<RxInt>(tag: key)) {
Get.delete<RxInt>(tag: key);
2022-10-08 17:27:30 +08:00
}
}
static RxInt find() => Get.find<RxInt>(tag: tag());
}
2022-11-12 22:33:10 +08:00
class PeerBoolOption {
static String tag(String id, String opt) => 'peer_{$opt}_$id';
static void init(String id, String opt, bool Function() init_getter) {
final key = tag(id, opt);
if (!Get.isRegistered<RxBool>(tag: key)) {
final RxBool value = RxBool(init_getter());
Get.put<RxBool>(value, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxBool>(tag: key).value = init_getter();
}
}
static void delete(String id, String opt) {
final key = tag(id, opt);
if (Get.isRegistered<RxBool>(tag: key)) {
Get.delete<RxBool>(tag: key);
}
}
static RxBool find(String id, String opt) =>
Get.find<RxBool>(tag: tag(id, opt));
}
2022-11-12 22:33:10 +08:00
class PeerStringOption {
static String tag(String id, String opt) => 'peer_{$opt}_$id';
static void init(String id, String opt, String Function() init_getter) {
final key = tag(id, opt);
if (!Get.isRegistered<RxString>(tag: key)) {
2022-11-12 22:33:10 +08:00
final RxString value = RxString(init_getter());
Get.put<RxString>(value, tag: key);
2023-03-03 11:51:33 +08:00
} else {
Get.find<RxString>(tag: key).value = init_getter();
2022-11-12 22:33:10 +08:00
}
}
static void delete(String id, String opt) {
final key = tag(id, opt);
if (Get.isRegistered<RxString>(tag: key)) {
Get.delete<RxString>(tag: key);
2022-11-12 22:33:10 +08:00
}
}
static RxString find(String id, String opt) =>
Get.find<RxString>(tag: tag(id, opt));
}
2023-04-12 09:41:13 +08:00
class UnreadChatCountState {
static String tag(id) => 'unread_chat_count_$id';
static void init(String id) {
final key = tag(id);
if (!Get.isRegistered<RxInt>(tag: key)) {
final RxInt state = RxInt(0);
Get.put<RxInt>(state, tag: key);
} else {
Get.find<RxInt>(tag: key).value = 0;
}
}
static void delete(String id) {
final key = tag(id);
if (Get.isRegistered<RxInt>(tag: key)) {
Get.delete<RxInt>(tag: key);
}
}
static RxInt find(String id) => Get.find<RxInt>(tag: tag(id));
}
2023-04-12 09:41:13 +08:00
initSharedStates(String id) {
PrivacyModeState.init(id);
BlockInputState.init(id);
CurrentDisplayState.init(id);
KeyboardEnabledState.init(id);
ShowRemoteCursorState.init(id);
ShowRemoteCursorLockState.init(id);
2023-04-12 09:41:13 +08:00
RemoteCursorMovedState.init(id);
2023-04-19 14:39:22 +08:00
FingerprintState.init(id);
PeerBoolOption.init(id, kOptionZoomCursor, () => false);
UnreadChatCountState.init(id);
2023-11-14 11:40:18 +08:00
if (isMobile) ConnectionTypeState.init(id); // desktop in other places
2023-04-12 09:41:13 +08:00
}
removeSharedStates(String id) {
PrivacyModeState.delete(id);
BlockInputState.delete(id);
CurrentDisplayState.delete(id);
ShowRemoteCursorState.delete(id);
ShowRemoteCursorLockState.delete(id);
2023-04-12 09:41:13 +08:00
KeyboardEnabledState.delete(id);
RemoteCursorMovedState.delete(id);
2023-04-19 14:39:22 +08:00
FingerprintState.delete(id);
PeerBoolOption.delete(id, kOptionZoomCursor);
UnreadChatCountState.delete(id);
2023-11-14 11:40:18 +08:00
if (isMobile) ConnectionTypeState.delete(id);
2023-04-12 09:41:13 +08:00
}