@@ -9,20 +9,27 @@ import 'package:path_provider/path_provider.dart';
99import 'package:http/http.dart' as http;
1010import 'dart:convert' ;
1111
12+ const gnomeWallpaperSuffix = 'file://' ;
13+ const _gnomeUserWallpaperLocation = '/.local/share/backgrounds/' ;
14+ const _bingApiUrl =
15+ 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US' ;
16+ const _bingUrl = 'http://www.bing.com' ;
17+ const _nasaUrl =
18+ 'https://api.nasa.gov/planetary/apod?api_key=PdQXYMNV2kT9atjMjNI9gbzLqe7qF6TcEHXhexXg' ;
19+
1220class WallpaperModel extends SafeChangeNotifier {
1321 final Settings ? _wallpaperSettings;
1422 static const _pictureUriKey = 'picture-uri' ;
1523 static const _preinstalledWallpapersDir = '/usr/share/backgrounds' ;
1624 static const _colorShadingTypeKey = 'color-shading-type' ;
1725 static const _primaryColorKey = 'primary-color' ;
1826 static const _secondaryColorKey = 'secondary-color' ;
19-
27+ String errorMessage = '' ;
2028 WallpaperMode wallpaperMode = WallpaperMode .custom;
2129 ImageOfTheDayProvider imageOfTheDayProvider = ImageOfTheDayProvider .bing;
2230
23-
2431 final String ? _userWallpapersDir =
25- Platform .environment['HOME' ]! + '/.local/share/backgrounds/' ;
32+ Platform .environment['HOME' ]! + _gnomeUserWallpaperLocation ;
2633
2734 WallpaperModel (SettingsService service)
2835 : _wallpaperSettings = service.lookup (schemaBackground) {
@@ -36,62 +43,63 @@ class WallpaperModel extends SafeChangeNotifier {
3643 }
3744
3845 String get pictureUri =>
39- _wallpaperSettings! .stringValue (_pictureUriKey) ?? '' ;
46+ _wallpaperSettings? .stringValue (_pictureUriKey) ?? '' ;
4047
4148 set pictureUri (String picPathString) {
42- _wallpaperSettings! .setValue (
43- _pictureUriKey, picPathString.isEmpty ? '' : 'file://' + picPathString);
49+ _wallpaperSettings? .setValue (_pictureUriKey,
50+ picPathString.isEmpty ? '' : gnomeWallpaperSuffix + picPathString);
4451 notifyListeners ();
4552 }
4653
4754 Future <void > copyToCollection (String picPathString) async {
4855 File image = File (picPathString);
56+ if (_userWallpapersDir != null ) return ;
4957 await image
5058 .copy (_userWallpapersDir! + File (picPathString).uri.pathSegments.last);
5159 notifyListeners ();
5260 }
5361
5462 Future <void > removeFromCollection (String picPathString) async {
55- final purePath = picPathString.replaceAll ('file://' , '' );
63+ final purePath = picPathString.replaceAll (gnomeWallpaperSuffix , '' );
5664 File image = File (purePath);
5765 await image.delete ();
5866 notifyListeners ();
5967 }
6068
61- Future <List <String >> get preInstalledBackgrounds async {
69+ Future <List <String >? > get preInstalledBackgrounds async {
6270 return (await getImages (_preinstalledWallpapersDir))
63- .map ((e) => e.path)
71+ ? .map ((e) => e.path)
6472 .toList ();
6573 }
6674
67- Future <List <String >> get customBackgrounds async {
68- return (await getImages (_userWallpapersDir! )).map ((e) => e.path).toList ();
75+ Future <List <String >? > get customBackgrounds async {
76+ return (await getImages (_userWallpapersDir! ))? .map ((e) => e.path).toList ();
6977 }
7078
71- Future <Iterable <File >> getImages (String dir) async {
79+ Future <Iterable <File >? > getImages (String dir) async {
7280 return (await Directory (dir).list ().toList ())
7381 .whereType <File >()
7482 .where ((element) => lookupMimeType (element.path)! .startsWith ('image/' ));
7583 }
7684
7785 String get primaryColor =>
78- _wallpaperSettings! .stringValue (_primaryColorKey) ?? '' ;
86+ _wallpaperSettings? .stringValue (_primaryColorKey) ?? '' ;
7987
8088 set primaryColor (String colorHexValueString) {
81- _wallpaperSettings! .setValue (_primaryColorKey, colorHexValueString);
89+ _wallpaperSettings? .setValue (_primaryColorKey, colorHexValueString);
8290 notifyListeners ();
8391 }
8492
8593 String get secondaryColor =>
86- _wallpaperSettings! .stringValue (_secondaryColorKey) ?? '' ;
94+ _wallpaperSettings? .stringValue (_secondaryColorKey) ?? '' ;
8795
8896 set secondaryColor (String colorHexValueString) {
89- _wallpaperSettings! .setValue (_secondaryColorKey, colorHexValueString);
97+ _wallpaperSettings? .setValue (_secondaryColorKey, colorHexValueString);
9098 notifyListeners ();
9199 }
92100
93101 ColorShadingType get colorShadingType {
94- final type = _wallpaperSettings! .stringValue (_colorShadingTypeKey);
102+ final type = _wallpaperSettings? .stringValue (_colorShadingTypeKey);
95103 return type == 'solid'
96104 ? ColorShadingType .solid
97105 : type == 'vertical'
@@ -102,13 +110,13 @@ class WallpaperModel extends SafeChangeNotifier {
102110 set colorShadingType (ColorShadingType ? colorShadingType) {
103111 switch (colorShadingType) {
104112 case ColorShadingType .horizontal:
105- _wallpaperSettings! .setValue (_colorShadingTypeKey, 'horizontal' );
113+ _wallpaperSettings? .setValue (_colorShadingTypeKey, 'horizontal' );
106114 break ;
107115 case ColorShadingType .vertical:
108- _wallpaperSettings! .setValue (_colorShadingTypeKey, 'vertical' );
116+ _wallpaperSettings? .setValue (_colorShadingTypeKey, 'vertical' );
109117 break ;
110118 case ColorShadingType .solid:
111- _wallpaperSettings! .setValue (_colorShadingTypeKey, 'solid' );
119+ _wallpaperSettings? .setValue (_colorShadingTypeKey, 'solid' );
112120 break ;
113121 case null :
114122 return ;
@@ -138,34 +146,32 @@ class WallpaperModel extends SafeChangeNotifier {
138146
139147 void _setFirstWallpaper () async {
140148 final list = await preInstalledBackgrounds;
141- pictureUri = list.first;
149+ pictureUri = list? .first ?? '' ;
142150 }
143151
144- void refreshUrlWallpaper (){
145- setUrlWallpaperProvider (imageOfTheDayProvider);
146- }
152+ Future < void > refreshUrlWallpaper () async {
153+ await setUrlWallpaperProvider (imageOfTheDayProvider);
154+ }
147155
148- Future <void > setUrlWallpaperProvider (
156+ Future <void > setUrlWallpaperProvider (
149157 ImageOfTheDayProvider newImageOfTheDayProvider) async {
158+ errorMessage = '' ;
150159 //Set the new provider
151160 imageOfTheDayProvider = newImageOfTheDayProvider;
152161
153- //Load the user's Documents Directory to store the downloaded wallpapers
154- final Directory directory = await getApplicationDocumentsDirectory ();
162+ // Load the user's Documents Directory to store the downloaded wallpapers
163+ final directory = await getApplicationDocumentsDirectory ();
155164 final file = File ('${directory .path }/${imageOfTheDayProvider .name }.jpeg' );
156165
157166 final Map providers = {
158167 'bing' : {
159- 'apiUrl' :
160- 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US' ,
168+ 'apiUrl' : _bingApiUrl,
161169 'getImageUrl' : (jsonData) {
162- return 'http://www.bing.com' +
163- json.decode (jsonData.body)['images' ][0 ]['url' ];
170+ return _bingUrl + json.decode (jsonData.body)['images' ][0 ]['url' ];
164171 }
165172 },
166173 'nasa' : {
167- 'apiUrl' :
168- 'https://api.nasa.gov/planetary/apod?api_key=PdQXYMNV2kT9atjMjNI9gbzLqe7qF6TcEHXhexXg' , //The api uses my own api_key
174+ 'apiUrl' : _nasaUrl, //The api uses my own api_key
169175 'getImageUrl' : (jsonData) {
170176 return json.decode (jsonData.body)['url' ];
171177 }
@@ -180,18 +186,19 @@ Future<void> setUrlWallpaperProvider(
180186 return currentProvider['getImageUrl' ](imageMetadataResponse);
181187 }
182188
183- String imageUrl = await getImageUrl ();
189+ String imageUrl = '' ;
190+ imageUrl = await getImageUrl ().onError ((error, stackTrace) {
191+ return errorMessage = error.toString ();
192+ });
184193
185194 // Refetch if the image doesn't exist or the current image is older than a day
186- bool shouldRefetch =
187- ! file.existsSync () || file.lastModifiedSync ().day != DateTime .now ().day;
188-
189- if (shouldRefetch) {
195+ if (! file.existsSync () ||
196+ file.lastModifiedSync ().day != DateTime .now ().day) {
190197 var imageResponse = await http.get (Uri .parse (imageUrl));
191198 await file.writeAsBytes (imageResponse.bodyBytes);
192199 }
193200
194- //Set the wallpaper to the downloaded image path
201+ // Set the wallpaper to the downloaded image path
195202 pictureUri = file.path;
196203 }
197204}
@@ -200,4 +207,7 @@ enum ColorShadingType { solid, vertical, horizontal }
200207
201208enum WallpaperMode { solid, custom, imageOfTheDay }
202209
203- enum ImageOfTheDayProvider { bing, nasa,}
210+ enum ImageOfTheDayProvider {
211+ bing,
212+ nasa,
213+ }
0 commit comments