Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 84d4a7d

Browse files
author
eightgran
committed
Implement file picking on example app
1 parent d6aadec commit 84d4a7d

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

example/lib/main.dart

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,38 @@ class HomeScreen extends StatefulWidget {
5454
}
5555

5656
class _HomeScreenState extends State<HomeScreen> {
57+
int _currentIndex = 0;
58+
bool _pressedPickFile = false;
59+
5760
TextEditingController _nameInput = TextEditingController(text: '');
5861
TextEditingController _quoteInput =
5962
TextEditingController(text: 'If you want to be happy, be.');
6063

61-
ExampleBackup get exampleBackup {
64+
ExampleBackup get _exampleBackup {
6265
return ExampleBackup(
6366
name: _nameInput.text,
6467
quote: _quoteInput.text,
6568
backupDate: DateTime.now().toIso8601String(),
6669
);
6770
}
6871

69-
final BackupStorage backupStorage = BackupStorage(
72+
String _formatAsLocalizedDate(BuildContext context, DateTime date) {
73+
final TimeOfDay timeOfDay = TimeOfDay.fromDateTime(date);
74+
final String dateFormat =
75+
MaterialLocalizations.of(context).formatShortDate(date);
76+
final String timeFormat = MaterialLocalizations.of(context).formatTimeOfDay(
77+
timeOfDay,
78+
);
79+
return "$dateFormat $timeFormat";
80+
}
81+
82+
void _onPressedPick() {
83+
setState(() {
84+
_pressedPickFile = !_pressedPickFile;
85+
});
86+
}
87+
88+
final BackupStorage _backupStorage = BackupStorage(
7089
organizationName: "MyOrganization",
7190
applicationName: "LitBackupService",
7291
fileName: "examplebackup",
@@ -80,13 +99,13 @@ class _HomeScreenState extends State<HomeScreen> {
8099
Future<void> _writeBackup(ExampleBackup backup) async {
81100
setState(
82101
() => {
83-
backupStorage.writeBackup(backup),
102+
_backupStorage.writeBackup(backup),
84103
},
85104
);
86105
}
87106

88107
Future<BackupModel?> _readBackup() {
89-
return backupStorage.readBackup(
108+
return _backupStorage.readBackup(
90109
decode: (contents) => ExampleBackup.fromJson(
91110
jsonDecode(contents),
92111
),
@@ -96,37 +115,23 @@ class _HomeScreenState extends State<HomeScreen> {
96115
Future<void> _deleteBackup() async {
97116
setState(
98117
() => {
99-
backupStorage.deleteBackup(),
118+
_backupStorage.deleteBackup(),
100119
},
101120
);
102121
}
103122

104123
Future<BackupModel?> _readBackupFromPicker() {
105-
return backupStorage.pickBackupFile(
124+
return _backupStorage.pickBackupFile(
106125
decode: (contents) => ExampleBackup.fromJson(
107126
jsonDecode(contents),
108127
),
109128
);
110129
}
111130

112131
Future<void> _requestPermissions() async {
113-
backupStorage.requestPermissions().then((value) => setState(() {}));
132+
_backupStorage.requestPermissions().then((value) => setState(() {}));
114133
}
115134

116-
String formatAsLocalizedDate(BuildContext context, DateTime date) {
117-
final TimeOfDay timeOfDay = TimeOfDay.fromDateTime(date);
118-
final String dateFormat =
119-
MaterialLocalizations.of(context).formatShortDate(date);
120-
final String timeFormat = MaterialLocalizations.of(context).formatTimeOfDay(
121-
timeOfDay,
122-
);
123-
return "$dateFormat $timeFormat";
124-
}
125-
126-
int currentIndex = 0;
127-
128-
bool _pressedPickFile = false;
129-
130135
@override
131136
void dispose() {
132137
_pressedPickFile = false;
@@ -137,10 +142,10 @@ class _HomeScreenState extends State<HomeScreen> {
137142
Widget build(BuildContext context) {
138143
return Scaffold(
139144
bottomNavigationBar: BottomNavigationBar(
140-
currentIndex: currentIndex,
145+
currentIndex: _currentIndex,
141146
onTap: (selected) {
142147
setState(() {
143-
currentIndex = selected;
148+
_currentIndex = selected;
144149
_pressedPickFile = false;
145150
});
146151
},
@@ -160,7 +165,7 @@ class _HomeScreenState extends State<HomeScreen> {
160165
backgroundColor: Colors.black,
161166
title: Text("LitBackupService"),
162167
),
163-
body: currentIndex == 0
168+
body: _currentIndex == 0
164169
? Scaffold(
165170
body: Center(
166171
child: Padding(
@@ -169,54 +174,50 @@ class _HomeScreenState extends State<HomeScreen> {
169174
horizontal: 16.0,
170175
),
171176
child: FutureBuilder(
172-
future: backupStorage.hasPermissions(),
173-
builder: (context, AsyncSnapshot<bool> hasPerSnap) {
174-
if (hasPerSnap.connectionState ==
175-
ConnectionState.done &&
176-
hasPerSnap.hasData) {
177-
return hasPerSnap.data!
178-
? Column(
179-
children: [
180-
ElevatedButton(
181-
onPressed: () {
182-
setState(() {
183-
_pressedPickFile = !_pressedPickFile;
184-
});
185-
},
186-
child: Text(_pressedPickFile
187-
? "CLEAR"
188-
: "PICK BACKUP FILE"),
189-
),
190-
_pressedPickFile
191-
? _BackupPreviewBuilder(
192-
backupStorage: backupStorage,
193-
formatAsLocalizedDate:
194-
formatAsLocalizedDate,
195-
readBackup: _readBackupFromPicker(),
196-
requestPermissions:
197-
_requestPermissions,
198-
)
199-
: SizedBox(),
200-
],
201-
)
202-
: Column(
203-
children: [
204-
Padding(
205-
padding: const EdgeInsets.symmetric(
206-
vertical: 8.0),
207-
child: Text(
208-
"Reading backup from storage denied."),
209-
),
210-
ElevatedButton(
211-
onPressed: _requestPermissions,
212-
child: Text("Request permissions"),
213-
),
214-
],
215-
);
216-
}
177+
future: _backupStorage.hasPermissions(),
178+
builder: (context, AsyncSnapshot<bool> hasPerSnap) {
179+
if (hasPerSnap.connectionState == ConnectionState.done &&
180+
hasPerSnap.hasData) {
181+
return hasPerSnap.data!
182+
? Column(
183+
children: [
184+
ElevatedButton(
185+
onPressed: _onPressedPick,
186+
child: Text(_pressedPickFile
187+
? "CLEAR"
188+
: "PICK BACKUP FILE"),
189+
),
190+
_pressedPickFile
191+
? _BackupPreviewBuilder(
192+
backupStorage: _backupStorage,
193+
formatAsLocalizedDate:
194+
_formatAsLocalizedDate,
195+
readBackup: _readBackupFromPicker(),
196+
requestPermissions:
197+
_requestPermissions,
198+
)
199+
: SizedBox(),
200+
],
201+
)
202+
: Column(
203+
children: [
204+
Padding(
205+
padding: const EdgeInsets.symmetric(
206+
vertical: 8.0),
207+
child: Text(
208+
"Reading backup from storage denied."),
209+
),
210+
ElevatedButton(
211+
onPressed: _requestPermissions,
212+
child: Text("Request permissions"),
213+
),
214+
],
215+
);
216+
}
217217

218-
return CircularProgressIndicator();
219-
}),
218+
return CircularProgressIndicator();
219+
},
220+
),
220221
),
221222
),
222223
)
@@ -239,18 +240,18 @@ class _HomeScreenState extends State<HomeScreen> {
239240
controller: _quoteInput,
240241
),
241242
ElevatedButton(
242-
onPressed: () => _writeBackup(exampleBackup),
243+
onPressed: () => _writeBackup(_exampleBackup),
243244
child: Text("BACKUP NOW"),
244245
),
245246
_BackupPreviewBuilder(
246-
backupStorage: backupStorage,
247-
formatAsLocalizedDate: formatAsLocalizedDate,
247+
backupStorage: _backupStorage,
248+
formatAsLocalizedDate: _formatAsLocalizedDate,
248249
readBackup: _readBackup(),
249250
requestPermissions: _requestPermissions,
250251
showMediaLocation: true,
251252
),
252253
FutureBuilder(
253-
future: backupStorage.hasPermissions(),
254+
future: _backupStorage.hasPermissions(),
254255
builder: (context, AsyncSnapshot<bool> hasPerSnap) {
255256
return hasPerSnap.hasData
256257
? hasPerSnap.data!

0 commit comments

Comments
 (0)