mirror of
https://github.com/venera-app/venera.git
synced 2025-09-28 00:07:24 +00:00
sync data using webdav
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:sliver_tools/sliver_tools.dart';
|
||||
import 'package:venera/components/components.dart';
|
||||
import 'package:venera/foundation/app.dart';
|
||||
import 'package:venera/foundation/comic_source/comic_source.dart';
|
||||
@@ -17,6 +18,7 @@ import 'package:venera/pages/downloading_page.dart';
|
||||
import 'package:venera/pages/history_page.dart';
|
||||
import 'package:venera/pages/search_page.dart';
|
||||
import 'package:venera/utils/cbz.dart';
|
||||
import 'package:venera/utils/data_sync.dart';
|
||||
import 'package:venera/utils/ext.dart';
|
||||
import 'package:venera/utils/io.dart';
|
||||
import 'package:venera/utils/translations.dart';
|
||||
@@ -32,6 +34,7 @@ class HomePage extends StatelessWidget {
|
||||
slivers: [
|
||||
SliverPadding(padding: EdgeInsets.only(top: context.padding.top)),
|
||||
const _SearchBar(),
|
||||
const _SyncDataWidget(),
|
||||
const _History(),
|
||||
const _Local(),
|
||||
const _ComicSourceWidget(),
|
||||
@@ -77,6 +80,97 @@ class _SearchBar extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class _SyncDataWidget extends StatefulWidget {
|
||||
const _SyncDataWidget();
|
||||
|
||||
@override
|
||||
State<_SyncDataWidget> createState() => _SyncDataWidgetState();
|
||||
}
|
||||
|
||||
class _SyncDataWidgetState extends State<_SyncDataWidget> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
DataSync().addListener(update);
|
||||
}
|
||||
|
||||
void update() {
|
||||
if(mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
DataSync().removeListener(update);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget child;
|
||||
if(!DataSync().isEnabled) {
|
||||
child = const SliverPadding(padding: EdgeInsets.zero);
|
||||
} else if (DataSync().isUploading || DataSync().isDownloading) {
|
||||
child = SliverToBoxAdapter(
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.sync),
|
||||
title: Text('Syncing Data'.tl),
|
||||
trailing: const CircularProgressIndicator(strokeWidth: 2)
|
||||
.fixWidth(18)
|
||||
.fixHeight(18),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
child = SliverToBoxAdapter(
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outlineVariant,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.sync),
|
||||
title: Text('Sync Data'.tl),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.cloud_upload_outlined),
|
||||
onPressed: () async {
|
||||
DataSync().uploadData();
|
||||
}
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.cloud_download_outlined),
|
||||
onPressed: () async {
|
||||
DataSync().downloadData();
|
||||
}
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return SliverAnimatedPaintExtent(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _History extends StatefulWidget {
|
||||
const _History();
|
||||
|
||||
@@ -529,14 +623,16 @@ class _ImportComicsWidgetState extends State<_ImportComicsWidget> {
|
||||
await xFile!.saveTo(cache);
|
||||
var comic = await CBZ.import(File(cache));
|
||||
if (selectedFolder != null) {
|
||||
LocalFavoritesManager().addComic(selectedFolder!, FavoriteItem(
|
||||
id: comic.id,
|
||||
name: comic.title,
|
||||
coverPath: comic.cover,
|
||||
author: comic.subtitle,
|
||||
type: comic.comicType,
|
||||
tags: comic.tags,
|
||||
));
|
||||
LocalFavoritesManager().addComic(
|
||||
selectedFolder!,
|
||||
FavoriteItem(
|
||||
id: comic.id,
|
||||
name: comic.title,
|
||||
coverPath: comic.cover,
|
||||
author: comic.subtitle,
|
||||
type: comic.comicType,
|
||||
tags: comic.tags,
|
||||
));
|
||||
}
|
||||
await File(cache).deleteIgnoreError();
|
||||
} catch (e, s) {
|
||||
@@ -610,14 +706,16 @@ class _ImportComicsWidgetState extends State<_ImportComicsWidget> {
|
||||
for (var comic in comics.values) {
|
||||
LocalManager().add(comic, LocalManager().findValidId(ComicType.local));
|
||||
if (selectedFolder != null) {
|
||||
LocalFavoritesManager().addComic(selectedFolder!, FavoriteItem(
|
||||
id: comic.id,
|
||||
name: comic.title,
|
||||
coverPath: comic.cover,
|
||||
author: comic.subtitle,
|
||||
type: comic.comicType,
|
||||
tags: comic.tags,
|
||||
));
|
||||
LocalFavoritesManager().addComic(
|
||||
selectedFolder!,
|
||||
FavoriteItem(
|
||||
id: comic.id,
|
||||
name: comic.title,
|
||||
coverPath: comic.cover,
|
||||
author: comic.subtitle,
|
||||
type: comic.comicType,
|
||||
tags: comic.tags,
|
||||
));
|
||||
}
|
||||
}
|
||||
context.pop();
|
||||
|
@@ -78,8 +78,7 @@ class _AppSettingsState extends State<AppSettings> {
|
||||
appdata.settings['cacheSize'] = int.parse(value);
|
||||
appdata.saveData();
|
||||
setState(() {});
|
||||
CacheManager()
|
||||
.setLimitSize(appdata.settings['cacheSize']);
|
||||
CacheManager().setLimitSize(appdata.settings['cacheSize']);
|
||||
return null;
|
||||
},
|
||||
);
|
||||
@@ -101,13 +100,12 @@ class _AppSettingsState extends State<AppSettings> {
|
||||
callback: () async {
|
||||
var controller = showLoadingDialog(context);
|
||||
var file = await selectFile(ext: ['venera']);
|
||||
if(file != null) {
|
||||
if (file != null) {
|
||||
var cacheFile = File(FilePath.join(App.cachePath, "temp.venera"));
|
||||
await file.saveTo(cacheFile.path);
|
||||
try {
|
||||
await importAppData(cacheFile);
|
||||
}
|
||||
catch(e, s) {
|
||||
} catch (e, s) {
|
||||
Log.error("Import data", e.toString(), s);
|
||||
context.showMessage(message: "Failed to import data".tl);
|
||||
}
|
||||
@@ -116,6 +114,13 @@ class _AppSettingsState extends State<AppSettings> {
|
||||
},
|
||||
actionTitle: 'Import'.tl,
|
||||
).toSliver(),
|
||||
_CallbackSetting(
|
||||
title: "Data Sync".tl,
|
||||
callback: () async {
|
||||
showPopUpWidget(context, const _WebdavSetting());
|
||||
},
|
||||
actionTitle: 'Set'.tl,
|
||||
).toSliver(),
|
||||
_SettingPartTitle(
|
||||
title: "Log".tl,
|
||||
icon: Icons.error_outline,
|
||||
@@ -271,3 +276,129 @@ class _LogsPageState extends State<LogsPage> {
|
||||
saveFile(data: utf8.encode(log), filename: 'log.txt');
|
||||
}
|
||||
}
|
||||
|
||||
class _WebdavSetting extends StatefulWidget {
|
||||
const _WebdavSetting();
|
||||
|
||||
@override
|
||||
State<_WebdavSetting> createState() => _WebdavSettingState();
|
||||
}
|
||||
|
||||
class _WebdavSettingState extends State<_WebdavSetting> {
|
||||
String url = "";
|
||||
String user = "";
|
||||
String pass = "";
|
||||
|
||||
bool isTesting = false;
|
||||
|
||||
bool upload = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (appdata.settings['webdav'] is! List) {
|
||||
appdata.settings['webdav'] = [];
|
||||
}
|
||||
var configs = appdata.settings['webdav'] as List;
|
||||
if (configs.whereType<String>().length != 3) {
|
||||
return;
|
||||
}
|
||||
url = configs[0];
|
||||
user = configs[1];
|
||||
pass = configs[2];
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return PopUpWidgetScaffold(
|
||||
title: "Webdav",
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: "URL",
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
controller: TextEditingController(text: url),
|
||||
onChanged: (value) => url = value,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
decoration: InputDecoration(
|
||||
labelText: "Username".tl,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
controller: TextEditingController(text: user),
|
||||
onChanged: (value) => user = value,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
decoration: InputDecoration(
|
||||
labelText: "Password".tl,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
controller: TextEditingController(text: pass),
|
||||
onChanged: (value) => pass = value,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Text("Operation".tl),
|
||||
Radio<bool>(
|
||||
groupValue: upload,
|
||||
value: true,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
upload = value!;
|
||||
});
|
||||
},
|
||||
),
|
||||
Text("Upload".tl),
|
||||
Radio<bool>(
|
||||
groupValue: upload,
|
||||
value: false,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
upload = value!;
|
||||
});
|
||||
},
|
||||
),
|
||||
Text("Download".tl),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Center(
|
||||
child: Button.filled(
|
||||
isLoading: isTesting,
|
||||
onPressed: () async {
|
||||
var oldConfig = appdata.settings['webdav'];
|
||||
appdata.settings['webdav'] = [url, user, pass];
|
||||
setState(() {
|
||||
isTesting = true;
|
||||
});
|
||||
var testResult = upload
|
||||
? await DataSync().uploadData()
|
||||
: await DataSync().downloadData();
|
||||
if (testResult.error) {
|
||||
setState(() {
|
||||
isTesting = false;
|
||||
});
|
||||
appdata.settings['webdav'] = oldConfig;
|
||||
context.showMessage(message: testResult.errorMessage!);
|
||||
return;
|
||||
}
|
||||
appdata.saveData();
|
||||
context.showMessage(message: "Saved".tl);
|
||||
App.rootPop();
|
||||
},
|
||||
child: Text("Continue".tl),
|
||||
),
|
||||
)
|
||||
],
|
||||
).paddingHorizontal(16),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -15,6 +15,7 @@ import 'package:venera/foundation/local.dart';
|
||||
import 'package:venera/foundation/log.dart';
|
||||
import 'package:venera/network/app_dio.dart';
|
||||
import 'package:venera/utils/data.dart';
|
||||
import 'package:venera/utils/data_sync.dart';
|
||||
import 'package:venera/utils/io.dart';
|
||||
import 'package:venera/utils/translations.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
Reference in New Issue
Block a user