· 8 years ago · Mar 10, 2018, 07:04 AM
1public class MovieContract {
2static final int LIST = 0;
3static final int ITEM = 1;
4public static final int ID = 0;
5public static final int TITLE = 1;
6public static final int ORIGINAL_TITLE = 2;
7public static final int OVERVIEW = 3;
8public static final int HOMEPAGE = 4;
9public static final int BUDGET = 5;
10public static final int BACKDROP_PATH = 6;
11public static final int POSTER_PATH = 7;
12public static final int RUNTIME = 8;
13public static final int STATUS = 9;
14public static final int ADULT = 10;
15public static final int RELEASE_DATE = 11;
16public static final int ORIGINAL_LANGUAGE = 12;
17public static final int REVENUE = 13;
18public static final int DOWNLOADED = 14;
19
20public static abstract class MovieEntry {
21 public static final String TABLE = "movie";
22
23 public static final String BUDGET = "budget";
24 public static final String BACKDROP_PATH = "backdrop_path";
25 public static final String STATUS = "status";
26 public static final String RUNTIME = "runtime";
27 public static final String ADULT = "adult";
28 public static final String HOMEPAGE = "homepage";
29 public static final String ID = "id";
30 public static final String TITLE = "title";
31 public static final String ORIGINAL_LANGUAGE = "original_language";
32 public static final String OVERVIEW = "overview";
33 public static final String RELEASE_DATE = "release_date";
34 public static final String ORIGINAL_TITLE = "original_title";
35 public static final String POSTER_PATH = "poster_path";
36 public static final String REVENUE = "revenue";
37 public static final String DOWNLOADED = "downloaded";
38}
39
40public static final String CREATE = "CREATE TABLE " +
41 MovieEntry.TABLE + "(" +
42 MovieEntry.ID + " INTEGER PRIMARY KEY," +
43 MovieEntry.TITLE + " TEXT," +
44 MovieEntry.ORIGINAL_TITLE + " TEXT," +
45 MovieEntry.OVERVIEW + " TEXT," +
46 MovieEntry.HOMEPAGE + " TEXT," +
47 MovieEntry.BUDGET + " INTEGER," +
48 MovieEntry.BACKDROP_PATH + " TEXT," +
49 MovieEntry.POSTER_PATH + " TEXT," +
50 MovieEntry.RUNTIME + " INTEGER," +
51 MovieEntry.STATUS + " TEXT," +
52 MovieEntry.ADULT + " BOOLEAN," +
53 MovieEntry.RELEASE_DATE + " TEXT," +
54 MovieEntry.ORIGINAL_LANGUAGE + " TEXT," +
55 MovieEntry.REVENUE + " INTEGER," +
56 MovieEntry.DOWNLOADED + " INTEGER " +
57 ")";
58
59public static final String DROP = "DROP TABLE IF EXISTS " +MovieEntry.TABLE ;
60
61public static long insert(Movie movie) {
62 long id;
63 SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
64
65 ContentValues values = new ContentValues();
66 values.put(MovieEntry.ID, movie.getId());
67 values.put(MovieEntry.TITLE, movie.getTitle());
68 values.put(MovieEntry.ORIGINAL_TITLE, movie.getOriginal_title());
69 values.put(MovieEntry.OVERVIEW, movie.getOverview());
70 values.put(MovieEntry.BACKDROP_PATH, movie.getBackdrop_path());
71 values.put(MovieEntry.POSTER_PATH, movie.getPoster_path());
72 values.put(MovieEntry.ADULT, movie.isAdult());
73 values.put(MovieEntry.RELEASE_DATE, movie.getRelease_date());
74 values.put(MovieEntry.ORIGINAL_LANGUAGE, movie.getOriginal_language());
75 values.put(MovieEntry.DOWNLOADED, LIST);
76
77 id = db.insert(MovieEntry.TABLE, null, values);
78 DatabaseManager.getInstance().closeDatabase();
79 return id;
80}
81
82public static long update(Movie movie) {
83 long id;
84 SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
85 ContentValues values = new ContentValues();
86
87 values.put(MovieEntry.ID, movie.getId());
88 values.put(MovieEntry.TITLE, movie.getTitle());
89 values.put(MovieEntry.ORIGINAL_TITLE, movie.getOriginal_title());
90 values.put(MovieEntry.OVERVIEW, movie.getOverview());
91 values.put(MovieEntry.HOMEPAGE, movie.getHomepage());
92 values.put(MovieEntry.BUDGET, movie.getBudget());
93 values.put(MovieEntry.BACKDROP_PATH, movie.getBackdrop_path());
94 values.put(MovieEntry.POSTER_PATH, movie.getPoster_path());
95 values.put(MovieEntry.RUNTIME, movie.getRuntime());
96 values.put(MovieEntry.STATUS, movie.getStatus());
97 values.put(MovieEntry.ADULT, movie.isAdult());
98 values.put(MovieEntry.RELEASE_DATE, movie.getRelease_date());
99 values.put(MovieEntry.ORIGINAL_LANGUAGE, movie.getOriginal_language());
100 values.put(MovieEntry.REVENUE, movie.getRevenue());
101 values.put(MovieEntry.DOWNLOADED, ITEM);
102
103 id = db.update(MovieEntry.TABLE, values, "id=" + movie.getId(), null);
104 DatabaseManager.getInstance().closeDatabase();
105 return id;
106}
107
108public static void delete() {
109 SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
110 db.delete(MovieEntry.TABLE, null, null);
111 DatabaseManager.getInstance().closeDatabase();
112}
113
114public static ArrayList<Movie> getAllMovies() {
115 SQLiteDatabase db = DatabaseManager.getInstance().openReadableDatabase();
116 Cursor c = db.rawQuery("SELECT * FROM " + MovieEntry.TABLE, null);
117 if (!c.moveToFirst())
118 return null;
119 ArrayList<Movie> movies = new ArrayList<>();
120 do {
121 Movie movie = new Movie();
122 movie.setId(c.getInt(ID));
123 movie.setTitle(c.getString(TITLE));
124 movie.setOriginal_title(c.getString(ORIGINAL_TITLE));
125 movie.setOverview(c.getString(OVERVIEW));
126 movie.setBackdrop_path(c.getString(BACKDROP_PATH));
127 movie.setPoster_path(c.getString(POSTER_PATH));
128 movie.setAdult(c.getInt(ADULT) > 0);
129 movie.setRelease_date(c.getString(RELEASE_DATE));
130 movie.setOriginal_language(c.getString(ORIGINAL_LANGUAGE));
131 } while (c.moveToNext());
132 c.close();
133 DatabaseManager.getInstance().closeDatabase();
134 return movies;
135}
136
137public static Movie getMovieById(int id) {
138 SQLiteDatabase db = DatabaseManager.getInstance().openReadableDatabase();
139 Cursor c = db.rawQuery("SELECT * FROM " + MovieEntry.TABLE +
140 " WHERE " + MovieEntry.ID + "=" + id + " AND " + MovieEntry.DOWNLOADED + "=" + ITEM, null);
141 if (!c.moveToFirst())
142 return null;
143 Movie movie = new Movie();
144 movie.setId(c.getInt(ID));
145 movie.setTitle(c.getString(TITLE));
146 movie.setOriginal_title(c.getString(ORIGINAL_TITLE));
147 movie.setOverview(c.getString(OVERVIEW));
148 movie.setHomepage(c.getString(HOMEPAGE));
149 movie.setBudget(c.getInt(BUDGET));
150 movie.setBackdrop_path(c.getString(BACKDROP_PATH));
151 movie.setPoster_path(c.getString(POSTER_PATH));
152 movie.setRuntime(c.getInt(RUNTIME));
153 movie.setStatus(c.getString(STATUS));
154 movie.setAdult(c.getInt(ADULT) > 0);
155 movie.setRelease_date(c.getString(RELEASE_DATE));
156 movie.setOriginal_language(c.getString(ORIGINAL_LANGUAGE));
157 movie.setRevenue(c.getInt(REVENUE));
158 c.close();
159 DatabaseManager.getInstance().closeDatabase();
160 return movie;
161}}
162
163public abstract class BaseModel<T> implements Base.Model<T> {
164String TAG = BaseModel.class.getSimpleName();
165static final int PAGE_START = 1;
166int current = PAGE_START;
167int last = 0;}
168
169public class MovieModel extends BaseModel<Movie> {
170@Override
171public void onLoadPopularDb(Base.Listener<Movie> listener) {
172 ArrayList<Movie> movies = MovieContract.getAllMovies();
173 if (movies != null && movies.size() != 0) {
174 Log.d("MOVIEMODEL", movies.size() + "");
175 listener.onFinishedDb(movies);
176 } else
177 listener.onMessage("NO HAY DATOS GUARDADOS EN LA DB");
178}
179
180@Override
181public void loadPopularFirstPage(final Base.Listener<Movie> listener) {
182 Service.Movies service = Client.getClient().create(Service.Movies.class);
183 Call<List> call = service.getMovieChanges(BuildConfig.THE_MOVIE_DB_API_TOKEN);
184 call.enqueue(new Callback<List>() {
185 @Override
186 public void onResponse(Call<List> call, Response<List> response) {
187 switch (response.code()) {
188 case 200:
189 List list = ListContract.getListByType(ListContract.MOVIE);
190 List movieList = response.body();
191 movieList.setType(List.TYPE_MOVIE);
192 if (list == null) {
193 listener.onMessage("No se ha inicializado");
194 long id = ListContract.insert(movieList);
195 Log.i("LIST", "type: " + movieList.getType() + " total:" + movieList.getTotal_pages() + " ID:" + id);
196 loadFirst(listener, false, true);
197 } else {
198 Log.i("MOVIEMODEL", "type: " + list.getType() + " totalPages: " + list.getTotal_pages());
199 if (list.getTotal_pages() != movieList.getTotal_pages()) {
200 ListContract.update(movieList);
201 loadFirst(listener, true, true);
202 } else
203 loadFirst(listener, true, false);
204 }
205 break;
206 case 401:
207 listener.onError("Invalid API key: You must be granted a valid key");
208 break;
209 case 404:
210 listener.onError("The resource you requested could not be found");
211 break;
212 }
213 }
214
215 @Override
216 public void onFailure(Call<List> call, Throwable t) {
217 listener.onError(t.getLocalizedMessage());
218 }
219 });
220}
221
222@Override
223public void loadPopularNextPage(final Base.Listener<Movie> listener) {
224 current++;
225 if (current <= last) {
226 Service.Movies service = Client.getClient().create(Service.Movies.class);
227 Call<List<Movie>> call = service.getPopularMovies(BuildConfig.THE_MOVIE_DB_API_TOKEN, "es-ES", current);
228 call.enqueue(new Callback<List<Movie>>() {
229 @Override
230 public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
231 switch (response.code()) {
232 case 200:
233 List<Movie> movies = response.body();
234 listener.onFinishedNext(movies.getResults(), (current == last));
235 break;
236 case 401:
237 listener.onError("Invalid API key: You must be granted a valid key");
238 break;
239 case 404:
240 listener.onError("The resource you requested could not be found");
241 break;
242 }
243 }
244
245 @Override
246 public void onFailure(Call<List<Movie>> call, Throwable t) {
247 listener.onError(t.getLocalizedMessage());
248 }
249 });
250 } else
251 listener.onError("do not have more pages");
252}
253
254private void loadFirst(final Base.Listener<Movie> listener, final boolean isSavedInDb, final boolean isChanged) {
255 Service.Movies service = Client.getClient().create(Service.Movies.class);
256 Call<List<Movie>> call = service.getPopularMovies(BuildConfig.THE_MOVIE_DB_API_TOKEN, "es-ES", 1);
257 call.enqueue(new Callback<List<Movie>>() {
258 @Override
259 public void onResponse(Call<List<Movie>> call, Response<List<Movie>> response) {
260 switch (response.code()) {
261 case 200:
262 List<Movie> movies = response.body();
263 last = movies.getTotal_pages();
264 if (isChanged) {
265 if (isSavedInDb)
266 MovieContract.delete();
267 for (Movie movie : movies.getResults()) {
268 MovieContract.insert(movie);
269 }
270 }
271 listener.onFinishedFirst(movies.getResults(), (current == last), last);
272 break;
273 case 401:
274 listener.onError("Invalid API key: You must be granted a valid key");
275 break;
276 case 404:
277 listener.onError("The resource you requested could not be found");
278 break;
279 }
280 }
281
282 @Override
283 public void onFailure(Call<List<Movie>> call, Throwable t) {
284 listener.onError(t.getLocalizedMessage());
285 }
286 });
287}}