first commit.
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cloud_archiver/utils.dart';
|
||||
import 'dart:convert' show json;
|
||||
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' show join;
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
late File _configFile;
|
||||
|
||||
AppConfig? appConfig;
|
||||
|
||||
class AppConfig {
|
||||
final String client;
|
||||
final String server;
|
||||
final String user;
|
||||
final String passwd;
|
||||
|
||||
final List<SyncItemConfig> syncItems;
|
||||
|
||||
static Future<void> init() async {
|
||||
var directory = await getApplicationSupportDirectory();
|
||||
_configFile = File(join(directory.path, 'config.json'));
|
||||
if (_configFile.existsSync()) {
|
||||
try {
|
||||
appConfig = AppConfig.fromJson(json.decode(_configFile.readAsStringSync()));
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void updateConfig({
|
||||
required String client,
|
||||
required String server,
|
||||
required String user,
|
||||
required String passwd,
|
||||
}) {
|
||||
appConfig = AppConfig(
|
||||
client: client,
|
||||
server: server,
|
||||
user: user,
|
||||
passwd: passwd,
|
||||
syncItems: appConfig?.syncItems ?? [],
|
||||
);
|
||||
_save();
|
||||
}
|
||||
|
||||
static void addSyncItem(SyncItemConfig item) {
|
||||
var items = (appConfig?.syncItems ?? []).where((ele) => ele.uuid != item.uuid);
|
||||
|
||||
appConfig = AppConfig(
|
||||
client: appConfig?.client ?? '',
|
||||
server: appConfig?.server ?? '',
|
||||
user: appConfig?.user ?? '',
|
||||
passwd: appConfig?.passwd ?? '',
|
||||
syncItems: [
|
||||
item,
|
||||
...items,
|
||||
],
|
||||
);
|
||||
_save();
|
||||
loadLocalStatus();
|
||||
}
|
||||
|
||||
static void removeSyncItem(SyncItemConfig item) {
|
||||
var items = (appConfig?.syncItems ?? []).where((ele) => ele.uuid != item.uuid);
|
||||
|
||||
appConfig = AppConfig(
|
||||
client: appConfig?.client ?? '',
|
||||
server: appConfig?.server ?? '',
|
||||
user: appConfig?.user ?? '',
|
||||
passwd: appConfig?.passwd ?? '',
|
||||
syncItems: items.toList(),
|
||||
);
|
||||
_save();
|
||||
loadLocalStatus();
|
||||
}
|
||||
|
||||
AppConfig({
|
||||
required this.client,
|
||||
required this.server,
|
||||
required this.user,
|
||||
required this.passwd,
|
||||
required this.syncItems,
|
||||
});
|
||||
|
||||
factory AppConfig.fromJson(Map<String, dynamic> json) => AppConfig(
|
||||
client: json['client'],
|
||||
server: json['server'],
|
||||
user: json['user'],
|
||||
passwd: json['passwd'],
|
||||
syncItems: (json['syncItems'] as List? ?? []).map((ele) => SyncItemConfig.fromJson(ele)).toList(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'client': client,
|
||||
'server': server,
|
||||
'user': user,
|
||||
'passwd': passwd,
|
||||
'syncItems': syncItems,
|
||||
};
|
||||
|
||||
static void _save() {
|
||||
_configFile.writeAsStringSync(json.encode(appConfig));
|
||||
}
|
||||
}
|
||||
|
||||
class SyncItemConfig {
|
||||
final String uuid;
|
||||
final String name;
|
||||
final String path;
|
||||
|
||||
SyncItemConfig({required this.name, required this.path, String? uuid}) : uuid = uuid ?? Uuid().v4();
|
||||
|
||||
factory SyncItemConfig.fromJson(Map<String, dynamic> json) => SyncItemConfig(
|
||||
uuid: json['uuid'],
|
||||
name: json['name'],
|
||||
path: json['path'],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'uuid': uuid,
|
||||
'name': name,
|
||||
'path': path,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user