diff --git a/assets/translation.json b/assets/translation.json index 6cbaeaf..93356c5 100644 --- a/assets/translation.json +++ b/assets/translation.json @@ -354,10 +354,10 @@ "The comic will be marked as no updates as soon as you read it.": "漫画将在您阅读后立即标记为无更新", "Disable": "禁用", "Once the operation is successful, app will automatically sync data with the server.": "操作成功后, APP将自动与服务器同步数据", - "Webdav is enabled." : "已启用Webdav", "Cache cleared": "缓存已清除", "Disabled": "已禁用", - "Last Reading: @epName Page @page": "上次阅读: @epName 第 @page 页" + "Last Reading: @epName Page @page": "上次阅读: @epName 第 @page 页", + "WebDAV Auto Sync": "WebDAV 自动同步" }, "zh_TW": { "Home": "首頁", @@ -714,9 +714,9 @@ "The comic will be marked as no updates as soon as you read it.": "漫畫將在您閱讀後立即標記為無更新", "Disable": "禁用", "Once the operation is successful, app will automatically sync data with the server.": "操作成功後, APP將自動與服務器同步數據", - "Webdav is enabled." : "已啟用Webdav", "Cache cleared": "緩存已清除", "Disabled": "已禁用", - "Last Reading: @epName Page @page": "上次閱讀: @epName 第 @page 頁" + "Last Reading: @epName Page @page": "上次閱讀: @epName 第 @page 頁", + "WebDAV Auto Sync": "WebDAV 自動同步" } } \ No newline at end of file diff --git a/lib/pages/settings/app.dart b/lib/pages/settings/app.dart index 3415aea..9dbd3ee 100644 --- a/lib/pages/settings/app.dart +++ b/lib/pages/settings/app.dart @@ -330,6 +330,7 @@ class _WebdavSettingState extends State<_WebdavSetting> { String url = ""; String user = ""; String pass = ""; + bool autoSync = false; bool isTesting = false; bool upload = true; @@ -349,6 +350,7 @@ class _WebdavSettingState extends State<_WebdavSetting> { user = configs[1]; pass = configs[2]; isEnabled = true; + autoSync = appdata.settings['webdavAutoSync'] ?? false; } @override @@ -358,6 +360,18 @@ class _WebdavSettingState extends State<_WebdavSetting> { body: SingleChildScrollView( child: Column( children: [ + const SizedBox(height: 12), + SwitchListTile( + title: Text("WebDAV Auto Sync".tl), + value: autoSync, + onChanged: (value) { + setState(() { + autoSync = value; + appdata.settings['webdavAutoSync'] = value; + appdata.saveData(); + }); + }, + ), const SizedBox(height: 12), TextField( decoration: const InputDecoration( @@ -428,39 +442,33 @@ class _WebdavSettingState extends State<_WebdavSetting> { ], ), ), - if (isEnabled) - Container( - padding: const EdgeInsets.all(8), - decoration: BoxDecoration( - color: Theme.of(context).colorScheme.primaryContainer, - borderRadius: BorderRadius.circular(8), - ), - child: Row( - children: [ - const Icon(Icons.sync, size: 20), - const SizedBox(width: 8), - Expanded( - child: Text("Webdav is enabled.".tl), - ), - Button.text( - onPressed: () { - appdata.settings['webdav'] = []; - appdata.saveData(); - context.showMessage(message: "Disabled".tl); - App.rootPop(); - }, - child: Text("Disable".tl), - ) - ], - ), - ).paddingTop(16), const SizedBox(height: 16), Center( child: Button.filled( isLoading: isTesting, onPressed: () async { var oldConfig = appdata.settings['webdav']; + var oldAutoSync = appdata.settings['webdavAutoSync']; + + if (url.trim().isEmpty && user.trim().isEmpty && pass.trim().isEmpty) { + appdata.settings['webdav'] = []; + appdata.settings['webdavAutoSync'] = false; + appdata.saveData(); + context.showMessage(message: "Saved".tl); + App.rootPop(); + return; + } + appdata.settings['webdav'] = [url, user, pass]; + appdata.settings['webdavAutoSync'] = autoSync; + + if (!autoSync) { + appdata.saveData(); + context.showMessage(message: "Saved".tl); + App.rootPop(); + return; + } + setState(() { isTesting = true; }); @@ -472,6 +480,7 @@ class _WebdavSettingState extends State<_WebdavSetting> { isTesting = false; }); appdata.settings['webdav'] = oldConfig; + appdata.settings['webdavAutoSync'] = oldAutoSync; context.showMessage(message: testResult.errorMessage!); return; } diff --git a/lib/utils/data_sync.dart b/lib/utils/data_sync.dart index 4ee6435..3cd9937 100644 --- a/lib/utils/data_sync.dart +++ b/lib/utils/data_sync.dart @@ -40,15 +40,19 @@ class DataSync with ChangeNotifier { bool get isEnabled { var config = appdata.settings['webdav']; - return config is List && config.isNotEmpty; + var autoSync = appdata.settings['webdavAutoSync'] ?? false; + return autoSync && config is List && config.isNotEmpty; } List? _validateConfig() { var config = appdata.settings['webdav']; - if (config is! List || (config.isNotEmpty && config.length != 3)) { + if (config is! List) { return null; } - if (config.whereType().length != 3) { + if (config.isEmpty) { + return []; + } + if (config.length != 3 || config.whereType().length != 3) { return null; } return List.from(config);