-
Notifications
You must be signed in to change notification settings - Fork 19
/
TmdbConfig.java
217 lines (178 loc) · 6.86 KB
/
TmdbConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (C) 2018, Brian He
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.brianspace.moviebrowser.models;
import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.util.Log;
import com.github.brianspace.moviebrowser.repository.IConfigStore;
import com.github.brianspace.moviebrowser.repository.IMovieDbService;
import com.github.brianspace.moviebrowser.repository.data.Configuration.ImageConfig;
import javax.inject.Inject;
/**
* Configuration used in this app, from TMDb Web API.
* For model layer internal use.
*/
class TmdbConfig implements IImageConfig {
// region Private Constants
/**
* Tag for logcat.
*/
private static final String TAG = TmdbConfig.class.getSimpleName();
// endregion
// region Private Fields
/**
* Interface for accessing TMDb Web API.
*/
private final IMovieDbService movieDbService;
/**
* Interface for configuration store.
*/
private final IConfigStore configStore;
/**
* Configuration builder for backdrop images.
*/
private final ImageSizeConfigBuilder backdropSizeConfigBuilder;
/**
* Configuration builder for poster images.
*/
private final ImageSizeConfigBuilder posterSizeConfigBuilder;
/**
* Base URL for images.
*/
private String imageBaseUrl = Constants.DEFAULT_IMAGE_BASE_URL;
/**
* Configuration for movie backdrop images. A new instance will be created when value changes.
*/
private ImageSizesConfig backdropSizeConfig;
/**
* Configuration for movie poster images. A new instance will be created when value changes.
*/
private ImageSizesConfig posterSizeConfig;
// endregion
// region Private Types
/**
* Class to help the creation of ImageSizesConfig.
*/
private class ImageSizeConfigBuilder {
/**
* Key to save & load config value from config store.
*/
private final String configKey;
/**
* Create a new instance for a specific image type.
*
* @param configKey Key to save & load config value from config store.
*/
/* default */ ImageSizeConfigBuilder(@NonNull final String configKey) {
this.configKey = configKey;
}
/**
* Load from config store.
*
* @return a new ImageSizesConfig instance.
*/
/* default */ ImageSizesConfig loadFromConfigStore() {
return ImageSizesConfig.loadFromConfigStore(imageBaseUrl, configStore, configKey);
}
/**
* Update config and save the new value if needed.
*
* @param originalConfig the original ImageSizesConfig instance.
* @param sizeStrings new image size strings.
* @return a new instance of ImageSizesConfig if the new config is different from the original one, otherwise
* the original instance is returned.
*/
/* default */ ImageSizesConfig updateAndSave(final ImageSizesConfig originalConfig,
final Iterable<String> sizeStrings) {
final ImageSizesConfig newConfig = ImageSizesConfig.createFromImageSizes(imageBaseUrl, sizeStrings);
final boolean changed = !originalConfig.sameAs(newConfig);
if (changed) {
ImageSizesConfig.saveToConfigStore(newConfig, configStore, configKey);
}
return changed ? newConfig : originalConfig;
}
}
// endregion
// region Constructors
/**
* Create a new instance of the TmdbConfig class.
*
* @param movieDbService TMDb Web API interface.
* @param configStore interface for configuration store.
*/
@Inject
/* default */ TmdbConfig(@NonNull final IMovieDbService movieDbService, @NonNull final IConfigStore configStore) {
this.movieDbService = movieDbService;
this.configStore = configStore;
backdropSizeConfigBuilder = new ImageSizeConfigBuilder(IConfigStore.KEY_TMDB_BACKDROP_SIZES);
posterSizeConfigBuilder = new ImageSizeConfigBuilder(IConfigStore.KEY_TMDB_POSTER_SIZES);
readFromConfigStore();
}
// endregion
// region Public Overrides
@Override
public final String getBackdropBaseUrl(final int width) {
return backdropSizeConfig.getImageBaseUrl(width);
}
@Override
public final String getPosterBaseUrl(final int width) {
return posterSizeConfig.getImageBaseUrl(width);
}
// endregion
// region Public Methods
/**
* Initialize. Should be call only once on application start.
*/
@SuppressLint("CheckResult") // We won't cancel the init process so the return value can be safely ignored.
public void init() {
movieDbService.getConfiguration().subscribe(
result -> updateAndSave(result.getImageConfig()),
err -> Log.e(TAG, err.getMessage()));
}
// endregion
// region Private Methods
private void readFromConfigStore() {
final String baseUrl = configStore.getConfigItem(IConfigStore.KEY_TMDB_IMAGE_BASE_URL);
updateBaseUrl(baseUrl, false);
backdropSizeConfig = backdropSizeConfigBuilder.loadFromConfigStore();
posterSizeConfig = posterSizeConfigBuilder.loadFromConfigStore();
}
private void updateAndSave(final ImageConfig config) {
if (config != null) {
final String baseUrl = config.getBaseUrl();
updateBaseUrl(baseUrl, true);
final Iterable<String> backdropSizeStrings = config.getBackdropSizes();
backdropSizeConfig = backdropSizeConfigBuilder.updateAndSave(backdropSizeConfig, backdropSizeStrings);
final Iterable<String> posterSizeStrings = config.getPosterSizes();
posterSizeConfig = posterSizeConfigBuilder.updateAndSave(posterSizeConfig, posterSizeStrings);
}
}
/**
* Update base URL.
*
* @param baseUrl new value for base URL.
* @param save save flag. True to save to config store.
*/
private void updateBaseUrl(final String baseUrl, final boolean save) {
if (baseUrl != null && !baseUrl.isEmpty() && !baseUrl.equals(imageBaseUrl)) {
imageBaseUrl = baseUrl;
if (save) {
configStore.saveConfigItem(IConfigStore.KEY_TMDB_IMAGE_BASE_URL, baseUrl);
}
}
}
// endregion
}