Skip to content

Commit 36a1167

Browse files
authored
Wallpaper: UI, l10n and performance improvements (#290)
* Format wallpaper model * Reduce real height of images by cacheHeight * Wallpaper: clean up model and page * Add l10n
1 parent be135a8 commit 36a1167

File tree

5 files changed

+133
-81
lines changed

5 files changed

+133
-81
lines changed

lib/l10n/app_de.arb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@
8686
"bluetoothStartDiscovery": "Starte Suche",
8787
"bluetoothStopDiscovery": "Stoppe Suche",
8888
"wallpaperPageTitle": "Hintergrund",
89+
"wallpaperPageBackgroundModeLabel": "Hintergrundmodus",
90+
"wallpaperPageBackgroundModeWallpaper": "Hintergrundbild",
91+
"wallpaperPageBackgroundModeColoredBackground": "Farbiger Hintergrund",
92+
"wallpaperPageBackgroundModeImageOfTheDay": "Hintergrund des Tages",
93+
"wallpaperPageYourWallpapersHeadline": "Ihre Hintergründe",
94+
"wallpaperPageDefaultWallpapersHeadline": "Standardhintergründe",
95+
"wallpaperPageColorModeLabel": "Farbmodus",
96+
"wallpaperPageHorizontalGradient": "horizontaler Verlauf",
97+
"wallpaperPageVerticalGradient": "vertikaler Verlauf",
98+
"wallpaperPageSolid": "einfarbig",
99+
"wallpaperPagePickerTitlePrimary": "Wählen Sie die Hauptfarbe",
100+
"wallpaperPagePickerTitleSecondary": "Wählen Sie eine zweite Farbe",
101+
"wallpaperPagePickerWheelHeading": "Farbschattierungen",
89102
"appearancePageTitle": "Erscheinungsbild",
90103
"multiTaskingPageTitle": "Multi-tasking",
91104
"notificationsPageTitle": "Benachrichtigungen",

lib/l10n/app_en.arb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@
8686
"bluetoothStartDiscovery": "Start discovery",
8787
"bluetoothStopDiscovery": "Stop discovery",
8888
"wallpaperPageTitle": "Wallpaper",
89+
"wallpaperPageBackgroundModeLabel": "Background mode",
90+
"wallpaperPageBackgroundModeWallpaper": "Wallpaper",
91+
"wallpaperPageBackgroundModeColoredBackground": "Colored background",
92+
"wallpaperPageBackgroundModeImageOfTheDay": "Image of the day",
93+
"wallpaperPageYourWallpapersHeadline": "Your wallpapers",
94+
"wallpaperPageDefaultWallpapersHeadline": "Default wallpapers",
95+
"wallpaperPageColorModeLabel": "Color mode",
96+
"wallpaperPageHorizontalGradient": "horizontal gradient",
97+
"wallpaperPageVerticalGradient": "vertical gradient",
98+
"wallpaperPageSolid": "solid",
99+
"wallpaperPagePickerTitlePrimary": "Select a primary color",
100+
"wallpaperPagePickerTitleSecondary": "Select a secondary color",
101+
"wallpaperPagePickerWheelHeading": "Color and shades",
89102
"appearancePageTitle": "Appearance",
90103
"multiTaskingPageTitle": "Multi-tasking",
91104
"notificationsPageTitle": "Notifications",

lib/view/pages/wallpaper/color_shading_option_row.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flex_color_picker/flex_color_picker.dart';
22
import 'package:flutter/material.dart';
33
import 'package:provider/provider.dart';
4+
import 'package:settings/l10n/l10n.dart';
45
import 'package:settings/utils.dart';
56
import 'package:settings/view/pages/wallpaper/wallpaper_model.dart';
67
import 'package:yaru_widgets/yaru_widgets.dart';
@@ -34,17 +35,17 @@ class ColorShadingOptionRow extends StatelessWidget {
3435
DropdownButton<ColorShadingType>(
3536
onChanged: (value) => model.colorShadingType = value,
3637
value: value,
37-
items: const [
38+
items: [
3839
DropdownMenuItem(
39-
child: Text('solid color'),
40+
child: Text(context.l10n.wallpaperPageSolid),
4041
value: ColorShadingType.solid,
4142
),
4243
DropdownMenuItem(
43-
child: Text('horizontal gradient'),
44+
child: Text(context.l10n.wallpaperPageHorizontalGradient),
4445
value: ColorShadingType.horizontal,
4546
),
4647
DropdownMenuItem(
47-
child: Text('vertical gradient'),
48+
child: Text(context.l10n.wallpaperPageVerticalGradient),
4849
value: ColorShadingType.vertical,
4950
),
5051
],
@@ -87,20 +88,18 @@ class ColorShadingOptionRow extends StatelessWidget {
8788
},
8889
width: 40,
8990
height: 40,
90-
borderRadius: 4,
91-
spacing: 5,
91+
borderRadius: 8,
92+
spacing: 10,
9293
runSpacing: 5,
9394
wheelDiameter: 155,
9495
heading: Text(
95-
primary ? 'Select a primary color' : 'Select a secondary color',
96-
style: Theme.of(context).textTheme.subtitle1,
97-
),
98-
subheading: Text(
99-
'Select color shade',
100-
style: Theme.of(context).textTheme.subtitle1,
96+
primary
97+
? context.l10n.wallpaperPagePickerTitlePrimary
98+
: context.l10n.wallpaperPagePickerTitleSecondary,
99+
style: Theme.of(context).textTheme.titleLarge,
101100
),
102101
wheelSubheading: Text(
103-
'Selected color and its shades',
102+
context.l10n.wallpaperPagePickerWheelHeading,
104103
style: Theme.of(context).textTheme.subtitle1,
105104
),
106105
showMaterialName: true,
@@ -116,16 +115,16 @@ class ColorShadingOptionRow extends StatelessWidget {
116115
selectedPickerTypeColor: Theme.of(context).colorScheme.primary,
117116
pickersEnabled: const <ColorPickerType, bool>{
118117
ColorPickerType.both: false,
119-
ColorPickerType.primary: true,
120-
ColorPickerType.accent: true,
118+
ColorPickerType.primary: false,
119+
ColorPickerType.accent: false,
121120
ColorPickerType.bw: false,
122121
ColorPickerType.custom: true,
123122
ColorPickerType.wheel: true,
124123
},
125124
).showPickerDialog(
126125
context,
127126
constraints:
128-
const BoxConstraints(minHeight: 480, minWidth: 300, maxWidth: 320),
127+
const BoxConstraints(minHeight: 300, minWidth: 300, maxWidth: 320),
129128
);
130129
}
131130
}

lib/view/pages/wallpaper/wallpaper_model.dart

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@ import 'package:path_provider/path_provider.dart';
99
import 'package:http/http.dart' as http;
1010
import '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+
1220
class 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

201208
enum WallpaperMode { solid, custom, imageOfTheDay }
202209

203-
enum ImageOfTheDayProvider { bing, nasa,}
210+
enum ImageOfTheDayProvider {
211+
bing,
212+
nasa,
213+
}

0 commit comments

Comments
 (0)