org.thanatos.flowgeek.ui.activity.DetailActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.thanatos.flowgeek.ui.activity.DetailActivity.java

Source

package org.thanatos.flowgeek.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomButtonsController;

import com.squareup.picasso.Picasso;
import com.trello.rxlifecycle.ActivityEvent;

import org.thanatos.base.domain.Entity;
import org.thanatos.base.model.SharePreferenceManager;
import org.thanatos.base.model.SharePreferenceManager.ApplicationSetting;
import org.thanatos.base.model.SharePreferenceManager.ApplicationSetting.ApplicationTheme;
import org.thanatos.base.ui.activity.BaseHoldBackActivity;
import org.thanatos.base.utils.UIHelper;
import org.thanatos.flowgeek.R;
import org.thanatos.flowgeek.UIManager;
import org.thanatos.flowgeek.bean.Article;
import org.thanatos.flowgeek.bean.Blog;
import org.thanatos.flowgeek.bean.Software;
import org.thanatos.flowgeek.bean.User;
import org.thanatos.flowgeek.event.Events;
import org.thanatos.flowgeek.event.RxBus;
import org.thanatos.flowgeek.listener.OnScrollerGoDownListener;
import org.thanatos.flowgeek.listener.ScrollerBottomLayoutDelegation;
import org.thanatos.flowgeek.presenter.DetailPresenter;
import org.thanatos.base.utils.Utilities;
import org.thanatos.flowgeek.utils.DialogFactory;

import java.text.ParseException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nucleus.factory.RequiresPresenter;

/**
 * Created by thanatos on 15/12/28.
 */
@SuppressWarnings("all")
@RequiresPresenter(DetailPresenter.class)
public class DetailActivity extends BaseHoldBackActivity<DetailPresenter> {

    public static final int DISPLAY_NEWS = 0;
    public static final int DISPLAY_BLOG = 1;
    public static final int DISPLAY_SOFTWARE = 2;
    public static final int DISPLAY_POST = 3;
    public static final int DISPLAY_TWEET = 4;
    public static final int DISPLAY_EVENT = 5;
    public static final int DISPLAY_TEAM_ISSUE_DETAIL = 6;
    public static final int DISPLAY_TEAM_DISCUSS_DETAIL = 7;
    public static final int DISPLAY_TEAM_TWEET_DETAIL = 8;
    public static final int DISPLAY_TEAM_DIARY = 9;
    public static final int DISPLAY_COMMENT = 10;

    public static final String BUNDLE_KEY_DISPLAY_TYPE = "BUNDLE_KEY_DISPLAY_TYPE";
    public static final String BUNDLE_KEY_NEWS_OBJECT = "BUNDLE_KEY_NEWS_OBJECT";

    private Article<? extends Entity> article;
    private int mCatalog;
    private ProgressDialog dialog;

    @Bind(R.id.layout_scroller)
    NestedScrollView mNestedScroller;
    @Bind(R.id.layout_bottom_panel)
    LinearLayout mBottomLayout;
    @Bind(R.id.tv_person_name)
    TextView tvName;
    @Bind(R.id.img_portrait)
    ImageView ivPortrait;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_extend)
    TextView tvExtend;
    @Bind(R.id.tv_publish_time)
    TextView tvCreateOn;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.btn_bookmark)
    TextView btnBookMark;
    @Bind(R.id.btn_comment)
    TextView btnComment;
    @Bind(R.id.layout_web_view)
    WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mCatalog = getIntent().getIntExtra(BUNDLE_KEY_DISPLAY_TYPE, DISPLAY_NEWS);
        article = (Article) getIntent().getSerializableExtra(BUNDLE_KEY_NEWS_OBJECT);
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        super.ClassName = "DetailActivity";

        initView();
        onLoading();
        initData();
        initSubscribers();
    }

    /**
     * ?
     */
    private void initSubscribers() {
        // ?id
        RxBus.with(this).setEvent(Events.EventEnum.GET_ARTICLE_ID).setEndEvent(ActivityEvent.DESTROY)
                .onNext((events) -> {
                    Events<Long> event = new Events<Long>();
                    event.what = events.getMessage();
                    event.message = article.getId();
                    RxBus.getInstance().send(event);
                }).create();

        // id
        RxBus.with(this).setEvent(Events.EventEnum.GET_ARTICLE_OWNER_ID).setEndEvent(ActivityEvent.DESTROY)
                .onNext((events) -> {
                    Events<Long> event = new Events<Long>();
                    event.what = events.getMessage();
                    event.message = article.getAuthorId();
                    RxBus.getInstance().send(event);
                }).create();

        // ?catalog
        RxBus.with(this).setEvent(Events.EventEnum.GET_ARTICLE_CATALOG).setEndEvent(ActivityEvent.DESTROY)
                .onNext((events) -> {
                    Events<Integer> event = new Events<Integer>();
                    event.what = events.getMessage();
                    event.message = mCatalog;
                    RxBus.getInstance().send(event);
                }).create();
    }

    /**
     * init data
     */
    @SuppressWarnings("all")
    private void initData() {
        switch (mCatalog) {
        case DISPLAY_SOFTWARE:
            tvTitle.setText(article.getTitle());
            Software software = article.<Software>getArticle();
            if (software == null)
                break;
            tvTitle.setText(software.getTitle());
            tvName.setText("?" + software.getLanguage());
            tvName.setTextColor(tvCreateOn.getCurrentTextColor());
            tvCreateOn.setText("" + software.getRecordTime());
            tvType.setText("???" + software.getLicense());
            tvType.setVisibility(View.VISIBLE);
            tvExtend.setText("System" + software.getOs());
            tvExtend.setVisibility(View.VISIBLE);
            Picasso.with(this).load(software.getLogo()).into(ivPortrait);
            break;
        case DISPLAY_BLOG:
            tvType.setVisibility(View.VISIBLE);
            if (article.getArticle() == null)
                break;
            if (article.<Blog>getArticle().getDocumenttype() == Blog.DOC_TYPE_REPASTE) {
                tvType.setText(getResources().getText(R.string.repaste));
            } else {
                tvType.setText(getResources().getText(R.string.original));
            }
        case DISPLAY_NEWS:
        case DISPLAY_EVENT:
        case DISPLAY_POST:
            tvTitle.setText(article.getTitle());
            tvName.setText(article.getAuthor());
            if (!Utilities.isEmpty(article.getPubDate())) {
                try {
                    tvCreateOn.setText(getResources().getText(R.string.publish_on)
                            + Utilities.dateFormat(article.getPubDate()));
                } catch (ParseException e) {
                    e.printStackTrace();
                    tvCreateOn.setText(article.getPubDate());
                }
            }
            if (!Utilities.isEmpty(article.getPortrait())) {
                Picasso.with(this).load(article.getPortrait()).into(ivPortrait);
            }
            break;
        }

        btnComment.setText(
                getResources().getString(R.string.comment) + "(" + String.valueOf(article.getCmmCount()) + ")");

        if (article.getFavorite() == 1) {
            btnBookMark.setCompoundDrawables(null,
                    getResources().getDrawable(R.mipmap.icon_star_mid_yes, getTheme()), null, null);
        }

        mWebView.loadDataWithBaseURL("", loadHTMLData(article.getBody() == null ? "" : article.getBody()),
                "text/html", "UTF-8", "");
    }

    /**
     * init view
     */
    private void initView() {
        // init WebView
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);

        btnComment.setClickable(false);

        ScrollerBottomLayoutDelegation.delegation(mNestedScroller, mBottomLayout);
    }

    /**
     * load html form remote
     * @param body
     * @return
     */
    private String loadHTMLData(String body) {
        StringBuilder builder = new StringBuilder().append(UIManager.WEB_STYLE).append(UIManager.WEB_LOAD_IMAGES);

        SharedPreferences preferences = SharePreferenceManager.getApplicationSetting(this);
        int theme = preferences.getInt(ApplicationSetting.KEY_THEME, ApplicationTheme.LIGHT.getKey());
        if (theme == SharePreferenceManager.ApplicationSetting.ApplicationTheme.DARK.getKey()) {
            builder.append("<body class='night'><div class='contentstyle' id='article_body'>");
        } else {
            builder.append("<body><div class='contentstyle' id='article_body'>");
        }

        return builder.append(setupContentImage(body)).append("</div></body>").toString();
    }

    /**
     * HTML??
     * @param body
     * @return
     */
    public static String setupContentImage(String body) {
        //  imgwidth,height
        body = body.replaceAll("(<img[^>]*?)\\s+width\\s*=\\s*\\S+", "$1");
        body = body.replaceAll("(<img[^>]*?)\\s+height\\s*=\\s*\\S+", "$1");
        // ?
        body = body.replaceAll("(<img[^>]+src=\")(\\S+)\"", "$1$2\" onClick=\"showImagePreview('$2')\"");
        return body;
    }

    /**
     * ?, presenter
     * @param article
     */
    public void onLoadFinished(Article article) {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
        this.article = article;
        initData();

        btnComment.setClickable(true);

        // make portrair and name clickable
        User user = new User();
        user.setName(article.getAuthor());
        user.setId(article.getAuthorId());
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManager.toUserHome(DetailActivity.this, user);
            }
        };
        tvName.setOnClickListener(listener);
        ivPortrait.setOnClickListener(listener);
    }

    /**
     * , presenter
     */
    public void onLoadFailure() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
        Toast.makeText(DetailActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
    }

    /**
     * , dialog
     */
    public void onLoading() {
        dialog = DialogFactory.getFactory().getLoadingDialog(this);
        dialog.show();
    }

    @OnClick(R.id.btn_comment)
    void onClickCmm() {
        UIManager.showCmmActivity(this);
    }

    @OnClick(R.id.btn_like)
    void onClickLike() {
        // TODO ???
    }

    @OnClick(R.id.btn_bookmark)
    void onClickBookmark() {
        // TODO ?
    }

    @OnClick(R.id.btn_share)
    void onClickShare() {
        // TODO 
    }

    public int getCatalog() {
        return mCatalog;
    }

    public Article getNews() {
        return article;
    }

    @Override
    protected String onSetTitle() {
        switch (mCatalog) {
        case DISPLAY_NEWS:
            return getResources().getString(R.string.news_detail);
        case DISPLAY_BLOG:
            return getResources().getString(R.string.blog_detail);
        case DISPLAY_POST:
            return getResources().getString(R.string.post_detail);
        case DISPLAY_TWEET:
            return getResources().getString(R.string.tweet_detail);
        case DISPLAY_SOFTWARE:
            return getResources().getString(R.string.software_detail);
        }
        return "---";
    }

    @Override
    protected int onBindLayout() {
        return R.layout.activity_news_detail;
    }
}