MovieMsgImpl.java 7.98 KB
/*
 * 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.model;

import android.text.TextUtils;
import com.gimi.common.cinema.model.MovieMessage;
import com.gimi.common.cinema.model.QueryBean;
import com.gimi.common.cinema.model.WrongMsg;
import com.gimi.common.cinema.utils.FileReadUtils;
import com.gimi.common.cinema.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by 李攀 on 2016/9/20
 */
public class MovieMsgImpl implements IMovieMsgModel {
    private static final String QUERY_DOUBAN_ID_URL =
            "https://api.douban.com/v2/movie/search?count=3&q=";
    private static final String GET_MOVIE_MESSAGE_URL =
            "https://api.douban.com/v2/movie/subject/";

    @Override
    public void queryDoubanId(String name, String idPath,
                              final OnGetDoubanIdListener listener) {
        String doubanId = null;
        File nameId = new File(idPath);
        if (nameId.exists()) {
            String id = FileReadUtils.readDoubanId(idPath, name);
            if (!TextUtils.isEmpty(id)) {
                String[] split = id.split("#");
                if (split.length >= 2) {
                    doubanId = split[1];
                }
            }
        }
        if (!TextUtils.isEmpty(doubanId)) {
            listener.onGetDoubanIdSuccess(doubanId);
            return;
        }

        OkHttpClientManager.getAsyn(QUERY_DOUBAN_ID_URL + name,
                new OkHttpClientManager.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        listener.onGetDoubanIdFailure(new WrongMsg());
                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            Gson gson = new Gson();
                            WrongMsg wrongMsg = gson.fromJson(response,
                                    new TypeToken<WrongMsg>() {
                                    }.getType());
                            if (wrongMsg.getCode() != 0) {
                                listener.onGetDoubanIdFailure(wrongMsg);
                            } else {
                                QueryBean bean = gson.fromJson(response,
                                        new TypeToken<QueryBean>() {
                                        }.getType());

                                List<QueryBean.SubjectsEntity> subjects =
                                        bean.getSubjects();
                                if (subjects.size() < 1) {
                                    wrongMsg.setMsg("获取结果内容为空");
                                    listener.onGetDoubanIdFailure(wrongMsg);
                                    return;
                                }
                                QueryBean.SubjectsEntity subjectsEntity =
                                        subjects.get(0);
                                String id = subjectsEntity.getId();
                                listener.onGetDoubanIdSuccess(id);
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            WrongMsg wrongMsg = new WrongMsg();
                            wrongMsg.setMsg(e.getMessage());
                            listener.onGetDoubanIdFailure(wrongMsg);
                        }
                    }
                });
    }

    @Override
    public void getMovieMessage(String doubanId,
                                final OnGetMovieMsgListener listener) {
        OkHttpClientManager.getAsyn(GET_MOVIE_MESSAGE_URL + doubanId,
                new OkHttpClientManager.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        WrongMsg wrongMsg = new WrongMsg();
                        wrongMsg.setMsg(e.getMessage());
                        listener.onGetMovieMsgFailure(wrongMsg);
                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            WrongMsg wrongMsg;
                            MovieMessage result;
                            Gson gson = new Gson();
                            wrongMsg = gson.fromJson(response,
                                    new TypeToken<WrongMsg>() {
                                    }.getType());
                            if (wrongMsg.getCode() != 0) {
                                if (listener != null)
                                    listener.onGetMovieMsgFailure(wrongMsg);
                            } else {
                                result = gson.fromJson(response,
                                        new TypeToken<MovieMessage>() {
                                        }.getType());
                                if (listener != null)
                                    listener.onGetMovieMsgSuccess(result);
                            }
                        } catch (JsonSyntaxException e) {
                            WrongMsg wrongMsg = new WrongMsg();
                            wrongMsg.setMsg(e.getMessage());
                            listener.onGetMovieMsgFailure(wrongMsg);
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void getMovieMessage(String doubanId, String name, String idPath,
                                final OnGetMovieMsgListener listener) {
        if (!TextUtils.isEmpty(doubanId)) {
            File nameId = new File(idPath);
            if (nameId.exists()) {
                String id = FileReadUtils.readDoubanId(idPath, name);
                if (!TextUtils.isEmpty(id)) {
                    String[] split = id.split("#");
                    if (split.length >= 2) {
                        doubanId = split[1];
                    }
                }
            }
            getMovieMessage(doubanId, listener);
        } else {
            queryDoubanId(name, idPath, new OnGetDoubanIdListener() {
                @Override
                public void onGetDoubanIdSuccess(String id) {
                    getMovieMessage(id, listener);
                }

                @Override
                public void onGetDoubanIdFailure(WrongMsg msg) {
                    listener.onGetMovieMsgFailure(msg);
                }
            });
        }
    }

    @Override
    public void saveEncryptMessage(String path, String content,
                                   OnSaveMovieMsgListener listener) {
        try {
            FileReadUtils.writeDateByEncrypt(path, content);
            listener.onSaveMovieMsgSuccess();
        } catch (IOException e) {
            e.printStackTrace();
            listener.onSaveMovieMsgFailure(e);
        }
    }

    interface OnGetDoubanIdListener {
        void onGetDoubanIdSuccess(String id);

        void onGetDoubanIdFailure(WrongMsg msg);
    }

    public interface OnGetMovieMsgListener {
        void onGetMovieMsgSuccess(MovieMessage mm);

        void onGetMovieMsgFailure(WrongMsg msg);
    }

    public interface OnSaveMovieMsgListener {
        void onSaveMovieMsgSuccess();

        void onSaveMovieMsgFailure(Exception e);
    }
}