@@ -129,40 +129,16 @@ class _WallpaperStoreScreenState extends State<WallpaperStoreScreen> {
129129
130130 Future <void > _setAsWallpaper (String url) async {
131131 try {
132- final response = await http.get (Uri .parse (url));
133-
134- if (response.statusCode == 200 ) {
135- // Get app documents directory
136- final Directory appDir = await getApplicationDocumentsDirectory ();
137- final String wallpapersDir = '${appDir .path }/wallpapers' ;
138-
139- // Create wallpapers directory if it doesn't exist
140- final Directory wallpaperDirectory = Directory (wallpapersDir);
141- if (! await wallpaperDirectory.exists ()) {
142- await wallpaperDirectory.create (recursive: true );
143- }
144-
145- // Generate filename from URL
146- final String fileName = url.split ('/' ).last;
147- final String savePath = '$wallpapersDir /$fileName ' ;
148-
149- // Save the image
150- final File file = File (savePath);
151- await file.writeAsBytes (response.bodyBytes);
152-
153- // Set as wallpaper using the wallpaper service
154- final wallpaperService = Provider .of <WallpaperService >(
155- context,
156- listen: false ,
157- );
132+ // Set as wallpaper using the wallpaper service
133+ final wallpaperService = Provider .of <WallpaperService >(
134+ context,
135+ listen: false ,
136+ );
158137
159- // Use the new method to set wallpaper from URL
160- final success = await wallpaperService.setWallpaperFromUrl (url);
161-
162- if (! success) {
163- throw Exception ('Failed to set wallpaper from URL' );
164- }
138+ // Use the existing method to set wallpaper from URL
139+ final success = await wallpaperService.setWallpaperFromUrl (url);
165140
141+ if (success) {
166142 // Show success message
167143 ScaffoldMessenger .of (context).showSnackBar (
168144 SnackBar (
@@ -172,11 +148,11 @@ class _WallpaperStoreScreenState extends State<WallpaperStoreScreen> {
172148 );
173149
174150 // Go back to the previous screen
175- Navigator .of (context).pop ();
151+ if (mounted) {
152+ Navigator .of (context).pop ();
153+ }
176154 } else {
177- throw Exception (
178- 'Failed to download image: HTTP ${response .statusCode }' ,
179- );
155+ throw Exception ('Failed to set wallpaper from URL' );
180156 }
181157 } catch (e) {
182158 ScaffoldMessenger .of (context).showSnackBar (
@@ -438,15 +414,7 @@ class _FullScreenImageViewerState extends State<FullScreenImageViewer> {
438414 IconButton (
439415 icon: const Icon (Icons .wallpaper, color: Colors .white),
440416 onPressed: () {
441- // TODO: Implement set as wallpaper functionality for fullscreen view
442- ScaffoldMessenger .of (context).showSnackBar (
443- const SnackBar (
444- content: Text (
445- 'Set as wallpaper functionality can be added here' ,
446- ),
447- backgroundColor: Colors .blue,
448- ),
449- );
417+ _setAsWallpaper (widget.imageUrl);
450418 },
451419 ),
452420 ],
@@ -522,4 +490,71 @@ class _FullScreenImageViewerState extends State<FullScreenImageViewer> {
522490 );
523491 }
524492 }
493+
494+ Future <void > _setAsWallpaper (String url) async {
495+ try {
496+ final response = await http.get (Uri .parse (url));
497+
498+ if (response.statusCode == 200 ) {
499+ // Get app documents directory
500+ final Directory appDir = await getApplicationDocumentsDirectory ();
501+ final String wallpapersDir = '${appDir .path }/wallpapers' ;
502+
503+ // Create wallpapers directory if it doesn't exist
504+ final Directory wallpaperDirectory = Directory (wallpapersDir);
505+ if (! await wallpaperDirectory.exists ()) {
506+ await wallpaperDirectory.create (recursive: true );
507+ }
508+
509+ // Generate filename from URL
510+ final String fileName = url.split ('/' ).last;
511+ final String savePath = '$wallpapersDir /$fileName ' ;
512+
513+ // Save the image
514+ final File file = File (savePath);
515+ await file.writeAsBytes (response.bodyBytes);
516+
517+ // Set as wallpaper using the wallpaper service
518+ final wallpaperService = Provider .of <WallpaperService >(
519+ context,
520+ listen: false ,
521+ );
522+
523+ // Set the wallpaper using the existing method
524+ final success = await wallpaperService.setWallpaperFromUrl (url);
525+ if (! success) {
526+ throw Exception ('Failed to set wallpaper from URL' );
527+ }
528+
529+ // Show success message
530+ ScaffoldMessenger .of (context).showSnackBar (
531+ SnackBar (
532+ content: Text (context.tr (TranslationKeys .wallpaperStoreSetSuccess)),
533+ backgroundColor: AppTheme .primaryBlue,
534+ ),
535+ );
536+
537+ // Go back to the previous screen
538+ if (mounted) {
539+ Navigator .of (context).pop ();
540+ }
541+ } else {
542+ throw Exception (
543+ 'Failed to download image: HTTP ${response .statusCode }' ,
544+ );
545+ }
546+ } catch (e) {
547+ ScaffoldMessenger .of (context).showSnackBar (
548+ SnackBar (
549+ content: Text (
550+ context.tr (
551+ TranslationKeys .wallpaperStoreDownloadError,
552+ parameters: {'error' : e.toString ()},
553+ ),
554+ ),
555+ backgroundColor: Colors .red,
556+ ),
557+ );
558+ }
559+ }
525560}
0 commit comments