mirror of https://github.com/immich-app/immich.git
feat(mobile): Auto switching server URLs (#14437)
parent
3c38851d50
commit
055f1fc72f
@ -1,5 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict/>
|
||||
<dict>
|
||||
<key>com.apple.developer.networking.wifi-info</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@ -1,5 +1,11 @@
|
||||
import 'package:immich_mobile/interfaces/database.interface.dart';
|
||||
import 'package:immich_mobile/models/auth/auxilary_endpoint.model.dart';
|
||||
|
||||
abstract interface class IAuthRepository implements IDatabaseRepository {
|
||||
Future<void> clearLocalData();
|
||||
String getAccessToken();
|
||||
bool getEndpointSwitchingFeature();
|
||||
String? getPreferredWifiName();
|
||||
String? getLocalEndpoint();
|
||||
List<AuxilaryEndpoint> getExternalEndpointList();
|
||||
}
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
abstract interface class INetworkRepository {
|
||||
Future<String?> getWifiName();
|
||||
Future<String?> getWifiIp();
|
||||
}
|
||||
@ -0,0 +1,105 @@
|
||||
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
||||
import 'dart:convert';
|
||||
|
||||
class AuxilaryEndpoint {
|
||||
final String url;
|
||||
final AuxCheckStatus status;
|
||||
|
||||
AuxilaryEndpoint({
|
||||
required this.url,
|
||||
required this.status,
|
||||
});
|
||||
|
||||
AuxilaryEndpoint copyWith({
|
||||
String? url,
|
||||
AuxCheckStatus? status,
|
||||
}) {
|
||||
return AuxilaryEndpoint(
|
||||
url: url ?? this.url,
|
||||
status: status ?? this.status,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => 'AuxilaryEndpoint(url: $url, status: $status)';
|
||||
|
||||
@override
|
||||
bool operator ==(covariant AuxilaryEndpoint other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other.url == url && other.status == status;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => url.hashCode ^ status.hashCode;
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return <String, dynamic>{
|
||||
'url': url,
|
||||
'status': status.toMap(),
|
||||
};
|
||||
}
|
||||
|
||||
factory AuxilaryEndpoint.fromMap(Map<String, dynamic> map) {
|
||||
return AuxilaryEndpoint(
|
||||
url: map['url'] as String,
|
||||
status: AuxCheckStatus.fromMap(map['status'] as Map<String, dynamic>),
|
||||
);
|
||||
}
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory AuxilaryEndpoint.fromJson(String source) =>
|
||||
AuxilaryEndpoint.fromMap(json.decode(source) as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
class AuxCheckStatus {
|
||||
final String name;
|
||||
AuxCheckStatus({
|
||||
required this.name,
|
||||
});
|
||||
const AuxCheckStatus._(this.name);
|
||||
|
||||
static const loading = AuxCheckStatus._('loading');
|
||||
static const valid = AuxCheckStatus._('valid');
|
||||
static const error = AuxCheckStatus._('error');
|
||||
static const unknown = AuxCheckStatus._('unknown');
|
||||
|
||||
@override
|
||||
bool operator ==(covariant AuxCheckStatus other) {
|
||||
if (identical(this, other)) return true;
|
||||
|
||||
return other.name == name;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => name.hashCode;
|
||||
|
||||
AuxCheckStatus copyWith({
|
||||
String? name,
|
||||
}) {
|
||||
return AuxCheckStatus(
|
||||
name: name ?? this.name,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return <String, dynamic>{
|
||||
'name': name,
|
||||
};
|
||||
}
|
||||
|
||||
factory AuxCheckStatus.fromMap(Map<String, dynamic> map) {
|
||||
return AuxCheckStatus(
|
||||
name: map['name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory AuxCheckStatus.fromJson(String source) =>
|
||||
AuxCheckStatus.fromMap(json.decode(source) as Map<String, dynamic>);
|
||||
|
||||
@override
|
||||
String toString() => 'AuxCheckStatus(name: $name)';
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/services/network.service.dart';
|
||||
|
||||
final networkProvider = StateNotifierProvider<NetworkNotifier, String>((ref) {
|
||||
return NetworkNotifier(
|
||||
ref.watch(networkServiceProvider),
|
||||
);
|
||||
});
|
||||
|
||||
class NetworkNotifier extends StateNotifier<String> {
|
||||
final NetworkService _networkService;
|
||||
|
||||
NetworkNotifier(this._networkService) : super('');
|
||||
|
||||
Future<String?> getWifiName() {
|
||||
return _networkService.getWifiName();
|
||||
}
|
||||
|
||||
Future<bool> getWifiReadPermission() {
|
||||
return _networkService.getLocationWhenInUserPermission();
|
||||
}
|
||||
|
||||
Future<bool> getWifiReadBackgroundPermission() {
|
||||
return _networkService.getLocationAlwaysPermission();
|
||||
}
|
||||
|
||||
Future<bool> requestWifiReadPermission() {
|
||||
return _networkService.requestLocationWhenInUsePermission();
|
||||
}
|
||||
|
||||
Future<bool> requestWifiReadBackgroundPermission() {
|
||||
return _networkService.requestLocationAlwaysPermission();
|
||||
}
|
||||
|
||||
Future<bool> openSettings() {
|
||||
return _networkService.openSettings();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/interfaces/network.interface.dart';
|
||||
import 'package:network_info_plus/network_info_plus.dart';
|
||||
|
||||
final networkRepositoryProvider = Provider((_) {
|
||||
final networkInfo = NetworkInfo();
|
||||
|
||||
return NetworkRepository(networkInfo);
|
||||
});
|
||||
|
||||
class NetworkRepository implements INetworkRepository {
|
||||
final NetworkInfo _networkInfo;
|
||||
|
||||
NetworkRepository(this._networkInfo);
|
||||
|
||||
@override
|
||||
Future<String?> getWifiName() {
|
||||
if (Platform.isAndroid) {
|
||||
// remove quote around the return value on Android
|
||||
// https://github.com/fluttercommunity/plus_plugins/tree/main/packages/network_info_plus/network_info_plus#android
|
||||
return _networkInfo.getWifiName().then((value) {
|
||||
if (value != null) {
|
||||
return value.replaceAll(RegExp(r'"'), '');
|
||||
}
|
||||
return value;
|
||||
});
|
||||
}
|
||||
return _networkInfo.getWifiName();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getWifiIp() {
|
||||
return _networkInfo.getWifiIP();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
final permissionRepositoryProvider = Provider((_) {
|
||||
return PermissionRepository();
|
||||
});
|
||||
|
||||
class PermissionRepository implements IPermissionRepository {
|
||||
PermissionRepository();
|
||||
|
||||
@override
|
||||
Future<bool> hasLocationWhenInUsePermission() {
|
||||
return Permission.locationWhenInUse.isGranted;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> requestLocationWhenInUsePermission() async {
|
||||
final result = await Permission.locationWhenInUse.request();
|
||||
return result.isGranted;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> hasLocationAlwaysPermission() {
|
||||
return Permission.locationAlways.isGranted;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> requestLocationAlwaysPermission() async {
|
||||
final result = await Permission.locationAlways.request();
|
||||
return result.isGranted;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> openSettings() {
|
||||
return openAppSettings();
|
||||
}
|
||||
}
|
||||
|
||||
abstract interface class IPermissionRepository {
|
||||
Future<bool> hasLocationWhenInUsePermission();
|
||||
Future<bool> requestLocationWhenInUsePermission();
|
||||
Future<bool> hasLocationAlwaysPermission();
|
||||
Future<bool> requestLocationAlwaysPermission();
|
||||
Future<bool> openSettings();
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/interfaces/network.interface.dart';
|
||||
import 'package:immich_mobile/repositories/network.repository.dart';
|
||||
import 'package:immich_mobile/repositories/permission.repository.dart';
|
||||
|
||||
final networkServiceProvider = Provider((ref) {
|
||||
return NetworkService(
|
||||
ref.watch(networkRepositoryProvider),
|
||||
ref.watch(permissionRepositoryProvider),
|
||||
);
|
||||
});
|
||||
|
||||
class NetworkService {
|
||||
final INetworkRepository _repository;
|
||||
final IPermissionRepository _permissionRepository;
|
||||
|
||||
NetworkService(this._repository, this._permissionRepository);
|
||||
|
||||
Future<bool> getLocationWhenInUserPermission() {
|
||||
return _permissionRepository.hasLocationWhenInUsePermission();
|
||||
}
|
||||
|
||||
Future<bool> requestLocationWhenInUsePermission() {
|
||||
return _permissionRepository.requestLocationWhenInUsePermission();
|
||||
}
|
||||
|
||||
Future<bool> getLocationAlwaysPermission() {
|
||||
return _permissionRepository.hasLocationAlwaysPermission();
|
||||
}
|
||||
|
||||
Future<bool> requestLocationAlwaysPermission() {
|
||||
return _permissionRepository.requestLocationAlwaysPermission();
|
||||
}
|
||||
|
||||
Future<String?> getWifiName() async {
|
||||
final canRead = await getLocationWhenInUserPermission();
|
||||
if (!canRead) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return await _repository.getWifiName();
|
||||
}
|
||||
|
||||
Future<bool> openSettings() {
|
||||
return _permissionRepository.openSettings();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,155 @@
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
||||
import 'package:immich_mobile/models/auth/auxilary_endpoint.model.dart';
|
||||
import 'package:immich_mobile/providers/auth.provider.dart';
|
||||
import 'package:immich_mobile/widgets/settings/networking_settings/networking_settings.dart';
|
||||
|
||||
class EndpointInput extends StatefulHookConsumerWidget {
|
||||
const EndpointInput({
|
||||
super.key,
|
||||
required this.initialValue,
|
||||
required this.index,
|
||||
required this.onValidated,
|
||||
required this.onDismissed,
|
||||
this.enabled = true,
|
||||
});
|
||||
|
||||
final AuxilaryEndpoint initialValue;
|
||||
final int index;
|
||||
final Function(String url, int index, AuxCheckStatus status) onValidated;
|
||||
final Function(int index) onDismissed;
|
||||
final bool enabled;
|
||||
|
||||
@override
|
||||
EndpointInputState createState() => EndpointInputState();
|
||||
}
|
||||
|
||||
class EndpointInputState extends ConsumerState<EndpointInput> {
|
||||
late final TextEditingController controller;
|
||||
late final FocusNode focusNode;
|
||||
late AuxCheckStatus auxCheckStatus;
|
||||
bool isInputValid = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
controller = TextEditingController(text: widget.initialValue.url);
|
||||
focusNode = FocusNode()..addListener(_onOutFocus);
|
||||
|
||||
setState(() {
|
||||
auxCheckStatus = widget.initialValue.status;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
focusNode.removeListener(_onOutFocus);
|
||||
focusNode.dispose();
|
||||
controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onOutFocus() {
|
||||
if (!focusNode.hasFocus && isInputValid) {
|
||||
validateAuxilaryServerUrl();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> validateAuxilaryServerUrl() async {
|
||||
final url = controller.text;
|
||||
setState(() => auxCheckStatus = AuxCheckStatus.loading);
|
||||
|
||||
final isValid =
|
||||
await ref.read(authProvider.notifier).validateAuxilaryServerUrl(url);
|
||||
|
||||
setState(() {
|
||||
if (mounted) {
|
||||
auxCheckStatus = isValid ? AuxCheckStatus.valid : AuxCheckStatus.error;
|
||||
}
|
||||
});
|
||||
|
||||
widget.onValidated(url, widget.index, auxCheckStatus);
|
||||
}
|
||||
|
||||
String? validateUrl(String? url) {
|
||||
try {
|
||||
if (url == null || url.isEmpty || !Uri.parse(url).isAbsolute) {
|
||||
isInputValid = false;
|
||||
return 'validate_endpoint_error'.tr();
|
||||
}
|
||||
} catch (_) {
|
||||
isInputValid = false;
|
||||
return 'validate_endpoint_error'.tr();
|
||||
}
|
||||
|
||||
isInputValid = true;
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dismissible(
|
||||
key: ValueKey(widget.index.toString()),
|
||||
direction: DismissDirection.endToStart,
|
||||
onDismissed: (_) => widget.onDismissed(widget.index),
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
alignment: Alignment.centerRight,
|
||||
padding: const EdgeInsets.only(right: 16),
|
||||
child: const Icon(
|
||||
Icons.delete,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
child: ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
trailing: ReorderableDragStartListener(
|
||||
enabled: widget.enabled,
|
||||
index: widget.index,
|
||||
child: const Icon(Icons.drag_handle_rounded),
|
||||
),
|
||||
leading: NetworkStatusIcon(
|
||||
key: ValueKey('status_$auxCheckStatus'),
|
||||
status: auxCheckStatus,
|
||||
enabled: widget.enabled,
|
||||
),
|
||||
subtitle: TextFormField(
|
||||
enabled: widget.enabled,
|
||||
onTapOutside: (_) => focusNode.unfocus(),
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
validator: validateUrl,
|
||||
keyboardType: TextInputType.url,
|
||||
style: const TextStyle(
|
||||
fontFamily: 'Inconsolata',
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 14,
|
||||
),
|
||||
decoration: InputDecoration(
|
||||
hintText: 'http(s)://immich.domain.com',
|
||||
contentPadding: const EdgeInsets.all(16),
|
||||
filled: true,
|
||||
fillColor: context.colorScheme.surfaceContainer,
|
||||
border: const OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(16)),
|
||||
),
|
||||
errorBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(color: Colors.red[300]!),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
),
|
||||
disabledBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color:
|
||||
context.isDarkTheme ? Colors.grey[900]! : Colors.grey[300]!,
|
||||
),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
),
|
||||
),
|
||||
controller: controller,
|
||||
focusNode: focusNode,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,189 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
||||
import 'package:immich_mobile/models/auth/auxilary_endpoint.model.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart' as db_store;
|
||||
import 'package:immich_mobile/widgets/settings/networking_settings/endpoint_input.dart';
|
||||
|
||||
class ExternalNetworkPreference extends HookConsumerWidget {
|
||||
const ExternalNetworkPreference({super.key, required this.enabled});
|
||||
|
||||
final bool enabled;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final entries =
|
||||
useState([AuxilaryEndpoint(url: '', status: AuxCheckStatus.unknown)]);
|
||||
final canSave = useState(false);
|
||||
|
||||
saveEndpointList() {
|
||||
canSave.value =
|
||||
entries.value.every((e) => e.status == AuxCheckStatus.valid);
|
||||
|
||||
final endpointList = entries.value
|
||||
.where((url) => url.status == AuxCheckStatus.valid)
|
||||
.toList();
|
||||
|
||||
final jsonString = jsonEncode(endpointList);
|
||||
|
||||
db_store.Store.put(
|
||||
db_store.StoreKey.externalEndpointList,
|
||||
jsonString,
|
||||
);
|
||||
}
|
||||
|
||||
updateValidationStatus(String url, int index, AuxCheckStatus status) {
|
||||
entries.value[index] =
|
||||
entries.value[index].copyWith(url: url, status: status);
|
||||
|
||||
saveEndpointList();
|
||||
}
|
||||
|
||||
handleReorder(int oldIndex, int newIndex) {
|
||||
if (oldIndex < newIndex) {
|
||||
newIndex -= 1;
|
||||
}
|
||||
|
||||
final entry = entries.value.removeAt(oldIndex);
|
||||
entries.value.insert(newIndex, entry);
|
||||
entries.value = [...entries.value];
|
||||
|
||||
saveEndpointList();
|
||||
}
|
||||
|
||||
handleDismiss(int index) {
|
||||
entries.value = [...entries.value..removeAt(index)];
|
||||
|
||||
saveEndpointList();
|
||||
}
|
||||
|
||||
Widget proxyDecorator(
|
||||
Widget child,
|
||||
int index,
|
||||
Animation<double> animation,
|
||||
) {
|
||||
return AnimatedBuilder(
|
||||
animation: animation,
|
||||
builder: (BuildContext context, Widget? child) {
|
||||
return Material(
|
||||
color: context.colorScheme.surfaceContainerHighest,
|
||||
shadowColor: context.colorScheme.primary.withOpacity(0.2),
|
||||
child: child,
|
||||
);
|
||||
},
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
useEffect(
|
||||
() {
|
||||
final jsonString =
|
||||
db_store.Store.tryGet(db_store.StoreKey.externalEndpointList);
|
||||
|
||||
if (jsonString == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<dynamic> jsonList = jsonDecode(jsonString);
|
||||
entries.value =
|
||||
jsonList.map((e) => AuxilaryEndpoint.fromJson(e)).toList();
|
||||
return null;
|
||||
},
|
||||
const [],
|
||||
);
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Container(
|
||||
clipBehavior: Clip.antiAlias,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
color: context.colorScheme.surfaceContainerLow,
|
||||
border: Border.all(
|
||||
color: context.colorScheme.surfaceContainerHighest,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
bottom: -36,
|
||||
right: -36,
|
||||
child: Icon(
|
||||
Icons.dns_rounded,
|
||||
size: 120,
|
||||
color: context.primaryColor.withOpacity(0.05),
|
||||
),
|
||||
),
|
||||
ListView(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
physics: const ClampingScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 4.0,
|
||||
horizontal: 24,
|
||||
),
|
||||
child: Text(
|
||||
"external_network_sheet_info".tr(),
|
||||
style: context.textTheme.bodyMedium,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Divider(color: context.colorScheme.surfaceContainerHighest),
|
||||
Form(
|
||||
key: GlobalKey<FormState>(),
|
||||
child: ReorderableListView.builder(
|
||||
buildDefaultDragHandles: false,
|
||||
proxyDecorator: proxyDecorator,
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
itemCount: entries.value.length,
|
||||
onReorder: handleReorder,
|
||||
itemBuilder: (context, index) {
|
||||
return EndpointInput(
|
||||
key: Key(index.toString()),
|
||||
index: index,
|
||||
initialValue: entries.value[index],
|
||||
onValidated: updateValidationStatus,
|
||||
onDismissed: handleDismiss,
|
||||
enabled: enabled,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24.0),
|
||||
child: SizedBox(
|
||||
height: 48,
|
||||
child: OutlinedButton.icon(
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text('add_endpoint'.tr().toUpperCase()),
|
||||
onPressed: enabled
|
||||
? () {
|
||||
entries.value = [
|
||||
...entries.value,
|
||||
AuxilaryEndpoint(
|
||||
url: '',
|
||||
status: AuxCheckStatus.unknown,
|
||||
),
|
||||
];
|
||||
}
|
||||
: null,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,256 @@
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
||||
import 'package:immich_mobile/providers/auth.provider.dart';
|
||||
import 'package:immich_mobile/providers/network.provider.dart';
|
||||
|
||||
class LocalNetworkPreference extends HookConsumerWidget {
|
||||
const LocalNetworkPreference({
|
||||
super.key,
|
||||
required this.enabled,
|
||||
});
|
||||
|
||||
final bool enabled;
|
||||
|
||||
Future<String?> _showEditDialog(
|
||||
BuildContext context,
|
||||
String title,
|
||||
String hintText,
|
||||
String initialValue,
|
||||
) {
|
||||
final controller = TextEditingController(text: initialValue);
|
||||
|
||||
return showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text(title),
|
||||
content: TextField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
decoration: InputDecoration(
|
||||
border: const OutlineInputBorder(),
|
||||
hintText: hintText,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text(
|
||||
'cancel'.tr().toUpperCase(),
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, controller.text),
|
||||
child: Text('save'.tr().toUpperCase()),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final wifiNameText = useState("");
|
||||
final localEndpointText = useState("");
|
||||
|
||||
useEffect(
|
||||
() {
|
||||
final wifiName = ref.read(authProvider.notifier).getSavedWifiName();
|
||||
final localEndpoint =
|
||||
ref.read(authProvider.notifier).getSavedLocalEndpoint();
|
||||
|
||||
if (wifiName != null) {
|
||||
wifiNameText.value = wifiName;
|
||||
}
|
||||
|
||||
if (localEndpoint != null) {
|
||||
localEndpointText.value = localEndpoint;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
saveWifiName(String wifiName) {
|
||||
wifiNameText.value = wifiName;
|
||||
return ref.read(authProvider.notifier).saveWifiName(wifiName);
|
||||
}
|
||||
|
||||
saveLocalEndpoint(String url) {
|
||||
localEndpointText.value = url;
|
||||
return ref.read(authProvider.notifier).saveLocalEndpoint(url);
|
||||
}
|
||||
|
||||
handleEditWifiName() async {
|
||||
final wifiName = await _showEditDialog(
|
||||
context,
|
||||
"wifi_name".tr(),
|
||||
"your_wifi_name".tr(),
|
||||
wifiNameText.value,
|
||||
);
|
||||
|
||||
if (wifiName != null) {
|
||||
await saveWifiName(wifiName);
|
||||
}
|
||||
}
|
||||
|
||||
handleEditServerEndpoint() async {
|
||||
final localEndpoint = await _showEditDialog(
|
||||
context,
|
||||
"server_endpoint".tr(),
|
||||
"http://local-ip:2283/api",
|
||||
localEndpointText.value,
|
||||
);
|
||||
|
||||
if (localEndpoint != null) {
|
||||
await saveLocalEndpoint(localEndpoint);
|
||||
}
|
||||
}
|
||||
|
||||
autofillCurrentNetwork() async {
|
||||
final wifiName = await ref.read(networkProvider.notifier).getWifiName();
|
||||
|
||||
if (wifiName == null) {
|
||||
context.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
"get_wifiname_error".tr(),
|
||||
style: context.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.w500,
|
||||
color: context.colorScheme.onSecondary,
|
||||
),
|
||||
),
|
||||
backgroundColor: context.colorScheme.secondary,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
saveWifiName(wifiName);
|
||||
}
|
||||
|
||||
final serverEndpoint =
|
||||
ref.read(authProvider.notifier).getServerEndpoint();
|
||||
|
||||
if (serverEndpoint != null) {
|
||||
saveLocalEndpoint(serverEndpoint);
|
||||
}
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0),
|
||||
child: Stack(
|
||||
children: [
|
||||
Container(
|
||||
clipBehavior: Clip.antiAlias,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
color: context.colorScheme.surfaceContainerLow,
|
||||
border: Border.all(
|
||||
color: context.colorScheme.surfaceContainerHighest,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned(
|
||||
bottom: -36,
|
||||
right: -36,
|
||||
child: Icon(
|
||||
Icons.home_outlined,
|
||||
size: 120,
|
||||
color: context.primaryColor.withOpacity(0.05),
|
||||
),
|
||||
),
|
||||
ListView(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||
physics: const ClampingScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 4.0,
|
||||
horizontal: 24,
|
||||
),
|
||||
child: Text(
|
||||
"local_network_sheet_info".tr(),
|
||||
style: context.textTheme.bodyMedium,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Divider(
|
||||
color: context.colorScheme.surfaceContainerHighest,
|
||||
),
|
||||
ListTile(
|
||||
enabled: enabled,
|
||||
contentPadding: const EdgeInsets.only(left: 24, right: 8),
|
||||
leading: const Icon(Icons.wifi_rounded),
|
||||
title: Text("wifi_name".tr()),
|
||||
subtitle: wifiNameText.value.isEmpty
|
||||
? Text("enter_wifi_name".tr())
|
||||
: Text(
|
||||
wifiNameText.value,
|
||||
style: context.textTheme.labelLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: enabled
|
||||
? context.primaryColor
|
||||
: context.colorScheme.onSurface
|
||||
.withAlpha(100),
|
||||
fontFamily: 'Inconsolata',
|
||||
),
|
||||
),
|
||||
trailing: IconButton(
|
||||
onPressed: enabled ? handleEditWifiName : null,
|
||||
icon: const Icon(Icons.edit_rounded),
|
||||
),
|
||||
),
|
||||
ListTile(
|
||||
enabled: enabled,
|
||||
contentPadding: const EdgeInsets.only(left: 24, right: 8),
|
||||
leading: const Icon(Icons.lan_rounded),
|
||||
title: Text("server_endpoint".tr()),
|
||||
subtitle: localEndpointText.value.isEmpty
|
||||
? const Text("http://local-ip:2283/api")
|
||||
: Text(
|
||||
localEndpointText.value,
|
||||
style: context.textTheme.labelLarge?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: enabled
|
||||
? context.primaryColor
|
||||
: context.colorScheme.onSurface
|
||||
.withAlpha(100),
|
||||
fontFamily: 'Inconsolata',
|
||||
),
|
||||
),
|
||||
trailing: IconButton(
|
||||
onPressed: enabled ? handleEditServerEndpoint : null,
|
||||
icon: const Icon(Icons.edit_rounded),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24.0,
|
||||
),
|
||||
child: SizedBox(
|
||||
height: 48,
|
||||
child: OutlinedButton.icon(
|
||||
icon: const Icon(Icons.wifi_find_rounded),
|
||||
label:
|
||||
Text('use_current_connection'.tr().toUpperCase()),
|
||||
onPressed: enabled ? autofillCurrentNetwork : null,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,266 @@
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/models/auth/auxilary_endpoint.model.dart';
|
||||
import 'package:immich_mobile/providers/network.provider.dart';
|
||||
import 'package:immich_mobile/services/app_settings.service.dart';
|
||||
import 'package:immich_mobile/utils/hooks/app_settings_update_hook.dart';
|
||||
import 'package:immich_mobile/widgets/settings/networking_settings/external_network_preference.dart';
|
||||
import 'package:immich_mobile/widgets/settings/networking_settings/local_network_preference.dart';
|
||||
import 'package:immich_mobile/widgets/settings/settings_switch_list_tile.dart';
|
||||
|
||||
import 'package:immich_mobile/entities/store.entity.dart' as db_store;
|
||||
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
||||
|
||||
class NetworkingSettings extends HookConsumerWidget {
|
||||
const NetworkingSettings({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final currentEndpoint =
|
||||
db_store.Store.get(db_store.StoreKey.serverEndpoint);
|
||||
final featureEnabled =
|
||||
useAppSettingsState(AppSettingsEnum.autoEndpointSwitching);
|
||||
|
||||
Future<void> checkWifiReadPermission() async {
|
||||
final [hasLocationInUse, hasLocationAlways] = await Future.wait([
|
||||
ref.read(networkProvider.notifier).getWifiReadPermission(),
|
||||
ref.read(networkProvider.notifier).getWifiReadBackgroundPermission(),
|
||||
]);
|
||||
|
||||
bool? isGrantLocationAlwaysPermission;
|
||||
|
||||
if (!hasLocationInUse) {
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return AlertDialog(
|
||||
title: Text("location_permission".tr()),
|
||||
content: Text("location_permission_content".tr()),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final isGrant = await ref
|
||||
.read(networkProvider.notifier)
|
||||
.requestWifiReadPermission();
|
||||
|
||||
Navigator.pop(context, isGrant);
|
||||
},
|
||||
child: Text("grant_permission".tr()),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (!hasLocationAlways) {
|
||||
isGrantLocationAlwaysPermission = await showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return AlertDialog(
|
||||
title: Text("background_location_permission".tr()),
|
||||
content: Text("background_location_permission_content".tr()),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final isGrant = await ref
|
||||
.read(networkProvider.notifier)
|
||||
.requestWifiReadBackgroundPermission();
|
||||
|
||||
Navigator.pop(context, isGrant);
|
||||
},
|
||||
child: Text("grant_permission".tr()),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (isGrantLocationAlwaysPermission != null &&
|
||||
!isGrantLocationAlwaysPermission) {
|
||||
await ref.read(networkProvider.notifier).openSettings();
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(
|
||||
() {
|
||||
if (featureEnabled.value == true) {
|
||||
checkWifiReadPermission();
|
||||
}
|
||||
return null;
|
||||
},
|
||||
[featureEnabled.value],
|
||||
);
|
||||
|
||||
return ListView(
|
||||
padding: const EdgeInsets.only(bottom: 96),
|
||||
physics: const ClampingScrollPhysics(),
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8, left: 16, bottom: 8),
|
||||
child: NetworkPreferenceTitle(
|
||||
title: "current_server_address".tr().toUpperCase(),
|
||||
icon: currentEndpoint.startsWith('https')
|
||||
? Icons.https_outlined
|
||||
: Icons.http_outlined,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Card(
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: const BorderRadius.all(Radius.circular(16)),
|
||||
side: BorderSide(
|
||||
color: context.colorScheme.surfaceContainerHighest,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
child: ListTile(
|
||||
leading:
|
||||
const Icon(Icons.check_circle_rounded, color: Colors.green),
|
||||
title: Text(
|
||||
currentEndpoint,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontFamily: 'Inconsolata',
|
||||
fontWeight: FontWeight.bold,
|
||||
color: context.primaryColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 10.0),
|
||||
child: Divider(
|
||||
color: context.colorScheme.surfaceContainerHighest,
|
||||
),
|
||||
),
|
||||
SettingsSwitchListTile(
|
||||
enabled: true,
|
||||
valueNotifier: featureEnabled,
|
||||
title: "automatic_endpoint_switching_title".tr(),
|
||||
subtitle: "automatic_endpoint_switching_subtitle".tr(),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8, left: 16, bottom: 16),
|
||||
child: NetworkPreferenceTitle(
|
||||
title: "local_network".tr().toUpperCase(),
|
||||
icon: Icons.home_outlined,
|
||||
),
|
||||
),
|
||||
LocalNetworkPreference(
|
||||
enabled: featureEnabled.value,
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 32, left: 16, bottom: 16),
|
||||
child: NetworkPreferenceTitle(
|
||||
title: "external_network".tr().toUpperCase(),
|
||||
icon: Icons.dns_outlined,
|
||||
),
|
||||
),
|
||||
ExternalNetworkPreference(
|
||||
enabled: featureEnabled.value,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NetworkPreferenceTitle extends StatelessWidget {
|
||||
const NetworkPreferenceTitle({
|
||||
super.key,
|
||||
required this.icon,
|
||||
required this.title,
|
||||
});
|
||||
|
||||
final IconData icon;
|
||||
final String title;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
Icon(
|
||||
icon,
|
||||
color: context.colorScheme.onSurface.withAlpha(150),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
title,
|
||||
style: context.textTheme.displaySmall?.copyWith(
|
||||
color: context.colorScheme.onSurface.withAlpha(200),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NetworkStatusIcon extends StatelessWidget {
|
||||
const NetworkStatusIcon({
|
||||
super.key,
|
||||
required this.status,
|
||||
this.enabled = true,
|
||||
}) : super();
|
||||
|
||||
final AuxCheckStatus status;
|
||||
final bool enabled;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: _buildIcon(context),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildIcon(BuildContext context) {
|
||||
switch (status) {
|
||||
case AuxCheckStatus.loading:
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(left: 4.0),
|
||||
child: SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(
|
||||
color: context.primaryColor,
|
||||
strokeWidth: 2,
|
||||
key: const ValueKey('loading'),
|
||||
),
|
||||
),
|
||||
);
|
||||
case AuxCheckStatus.valid:
|
||||
return enabled
|
||||
? const Icon(
|
||||
Icons.check_circle_rounded,
|
||||
color: Colors.green,
|
||||
key: ValueKey('success'),
|
||||
)
|
||||
: Icon(
|
||||
Icons.check_circle_rounded,
|
||||
color: context.colorScheme.onSurface.withAlpha(100),
|
||||
key: const ValueKey('success'),
|
||||
);
|
||||
case AuxCheckStatus.error:
|
||||
return enabled
|
||||
? const Icon(
|
||||
Icons.error_rounded,
|
||||
color: Colors.red,
|
||||
key: ValueKey('error'),
|
||||
)
|
||||
: const Icon(
|
||||
Icons.error_rounded,
|
||||
color: Colors.grey,
|
||||
key: ValueKey('error'),
|
||||
);
|
||||
default:
|
||||
return const Icon(Icons.circle_outlined, key: ValueKey('unknown'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
description: This file stores settings for Dart & Flutter DevTools.
|
||||
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
|
||||
extensions:
|
||||
Loading…
Reference in New Issue