Merge pull request 'feat(platform): Implement custom platform-dependent storage path definition' (#240) from platform-path into master

Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy.org.app/pulls/240
Reviewed-by: Inex Code <inex.code@selfprivacy.org>
This commit is contained in:
NaiJi ✨ 2023-07-28 00:24:40 +03:00
commit 06fc2935a8
4 changed files with 73 additions and 41 deletions

View file

@ -8,10 +8,13 @@ import 'package:selfprivacy/logic/models/hive/backups_credential.dart';
import 'package:selfprivacy/logic/models/hive/server_details.dart';
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
import 'package:selfprivacy/logic/models/hive/user.dart';
import 'package:selfprivacy/utils/platform_adapter.dart';
class HiveConfig {
static Future<void> init() async {
await Hive.initFlutter();
final String? storagePath = PlatformAdapter.storagePath;
print('HiveConfig: Custom storage path: $storagePath');
await Hive.initFlutter(storagePath);
Hive.registerAdapter(UserAdapter());
Hive.registerAdapter(ServerHostingDetailsAdapter());
Hive.registerAdapter(ServerDomainAdapter());

View file

@ -1,9 +1,7 @@
import 'dart:async';
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:selfprivacy/config/get_it_config.dart';
@ -22,6 +20,7 @@ import 'package:selfprivacy/logic/models/server_basic_info.dart';
import 'package:selfprivacy/logic/models/server_type.dart';
import 'package:selfprivacy/logic/providers/providers_controller.dart';
import 'package:selfprivacy/utils/network_utils.dart';
import 'package:selfprivacy/utils/platform_adapter.dart';
class IpNotFoundException implements Exception {
IpNotFoundException(this.message);
@ -308,40 +307,6 @@ class ServerInstallationRepository {
return domain!;
}
Future<String> getDeviceName() async {
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (kIsWeb) {
return deviceInfo.webBrowserInfo.then(
(final WebBrowserInfo value) =>
'${value.browserName} ${value.platform}',
);
} else {
if (Platform.isAndroid) {
return deviceInfo.androidInfo.then(
(final AndroidDeviceInfo value) =>
'${value.model} ${value.version.release}',
);
} else if (Platform.isIOS) {
return deviceInfo.iosInfo.then(
(final IosDeviceInfo value) =>
'${value.utsname.machine} ${value.systemName} ${value.systemVersion}',
);
} else if (Platform.isLinux) {
return deviceInfo.linuxInfo
.then((final LinuxDeviceInfo value) => value.prettyName);
} else if (Platform.isMacOS) {
return deviceInfo.macOsInfo.then(
(final MacOsDeviceInfo value) =>
'${value.hostName} ${value.computerName}',
);
} else if (Platform.isWindows) {
return deviceInfo.windowsInfo
.then((final WindowsDeviceInfo value) => value.computerName);
}
}
return 'Unidentified';
}
Future<ServerHostingDetails> authorizeByNewDeviceKey(
final ServerDomain serverDomain,
final String newDeviceKey,
@ -353,7 +318,10 @@ class ServerInstallationRepository {
);
final String serverIp = await getServerIpFromDomain(serverDomain);
final GenericResult<String> result = await serverApi.authorizeDevice(
DeviceToken(device: await getDeviceName(), token: newDeviceKey),
DeviceToken(
device: await PlatformAdapter.deviceName,
token: newDeviceKey,
),
);
if (result.success) {
@ -390,7 +358,7 @@ class ServerInstallationRepository {
);
final String serverIp = await getServerIpFromDomain(serverDomain);
final GenericResult<String> result = await serverApi.useRecoveryToken(
DeviceToken(device: await getDeviceName(), token: recoveryKey),
DeviceToken(device: await PlatformAdapter.deviceName, token: recoveryKey),
);
if (result.success) {
@ -453,7 +421,10 @@ class ServerInstallationRepository {
final GenericResult<String> deviceAuthKey =
await serverApi.createDeviceToken();
final GenericResult<String> result = await serverApi.authorizeDevice(
DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data),
DeviceToken(
device: await PlatformAdapter.deviceName,
token: deviceAuthKey.data,
),
);
if (result.success) {

View file

@ -96,7 +96,6 @@ class _ProvidersPageState extends State<ProvidersPage> {
onTap: () => context.pushRoute(const DnsDetailsRoute()),
),
const SizedBox(height: 16),
// TODO: When backups are fixed, show this card
_Card(
state: isBackupInitialized
? StateType.stable

View file

@ -0,0 +1,59 @@
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
/// SelfPrivacy wrapper for Platform information provider.
class PlatformAdapter {
/// Persistent storage directory for data files.
static String? get storagePath {
String? path;
if (Platform.isLinux) {
// https://wiki.archlinux.org/title/XDG_Base_Directory
path = Platform.environment['XDG_DATA_HOME'];
if (path == null) {
final String home = Platform.environment['HOME'] ?? '.';
path = '$home/.local/share';
}
path += '/selfprivacy';
}
return path;
}
/// Running operating environment.
static Future<String> get deviceName async {
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (kIsWeb) {
return deviceInfo.webBrowserInfo.then(
(final WebBrowserInfo value) =>
'${value.browserName} ${value.platform}',
);
} else {
if (Platform.isAndroid) {
return deviceInfo.androidInfo.then(
(final AndroidDeviceInfo value) =>
'${value.model} ${value.version.release}',
);
} else if (Platform.isIOS) {
return deviceInfo.iosInfo.then(
(final IosDeviceInfo value) =>
'${value.utsname.machine} ${value.systemName} ${value.systemVersion}',
);
} else if (Platform.isLinux) {
return deviceInfo.linuxInfo
.then((final LinuxDeviceInfo value) => value.prettyName);
} else if (Platform.isMacOS) {
return deviceInfo.macOsInfo.then(
(final MacOsDeviceInfo value) =>
'${value.hostName} ${value.computerName}',
);
} else if (Platform.isWindows) {
return deviceInfo.windowsInfo
.then((final WindowsDeviceInfo value) => value.computerName);
}
}
return 'Unidentified';
}
}