feat(platform): Implement custom platform-dependent storage path definition

- Create new PlatformAdapter util and hide i/o there
- Move other usages of Platform class in PlatformAdapter
pull/240/head
NaiJi ✨ 2023-07-20 17:06:17 -03:00
parent 1e5b6c12e6
commit 68ed776785
3 changed files with 71 additions and 40 deletions

View File

@ -8,10 +8,11 @@ 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();
await Hive.initFlutter(PlatformAdapter.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);
@ -285,40 +284,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,
@ -330,7 +295,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) {
@ -367,7 +335,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) {
@ -430,7 +398,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

@ -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 == '.') {
final String home = Platform.environment['HOME'] ?? '.';
path += '$home/.local/share';
}
path += '/selfprivacy';
}
return path;
}
/// Running operation 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';
}
}