Skip to content

Commit a8649ce

Browse files
author
code3-dev
committed
Add BG Store
1 parent 78dfa86 commit a8649ce

File tree

1 file changed

+119
-96
lines changed

1 file changed

+119
-96
lines changed

lib/screens/wallpaper_store_screen.dart

Lines changed: 119 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ class _WallpaperStoreScreenState extends State<WallpaperStoreScreen> {
4949
if (response.statusCode == 200) {
5050
// Parse the JSON response
5151
final List<dynamic> data = json.decode(response.body);
52-
final List<String> wallpapers =
53-
data.map((item) => item.toString()).toList();
54-
52+
final List<String> wallpapers = data
53+
.map((item) => item.toString())
54+
.toList();
55+
5556
setState(() {
5657
_wallpapers = wallpapers;
5758
_isLoading = false;
@@ -80,40 +81,46 @@ class _WallpaperStoreScreenState extends State<WallpaperStoreScreen> {
8081
try {
8182
// Use Android Download Manager for downloading
8283
const platform = MethodChannel('com.cloud.pira/download');
83-
84+
8485
// Generate filename from URL
8586
final String fileName = url.split('/').last;
86-
87+
8788
// Invoke the Android Download Manager
8889
final String downloadId = await platform.invokeMethod('downloadFile', {
8990
'url': url,
9091
'fileName': fileName,
9192
});
92-
93+
9394
// Show success message
9495
ScaffoldMessenger.of(context).showSnackBar(
9596
SnackBar(
96-
content: Text(context.tr(TranslationKeys.wallpaperStoreDownloadSuccess)),
97+
content: Text(
98+
context.tr(TranslationKeys.wallpaperStoreDownloadSuccess),
99+
),
97100
backgroundColor: AppTheme.primaryBlue,
98101
),
99102
);
100103
} on PlatformException catch (e) {
101104
ScaffoldMessenger.of(context).showSnackBar(
102105
SnackBar(
103-
content: Text(context.tr(
104-
TranslationKeys.wallpaperStoreDownloadError,
105-
parameters: {'error': e.message ?? 'Unknown error'},
106-
)),
106+
content: Text(
107+
context.tr(
108+
TranslationKeys.wallpaperStoreDownloadError,
109+
parameters: {'error': e.message ?? 'Unknown error'},
110+
),
111+
),
107112
backgroundColor: Colors.red,
108113
),
109114
);
110115
} catch (e) {
111116
ScaffoldMessenger.of(context).showSnackBar(
112117
SnackBar(
113-
content: Text(context.tr(
114-
TranslationKeys.wallpaperStoreDownloadError,
115-
parameters: {'error': e.toString()},
116-
)),
118+
content: Text(
119+
context.tr(
120+
TranslationKeys.wallpaperStoreDownloadError,
121+
parameters: {'error': e.toString()},
122+
),
123+
),
117124
backgroundColor: Colors.red,
118125
),
119126
);
@@ -123,7 +130,7 @@ class _WallpaperStoreScreenState extends State<WallpaperStoreScreen> {
123130
Future<void> _setAsWallpaper(String url) async {
124131
try {
125132
final response = await http.get(Uri.parse(url));
126-
133+
127134
if (response.statusCode == 200) {
128135
// Get app documents directory
129136
final Directory appDir = await getApplicationDocumentsDirectory();
@@ -148,34 +155,38 @@ class _WallpaperStoreScreenState extends State<WallpaperStoreScreen> {
148155
context,
149156
listen: false,
150157
);
151-
158+
152159
// Use the new method to set wallpaper from URL
153160
final success = await wallpaperService.setWallpaperFromUrl(url);
154-
161+
155162
if (!success) {
156163
throw Exception('Failed to set wallpaper from URL');
157164
}
158-
165+
159166
// Show success message
160167
ScaffoldMessenger.of(context).showSnackBar(
161168
SnackBar(
162169
content: Text(context.tr(TranslationKeys.wallpaperStoreSetSuccess)),
163170
backgroundColor: AppTheme.primaryBlue,
164171
),
165172
);
166-
173+
167174
// Go back to the previous screen
168175
Navigator.of(context).pop();
169176
} else {
170-
throw Exception('Failed to download image: HTTP ${response.statusCode}');
177+
throw Exception(
178+
'Failed to download image: HTTP ${response.statusCode}',
179+
);
171180
}
172181
} catch (e) {
173182
ScaffoldMessenger.of(context).showSnackBar(
174183
SnackBar(
175-
content: Text(context.tr(
176-
TranslationKeys.wallpaperStoreDownloadError,
177-
parameters: {'error': e.toString()},
178-
)),
184+
content: Text(
185+
context.tr(
186+
TranslationKeys.wallpaperStoreDownloadError,
187+
parameters: {'error': e.toString()},
188+
),
189+
),
179190
backgroundColor: Colors.red,
180191
),
181192
);
@@ -206,54 +217,55 @@ class _WallpaperStoreScreenState extends State<WallpaperStoreScreen> {
206217
),
207218
)
208219
: _errorMessage.isNotEmpty
209-
? Center(
210-
child: Column(
211-
mainAxisAlignment: MainAxisAlignment.center,
212-
children: [
213-
Text(
214-
_errorMessage,
215-
style: const TextStyle(color: Colors.white70),
216-
textAlign: TextAlign.center,
217-
),
218-
const SizedBox(height: 16),
219-
ElevatedButton(
220-
onPressed: _fetchWallpapers,
221-
style: ElevatedButton.styleFrom(
222-
backgroundColor: AppTheme.primaryBlue,
223-
),
224-
child: Text(
225-
context.tr(TranslationKeys.wallpaperStoreRetry),
226-
),
227-
),
228-
],
220+
? Center(
221+
child: Column(
222+
mainAxisAlignment: MainAxisAlignment.center,
223+
children: [
224+
Text(
225+
_errorMessage,
226+
style: const TextStyle(color: Colors.white70),
227+
textAlign: TextAlign.center,
229228
),
230-
)
231-
: _wallpapers.isEmpty
232-
? Center(
233-
child: Text(
234-
context.tr(TranslationKeys.wallpaperStoreNoWallpapers),
235-
style: const TextStyle(color: Colors.white70),
236-
),
237-
)
238-
: GridView.builder(
239-
padding: const EdgeInsets.all(16),
240-
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
241-
crossAxisCount: 2,
242-
crossAxisSpacing: 12,
243-
mainAxisSpacing: 12,
244-
childAspectRatio: 0.9, // Increased to give more space for buttons
245-
),
246-
itemCount: _wallpapers.length,
247-
itemBuilder: (context, index) {
248-
final wallpaperUrl = _wallpapers[index];
249-
return _WallpaperItem(
250-
imageUrl: wallpaperUrl,
251-
onDownload: () => _downloadWallpaper(wallpaperUrl),
252-
onSetAsWallpaper: () => _setAsWallpaper(wallpaperUrl),
253-
onTap: () => _showFullScreenImage(wallpaperUrl),
254-
);
255-
},
229+
const SizedBox(height: 16),
230+
ElevatedButton(
231+
onPressed: _fetchWallpapers,
232+
style: ElevatedButton.styleFrom(
233+
backgroundColor: AppTheme.primaryBlue,
234+
),
235+
child: Text(
236+
context.tr(TranslationKeys.wallpaperStoreRetry),
256237
),
238+
),
239+
],
240+
),
241+
)
242+
: _wallpapers.isEmpty
243+
? Center(
244+
child: Text(
245+
context.tr(TranslationKeys.wallpaperStoreNoWallpapers),
246+
style: const TextStyle(color: Colors.white70),
247+
),
248+
)
249+
: GridView.builder(
250+
padding: const EdgeInsets.all(16),
251+
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
252+
crossAxisCount: 2,
253+
crossAxisSpacing: 12,
254+
mainAxisSpacing: 12,
255+
childAspectRatio:
256+
0.9, // Increased to give more space for buttons
257+
),
258+
itemCount: _wallpapers.length,
259+
itemBuilder: (context, index) {
260+
final wallpaperUrl = _wallpapers[index];
261+
return _WallpaperItem(
262+
imageUrl: wallpaperUrl,
263+
onDownload: () => _downloadWallpaper(wallpaperUrl),
264+
onSetAsWallpaper: () => _setAsWallpaper(wallpaperUrl),
265+
onTap: () => _showFullScreenImage(wallpaperUrl),
266+
);
267+
},
268+
),
257269
);
258270
}
259271

@@ -295,13 +307,17 @@ class _WallpaperItem extends StatelessWidget {
295307
child: GestureDetector(
296308
onTap: onTap,
297309
child: ClipRRect(
298-
borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
310+
borderRadius: const BorderRadius.vertical(
311+
top: Radius.circular(12),
312+
),
299313
child: CachedNetworkImage(
300314
imageUrl: imageUrl,
301315
fit: BoxFit.cover,
302316
placeholder: (context, url) => Center(
303317
child: CircularProgressIndicator(
304-
valueColor: AlwaysStoppedAnimation<Color>(AppTheme.primaryBlue),
318+
valueColor: AlwaysStoppedAnimation<Color>(
319+
AppTheme.primaryBlue,
320+
),
305321
),
306322
),
307323
errorWidget: (context, url, error) => Container(
@@ -318,7 +334,7 @@ class _WallpaperItem extends StatelessWidget {
318334
),
319335
),
320336
),
321-
337+
322338
// Buttons container - fixed height to prevent overflow
323339
SizedBox(
324340
height: 60, // Fixed height to prevent overflow
@@ -342,16 +358,18 @@ class _WallpaperItem extends StatelessWidget {
342358
),
343359
),
344360
child: Text(
345-
context.tr(TranslationKeys.wallpaperStoreSetAsWallpaper),
361+
context.tr(
362+
TranslationKeys.wallpaperStoreSetAsWallpaper,
363+
),
346364
style: const TextStyle(fontSize: 10),
347365
textAlign: TextAlign.center,
348366
),
349367
),
350368
),
351369
),
352-
370+
353371
const SizedBox(width: 4),
354-
372+
355373
// Download button
356374
Expanded(
357375
flex: 1,
@@ -389,7 +407,8 @@ class _WallpaperItem extends StatelessWidget {
389407
class FullScreenImageViewer extends StatefulWidget {
390408
final String imageUrl;
391409

392-
const FullScreenImageViewer({Key? key, required this.imageUrl}) : super(key: key);
410+
const FullScreenImageViewer({Key? key, required this.imageUrl})
411+
: super(key: key);
393412

394413
@override
395414
State<FullScreenImageViewer> createState() => _FullScreenImageViewerState();
@@ -422,7 +441,9 @@ class _FullScreenImageViewerState extends State<FullScreenImageViewer> {
422441
// TODO: Implement set as wallpaper functionality for fullscreen view
423442
ScaffoldMessenger.of(context).showSnackBar(
424443
const SnackBar(
425-
content: Text('Set as wallpaper functionality can be added here'),
444+
content: Text(
445+
'Set as wallpaper functionality can be added here',
446+
),
426447
backgroundColor: Colors.blue,
427448
),
428449
);
@@ -444,11 +465,7 @@ class _FullScreenImageViewerState extends State<FullScreenImageViewer> {
444465
),
445466
),
446467
errorWidget: (context, url, error) => const Center(
447-
child: Icon(
448-
Icons.broken_image,
449-
color: Colors.white70,
450-
size: 48,
451-
),
468+
child: Icon(Icons.broken_image, color: Colors.white70, size: 48),
452469
),
453470
),
454471
),
@@ -460,40 +477,46 @@ class _FullScreenImageViewerState extends State<FullScreenImageViewer> {
460477
try {
461478
// Use Android Download Manager for downloading
462479
const platform = MethodChannel('com.cloud.pira/download');
463-
480+
464481
// Generate filename from URL
465482
final String fileName = url.split('/').last;
466-
483+
467484
// Invoke the Android Download Manager
468485
final String downloadId = await platform.invokeMethod('downloadFile', {
469486
'url': url,
470487
'fileName': fileName,
471488
});
472-
489+
473490
// Show success message
474491
ScaffoldMessenger.of(context).showSnackBar(
475492
SnackBar(
476-
content: Text(context.tr(TranslationKeys.wallpaperStoreDownloadSuccess)),
493+
content: Text(
494+
context.tr(TranslationKeys.wallpaperStoreDownloadSuccess),
495+
),
477496
backgroundColor: AppTheme.primaryBlue,
478497
),
479498
);
480499
} on PlatformException catch (e) {
481500
ScaffoldMessenger.of(context).showSnackBar(
482501
SnackBar(
483-
content: Text(context.tr(
484-
TranslationKeys.wallpaperStoreDownloadError,
485-
parameters: {'error': e.message ?? 'Unknown error'},
486-
)),
502+
content: Text(
503+
context.tr(
504+
TranslationKeys.wallpaperStoreDownloadError,
505+
parameters: {'error': e.message ?? 'Unknown error'},
506+
),
507+
),
487508
backgroundColor: Colors.red,
488509
),
489510
);
490511
} catch (e) {
491512
ScaffoldMessenger.of(context).showSnackBar(
492513
SnackBar(
493-
content: Text(context.tr(
494-
TranslationKeys.wallpaperStoreDownloadError,
495-
parameters: {'error': e.toString()},
496-
)),
514+
content: Text(
515+
context.tr(
516+
TranslationKeys.wallpaperStoreDownloadError,
517+
parameters: {'error': e.toString()},
518+
),
519+
),
497520
backgroundColor: Colors.red,
498521
),
499522
);

0 commit comments

Comments
 (0)