MovieDetailPresenter.java
9.41 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (c) 2016 wugian
* 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.xgimi.gimicinema.presenter;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.gimi.common.cinema.model.Constant;
import com.gimi.common.cinema.model.LocalMovieMessage;
import com.gimi.common.cinema.model.MovieMessage;
import com.gimi.common.cinema.model.Rating;
import com.gimi.common.cinema.model.SambaMsg;
import com.gimi.common.cinema.model.WrongMsg;
import com.gimi.common.cinema.utils.FolderUtils;
import com.gimi.common.cinema.utils.LeeImageLoader;
import com.gimi.common.cinema.utils.MovieMessageUtils;
import com.gimi.common.cinema.utils.NameFilterUtils;
import com.gimi.common.cinema.utils.SambaFileCharge;
import com.gimi.common.cinema.utils.Utils;
import com.google.gson.Gson;
import com.xgimi.gimicinema.BuildConfig;
import com.xgimi.gimicinema.R;
import com.xgimi.gimicinema.activity.CinemaConfig;
import com.xgimi.gimicinema.model.DBUpdateModelImpl;
import com.xgimi.gimicinema.model.IDBUpdateModel;
import com.xgimi.gimicinema.model.IMovieMsgModel;
import com.xgimi.gimicinema.model.IOtherModel;
import com.xgimi.gimicinema.model.IPosterModel;
import com.xgimi.gimicinema.model.MovieListModelImpl;
import com.xgimi.gimicinema.model.MovieMsgImpl;
import com.xgimi.gimicinema.model.OtherModelImpl;
import com.xgimi.gimicinema.model.PosterModelImpl;
import com.xgimi.gimicinema.mview.IMovieDetailView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 李攀 on 2016/9/20
*/
public class MovieDetailPresenter extends BasePresenter
implements MovieMsgImpl.OnGetMovieMsgListener,
PosterModelImpl.OnPosterDownloadListener,
MovieMsgImpl.OnSaveMovieMsgListener,
MovieListModelImpl.OnGetMovieListListener,
DBUpdateModelImpl.OnUpdateDbListener,
OtherModelImpl.OnScanOtherMovieListener {
private Context context;
private IMovieDetailView view;
private IMovieMsgModel movieMsgModel;
private IPosterModel posterModel;
private MovieListModelImpl movieListModel;
private IDBUpdateModel dbModel;
private IOtherModel otherModel;
private SambaMsg sambaMsg;
private String idPath;
private String movieFolderPath;
private LocalMovieMessage localMovieMessage;
public MovieDetailPresenter(Context context, IMovieDetailView view) {
this.view = view;
this.context = context;
movieMsgModel = new MovieMsgImpl();
posterModel = new PosterModelImpl();
movieListModel = new MovieListModelImpl();
dbModel = new DBUpdateModelImpl();
otherModel = new OtherModelImpl();
}
public void loadMovieMsg() {
if (sambaMsg == null) {
sambaMsg = Utils.getSambaMsg(context);
}
if (TextUtils.isEmpty(idPath)) {
idPath = sambaMsg.getLocalPath() + "name-id.txt";
if (BuildConfig.MACHINE_TYPE.equals("himedia")) {
idPath = CinemaConfig.BASIC_ROOT + "/name-id.txt";
}
}
localMovieMessage = view.getMovieItem();
// if (TextUtils.isEmpty(movieFolderPath)) {
// view.finishSelf();
// return;
// }
movieFolderPath = FolderUtils.getMovieFolderPath(
localMovieMessage.getPlayPath(), sambaMsg.getIp(), null);
view.setMovieName(NameFilterUtils.getName(
localMovieMessage.getMovieName()));
MovieMessage mm = MovieMessageUtils.getLocalMovieMessage(
movieFolderPath);
if (mm != null) {
view.loadMovieMsg(mm);
} else {
String doubanId = view.getDoubanId();
view.showProgress();
movieMsgModel.getMovieMessage(
doubanId, localMovieMessage.getMovieName(), idPath, this);
}
String poster;
if (SambaFileCharge.fileExist((localMovieMessage.getPosterPath()))) {
poster = localMovieMessage.getPosterPath();
view.loadMoviePoster(poster);
} else {
poster = MovieMessageUtils.getLocalMoviePoster(movieFolderPath);
view.loadMoviePoster(poster);
if (mm != null) {
posterModel.downloadEncryptPoster(mm.getImages().getLarge(),
movieFolderPath + Constant.MOVIE_POSTER_NEW, this);
}
}
}
public void handlerClick(int id) {
switch (id) {
case R.id.playMovie:
view.playMovie();
break;
case R.id.agreeTo:
view.agreeTo();
break;
}
}
public void loadRecommend() {
movieListModel.getRecommendMovieList(context, this);
}
@Override
public void onGetMovieMsgSuccess(MovieMessage mm) {
view.updateDBData();
view.loadMovieMsg(mm);
reloadLocalMsg(mm);
dbModel.update(context, localMovieMessage, this);
String content = new Gson().toJson(mm);
movieMsgModel.saveEncryptMessage(movieFolderPath
+ Constant.MOVIE_MESSAGE_NAME_NEW, content, this);
if (TextUtils.isEmpty(localMovieMessage.getPosterPath())
|| !SambaFileCharge.fileExist(localMovieMessage.getPosterPath())) {
posterModel.downloadEncryptPoster(mm.getImages().getLarge(),
movieFolderPath + Constant.MOVIE_POSTER_NEW, this);
}
view.hideProgress();
}
private void reloadLocalMsg(MovieMessage mm) {
Rating rating = mm.getRating();
double average = 0;//rating
if (rating != null) {
average = rating.getAverage();
}
String[] genres = mm.getGenres();
//clazz
if (!TextUtils.isEmpty(mm.getId())) {
localMovieMessage.setDoubanId(mm.getId());
}
//types
StringBuilder types = new StringBuilder();
localMovieMessage.setDoubanRating(average);
if (genres.length > 0) {
types.append(genres[0]);
}
for (int i = 1; i < genres.length; i++) {
types.append("/").append(genres[i]);
}
localMovieMessage.setScreenTime(mm.getYear());
localMovieMessage.setClassDescribe(types.toString());
}
@Override
public void onGetMovieMsgFailure(WrongMsg msg) {
view.hideProgress();
}
@Override
public void downloadEnPosterSuccess(String path) {
Log.d("lovely", "╟ " + path);
LeeImageLoader.clearCache(path);
localMovieMessage.setPosterPath(path);
dbModel.update(context, localMovieMessage, this);
view.loadMoviePoster(path);
}
@Override
public void downloadEnPosterFailure(Exception e) {
Log.e("lovely", "╟ " + e.getMessage());
}
@Override
public void onSaveMovieMsgSuccess() {
Log.d("lovely", "╟ onSaveMovieMsgSuccess ");
}
@Override
public void onSaveMovieMsgFailure(Exception e) {
Log.e("lovely", "╟ " + e.getMessage());
}
@Override
public void success(final List<LocalMovieMessage> list) {
view.showData(list);
}
@Override
public void failure() {
}
@Override
public void updateDBSuccess() {
Log.d("lovely", "╟ updateDBSuccess ");
}
@Override
public void updateDBFailure() {
Log.d("lovely", "╟ updateDBFailure ");
}
public void playPath() {
ArrayList<String> otherMovie = view.getOtherMovie();
if (otherMovie != null && otherMovie.size() > 1 &&
!view.getMovieItem().getPlayPath().contains("./BD")) {
view.showOtherMovie();
} else {
playPath(null);
}
}
private void recordPlayMsg() {
LocalMovieMessage movieItem = view.getMovieItem();
int playCount = MovieMessageUtils.getPlayCount(movieFolderPath);
movieItem.setCount(playCount + 1);
dbModel.updateMovieCount(context, movieItem);
dbModel.updatePlayTime(context, movieItem);
MovieMessageUtils.writePlayCount(movieFolderPath, movieItem.getCount());
}
public void playPath(String path) {
String playPath = TextUtils.isEmpty(path)
? view.getMovieItem().getPlayPath() : path;
if (SambaFileCharge.fileExist(playPath)) {
view.jumpToPlay(playPath);
} else {
view.showMsg("电影被移除");
}
recordPlayMsg();
}
public void loadOtherMovies() {
otherModel.scanOtherMovies(context, view.getMovieItem().getPlayPath(),
this);
}
@Override
public void onScanOtherMovieSuccess(ArrayList<String> result) {
view.updateOtherMovieList(result);
}
public void updateCurrentMovie(LocalMovieMessage lmm) {
if (SambaFileCharge.fileExist(lmm.getPlayPath())) {
view.updateCurrentMovie(lmm);
loadRecommend();
loadMovieMsg();
loadOtherMovies();
} else {
view.showMsg("电影被移除");
}
}
}