11import 'package:arabic_learning/funcs/ui.dart' ;
22import 'package:arabic_learning/sub_pages_builder/setting_pages/item_widget.dart' ;
3+ import 'package:arabic_learning/funcs/sync.dart' ;
34import 'package:arabic_learning/vars/global.dart' ;
45import 'package:arabic_learning/vars/statics_var.dart' ;
56import 'package:flutter/material.dart' ;
@@ -14,10 +15,17 @@ class DataSyncPage extends StatefulWidget {
1415
1516class _DataSyncPage extends State <DataSyncPage > {
1617 bool ? enabled;
18+ bool isUploading = false ;
19+ bool isDownloading = false ;
1720
1821 @override
1922 Widget build (BuildContext context) {
2023 enabled ?? = context.read <Global >().settingData["sync" ]["enabled" ];
24+ final WebDAV webdav = WebDAV (
25+ uri: context.read <Global >().settingData["sync" ]["account" ]["uri" ],
26+ user: context.read <Global >().settingData["sync" ]["account" ]["userName" ],
27+ password: context.read <Global >().settingData["sync" ]["account" ]["passWord" ]
28+ );
2129
2230 return Scaffold (
2331 appBar: AppBar (
@@ -26,64 +34,156 @@ class _DataSyncPage extends State<DataSyncPage> {
2634 body: ListView (
2735 children: [
2836 TextContainer (text: "该功能还处在预览阶段" , style: TextStyle (color: Colors .redAccent)),
29- SyncRemoteSettingWidget (setPageState : setState)
30- ] ,
31- ),
32- );
33- }
34- }
35-
36- class SyncRemoteSettingWidget extends StatelessWidget {
37- final Function setPageState;
38- const SyncRemoteSettingWidget ({ super .key, required this .setPageState});
39-
40- @override
41- Widget build ( BuildContext context) {
42- return Column (
43- crossAxisAlignment : CrossAxisAlignment .start,
44- children : [
45- SettingItem (
46- title : "远程" ,
47- padding : EdgeInsets . all ( 8.0 ),
48- children : [
49- Row (
50- children : [
51- Icon ( Icons .account_box, size : 36 ),
52- Expanded (
53- child : Column (
54- crossAxisAlignment : CrossAxisAlignment .start,
37+ Column (
38+ crossAxisAlignment : CrossAxisAlignment .start ,
39+ children : [
40+ SettingItem (
41+ title : "远程" ,
42+ padding : EdgeInsets . all ( 8.0 ),
43+ children : [
44+ Row (
45+ children : [
46+ Icon ( Icons .account_box, size : 36 ),
47+ Expanded (
48+ child : Text ( "WebDAV账户" ),
49+ ),
50+ ElevatedButton (
51+ style : ElevatedButton . styleFrom (
52+ shape : RoundedRectangleBorder (borderRadius : StaticsVar .br)
53+ ),
54+ onPressed : () async {
55+ await popAccountSetting (context);
56+ setState (() {});
57+ },
58+ child : Text ( "绑定" )
59+ ),
60+ ],
61+ ),
62+ Row (
5563 children: [
56- Text ("WebDAV账户" ),
64+ Text ("联通性检查: " ),
65+ if ((context.read <Global >().settingData["sync" ]["account" ]["uri" ] as String ).isEmpty) Text ("未绑定" , style: Theme .of (context).textTheme.labelSmall),
66+ FutureBuilder (
67+ future: WebDAV .test (
68+ context.read <Global >().settingData["sync" ]["account" ]["uri" ],
69+ context.read <Global >().settingData["sync" ]["account" ]["userName" ],
70+ password: context.read <Global >().settingData["sync" ]["account" ]["passWord" ]
71+ ),
72+ builder: (context, snapshot) {
73+ if (snapshot.hasError) {
74+ return Row (
75+ children: [
76+ Icon (Icons .circle, color: Colors .redAccent, size: 18 ),
77+ Text ("在测试中遇到了未知的异常" , style: TextStyle (fontSize: 8 ))
78+ ],
79+ );
80+ }
81+ if (snapshot.connectionState == ConnectionState .waiting) {
82+ return CircularProgressIndicator ();
83+ }
84+ if (snapshot.hasData) {
85+ return Row (
86+ children: [
87+ Icon (Icons .circle, color: snapshot.data! [1 ] ? Colors .greenAccent : snapshot.data! [0 ] ? Colors .amber : Colors .redAccent, size: 18 )
88+ ],
89+ );
90+ }
91+ return CircularProgressIndicator ();
92+ },
93+ )
94+ ],
95+ )
96+ ],
97+ ),
98+ StatefulBuilder (
99+ builder: (context, setLocalState) {
100+ return SettingItem (
101+ title: "同步" ,
102+ padding: EdgeInsets .all (8.0 ),
103+ children: [
104+ Row (
105+ children: [
106+ Expanded (
107+ child: Column (
108+ crossAxisAlignment: CrossAxisAlignment .start,
109+ children: [
110+ Text ("上传数据" ),
111+ Text ("将本地配置上传到WebDAV服务器" , style: TextStyle (color: Colors .grey, fontSize: 8.0 ))
112+ ],
113+ )
114+ ),
115+ isUploading
116+ ? CircularProgressIndicator ()
117+ : ElevatedButton (
118+ onPressed: () async {
119+ setLocalState (() {
120+ isUploading = true ;
121+ });
122+ try {
123+ if (! webdav.isReachable) await webdav.connect ();
124+ if (context.mounted) await webdav.upload (context.read <Global >().prefs);
125+ if (! context.mounted) return ;
126+ } catch (e) {
127+ alart (context, e.toString ());
128+ return ;
129+ }
130+ setLocalState (() {
131+ isUploading = false ;
132+ });
133+ alart (context, "已上传" );
134+ },
135+ child: Text ("上传" )
136+ )
137+ ],
138+ ),
57139 Row (
58140 children: [
59- Text ("联通性检查: " ),
60- if ((context.read <Global >().settingData["sync" ]["account" ]["uri" ] as String ).isEmpty) Text ("未绑定" , style: Theme .of (context).textTheme.labelSmall),
61- Icon (Icons .circle, color: Colors .greenAccent, size: 12 )
141+ Expanded (
142+ child: Column (
143+ crossAxisAlignment: CrossAxisAlignment .start,
144+ children: [
145+ Text ("恢复数据" ),
146+ Text ("从WebDAV服务器恢复配置" , style: TextStyle (color: Colors .grey, fontSize: 8.0 ))
147+ ],
148+ )
149+ ),
150+ isDownloading
151+ ? CircularProgressIndicator ()
152+ : ElevatedButton (
153+ onPressed: () async {
154+ setLocalState (() {
155+ isDownloading = true ;
156+ });
157+ try {
158+ if (! webdav.isReachable) await webdav.connect ();
159+ if (context.mounted) if (await webdav.download (context.read <Global >().prefs) && context.mounted) context.read <Global >().conveySetting ();
160+ if (! context.mounted) return ;
161+ } catch (e) {
162+ alart (context, e.toString ());
163+ return ;
164+ }
165+ setLocalState (() {
166+ isDownloading = false ;
167+ });
168+ alart (context, "已恢复\n 部分设置可能需要软件重启后才能生效" );
169+ },
170+ child: Text ("恢复" )
171+ )
62172 ],
63173 )
64174 ],
65- ),
66- ),
67- ElevatedButton (
68- style: ElevatedButton .styleFrom (
69- shape: RoundedRectangleBorder (borderRadius: StaticsVar .br)
70- ),
71- onPressed: () async {
72- await popAccountSetting (context);
73- setPageState (() {});
74- },
75- child: Text ("绑定" )
76- ),
77- ],
78- ),
79- ],
80- )
81-
82- ],
175+ );
176+ }
177+ ),
178+ ],
179+ ),
180+ ],
181+ ),
83182 );
84183 }
85184}
86185
186+
87187Future <void > popAccountSetting (BuildContext context) async {
88188 TextEditingController uriController = TextEditingController ();
89189 TextEditingController accountController = TextEditingController ();
0 commit comments