@@ -28,9 +28,23 @@ class MyApp extends StatelessWidget {
28
28
}
29
29
}
30
30
31
- class Home extends StatelessWidget {
31
+ class Home extends StatefulWidget {
32
32
const Home ({Key ? key}) : super (key: key);
33
33
34
+ @override
35
+ State <Home > createState () => _HomeState ();
36
+ }
37
+
38
+ class _HomeState extends State <Home > {
39
+ // for backups
40
+ bool showBackupProgress = false ;
41
+ int contactsAmountDatabase = 0 ;
42
+ int contactsExported = 0 ;
43
+ // for restores
44
+ bool showRestoreProgress = false ;
45
+ int contactsAmountFilesystem = 0 ;
46
+ int contactsImported = 0 ;
47
+
34
48
Future <void > backup (BuildContext context) async {
35
49
// Requests contacts & internal storage permissions
36
50
if (await FlutterContacts .requestPermission () &&
@@ -51,6 +65,10 @@ class Home extends StatelessWidget {
51
65
final List <Contact > contacts = await FlutterContacts .getContacts (
52
66
withProperties: true , withPhoto: true , withGroups: true );
53
67
68
+ setState (() {
69
+ contactsAmountDatabase = contacts.length;
70
+ });
71
+
54
72
// Recreate the temp directory if it already exists.
55
73
final Directory directory =
56
74
Directory ("/storage/emulated/0/linux-android-backup-temp" );
@@ -65,6 +83,9 @@ class Home extends StatelessWidget {
65
83
final File file = File (
66
84
"/storage/emulated/0/linux-android-backup-temp/linux-android-backup-contact-$i .vcf" );
67
85
file.writeAsString (vCard);
86
+ setState (() {
87
+ contactsExported = i + 1 ;
88
+ });
68
89
}
69
90
70
91
// Show a dialog if the export is complete
@@ -97,6 +118,10 @@ class Home extends StatelessWidget {
97
118
// List directory contents
98
119
final List <FileSystemEntity > files = await contactsDir.list ().toList ();
99
120
121
+ setState (() {
122
+ contactsAmountFilesystem = files.length;
123
+ });
124
+
100
125
// Loop over the contents
101
126
for (var i = 0 ; i < files.length; i++ ) {
102
127
if (files[i] is File ) {
@@ -105,6 +130,10 @@ class Home extends StatelessWidget {
105
130
final Contact contact = Contact .fromVCard (vcard);
106
131
await contact.insert ();
107
132
}
133
+
134
+ setState (() {
135
+ contactsImported = i + 1 ;
136
+ });
108
137
}
109
138
110
139
showInfoDialog (context, "Success" , "Data has been imported." );
@@ -149,19 +178,46 @@ class Home extends StatelessWidget {
149
178
),
150
179
ElevatedButton (
151
180
onPressed: () {
181
+ setState (() {
182
+ showBackupProgress = true ;
183
+ });
152
184
backup (context);
153
185
},
154
186
child: const Text ("Export Data" ),
155
187
),
188
+ Visibility (
189
+ visible: showBackupProgress,
190
+ child: Text ("Exported " +
191
+ contactsExported.toString () +
192
+ " contact(s) out of " +
193
+ contactsAmountDatabase.toString () +
194
+ "." )),
195
+ const Divider (
196
+ color: Color .fromARGB (31 , 44 , 44 , 44 ),
197
+ height: 25 ,
198
+ thickness: 1 ,
199
+ indent: 5 ,
200
+ endIndent: 5 ,
201
+ ),
156
202
const Text (
157
203
"Upon restoring a backup, press the button below to automatically import all contacts." ,
158
204
),
159
205
ElevatedButton (
160
206
onPressed: () {
207
+ setState (() {
208
+ showRestoreProgress = true ;
209
+ });
161
210
autoRestoreContacts (context);
162
211
},
163
212
child: const Text ("Auto-restore contacts" ),
164
- )
213
+ ),
214
+ Visibility (
215
+ visible: showRestoreProgress,
216
+ child: Text ("Restored " +
217
+ contactsImported.toString () +
218
+ " contact(s) out of " +
219
+ contactsAmountFilesystem.toString () +
220
+ "." )),
165
221
],
166
222
),
167
223
),
0 commit comments