Skip to content

Commit 6e002f5

Browse files
YoussefLasheenFeichtmeier
authored andcommitted
Reformat the wallpaper model to accept other image providers.
1 parent 6580923 commit 6e002f5

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

lib/view/pages/wallpaper/wallpaper_model.dart

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ class WallpaperModel extends SafeChangeNotifier {
1717
static const _primaryColorKey = 'primary-color';
1818
static const _secondaryColorKey = 'secondary-color';
1919

20-
//TODO: Sync the locale of the image with the device's locale
21-
static const String _bingImageAddress =
22-
'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US';
23-
static const String _bingAddress = 'http://www.bing.com';
24-
2520
WallpaperMode wallpaperMode = WallpaperMode.custom;
21+
ImageOfTheDayProvider imageOfTheDayProvider = ImageOfTheDayProvider.bing;
22+
2623

2724
final String? _userWallpapersDir =
2825
Platform.environment['HOME']! + '/.local/share/backgrounds/';
@@ -132,7 +129,7 @@ class WallpaperModel extends SafeChangeNotifier {
132129
}
133130
break;
134131
case WallpaperMode.imageOfTheDay:
135-
refreshBingWallpaper();
132+
setUrlWallpaperProvider(imageOfTheDayProvider);
136133
break;
137134
}
138135

@@ -144,31 +141,63 @@ class WallpaperModel extends SafeChangeNotifier {
144141
pictureUri = list.first;
145142
}
146143

147-
static Future<String> getBingImageUrl() async {
148-
http.Response imageMetadataResponse =
149-
await http.get(Uri.parse(_bingImageAddress));
150-
return _bingAddress +
151-
json.decode(imageMetadataResponse.body)['images'][0]['url'];
152-
}
144+
void refreshUrlWallpaper(){
145+
setUrlWallpaperProvider(imageOfTheDayProvider);
146+
}
147+
148+
Future<void> setUrlWallpaperProvider(
149+
ImageOfTheDayProvider newImageOfTheDayProvider) async {
150+
//Set the new provider
151+
imageOfTheDayProvider = newImageOfTheDayProvider;
153152

154-
Future<void> refreshBingWallpaper() async {
153+
//Load the user's Documents Directory to store the downloaded wallpapers
155154
final Directory directory = await getApplicationDocumentsDirectory();
155+
final file = File('${directory.path}/${imageOfTheDayProvider.name}.jpeg');
156+
157+
final Map providers = {
158+
'bing': {
159+
'apiUrl':
160+
'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US',
161+
'getImageUrl': (jsonData) {
162+
return 'http://www.bing.com' +
163+
json.decode(jsonData.body)['images'][0]['url'];
164+
}
165+
},
166+
'nasa': {
167+
'apiUrl':
168+
'https://api.nasa.gov/planetary/apod?api_key=PdQXYMNV2kT9atjMjNI9gbzLqe7qF6TcEHXhexXg', //The api uses my own api_key
169+
'getImageUrl': (jsonData) {
170+
return json.decode(jsonData.body)['url'];
171+
}
172+
},
173+
};
174+
175+
//Get the url of the day using the apiUrl in the providers Map
176+
Future<String> getImageUrl() async {
177+
Map currentProvider = providers[imageOfTheDayProvider.name];
178+
http.Response imageMetadataResponse =
179+
await http.get(Uri.parse(currentProvider['apiUrl']));
180+
return currentProvider['getImageUrl'](imageMetadataResponse);
181+
}
156182

157-
final file = File('${directory.path}/bing.jpeg');
183+
String imageUrl = await getImageUrl();
158184

159185
// Refetch if the image doesn't exist or the current image is older than a day
160186
bool shouldRefetch =
161187
!file.existsSync() || file.lastModifiedSync().day != DateTime.now().day;
162188

163189
if (shouldRefetch) {
164-
var imageResponse = await http.get(Uri.parse(await getBingImageUrl()));
190+
var imageResponse = await http.get(Uri.parse(imageUrl));
165191
await file.writeAsBytes(imageResponse.bodyBytes);
166192
}
167193

168-
pictureUri = '${directory.path}/bing.jpeg';
194+
//Set the wallpaper to the downloaded image path
195+
pictureUri = file.path;
169196
}
170197
}
171198

172199
enum ColorShadingType { solid, vertical, horizontal }
173200

174201
enum WallpaperMode { solid, custom, imageOfTheDay }
202+
203+
enum ImageOfTheDayProvider { bing, nasa,}

pubspec.lock

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ packages:
374374
name: js
375375
url: "https://pub.dartlang.org"
376376
source: hosted
377-
version: "0.6.4"
377+
version: "0.6.3"
378378
json_annotation:
379379
dependency: transitive
380380
description:
@@ -410,13 +410,6 @@ packages:
410410
url: "https://pub.dartlang.org"
411411
source: hosted
412412
version: "0.12.11"
413-
material_color_utilities:
414-
dependency: transitive
415-
description:
416-
name: material_color_utilities
417-
url: "https://pub.dartlang.org"
418-
source: hosted
419-
version: "0.1.3"
420413
meta:
421414
dependency: transitive
422415
description:
@@ -896,5 +889,5 @@ packages:
896889
source: git
897890
version: "1.0.4"
898891
sdks:
899-
dart: ">=2.16.0-100.0.dev <3.0.0"
892+
dart: ">=2.15.0 <3.0.0"
900893
flutter: ">=2.5.0"

0 commit comments

Comments
 (0)