105 lines
3.7 KiB
Java
105 lines
3.7 KiB
Java
package com.gh.gamecenter.collection;
|
|
|
|
import android.os.Bundle;
|
|
import android.view.View;
|
|
|
|
import com.gh.common.util.CollectionUtils;
|
|
import com.gh.common.util.DataCollectionUtils;
|
|
import com.gh.common.util.DialogHelper;
|
|
import com.gh.common.util.DialogUtils;
|
|
import com.gh.common.util.EntranceUtils;
|
|
import com.gh.gamecenter.NewsDetailActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.baselist.ListAdapter;
|
|
import com.gh.gamecenter.baselist.ListFragment;
|
|
import com.gh.gamecenter.baselist.LoadType;
|
|
import com.gh.gamecenter.entity.NewsEntity;
|
|
import com.gh.gamecenter.eventbus.EBCollectionChanged;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
/**
|
|
* Created by khy on 18/07/17.
|
|
*/
|
|
|
|
public class ArticleFragment extends ListFragment<NewsEntity, ArticleViewModel> {
|
|
|
|
private String mType;
|
|
private ArticleAdapter mAdapter;
|
|
private ArticleViewModel mViewModel;
|
|
|
|
public static String COLLECTION = "collection";
|
|
public static String HISTORY = "history";
|
|
|
|
@Override
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
mType = getArguments().getString(EntranceUtils.KEY_TYPE, COLLECTION);
|
|
super.onCreate(savedInstanceState);
|
|
}
|
|
|
|
@Override
|
|
protected ListAdapter provideListAdapter() {
|
|
return mAdapter == null ? mAdapter = new ArticleAdapter(getContext(), mViewModel,this) : mAdapter;
|
|
}
|
|
|
|
@Override
|
|
protected ArticleViewModel provideListViewModel() {
|
|
if (mViewModel == null) {
|
|
mViewModel = super.provideListViewModel();
|
|
}
|
|
mViewModel.type = mType;
|
|
return mViewModel;
|
|
}
|
|
|
|
// 收藏事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBCollectionChanged changed) {
|
|
if (changed.getCollectionType().equals(CollectionUtils.CollectionType.article)) { // 取消关注/新增关注
|
|
mListViewModel.load(LoadType.REFRESH);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onListClick(View view, int position, Object data) {
|
|
NewsEntity newsEntity = (NewsEntity) data;
|
|
|
|
if (COLLECTION.equals(mType)) {
|
|
if (!newsEntity.getActive()) {
|
|
showDeleteDialog(newsEntity.getId());
|
|
return;
|
|
}
|
|
|
|
DataCollectionUtils.uploadClick(getContext(), "列表", "收藏-文章", newsEntity.getTitle());
|
|
NewsDetailActivity.startNewsDetailByCollection(getContext(), newsEntity, "(收藏:文章)");
|
|
} else {
|
|
DataCollectionUtils.uploadClick(getContext(), "列表", "浏览记录-文章", newsEntity.getTitle());
|
|
NewsDetailActivity.startNewsDetailByCollection(getContext(), newsEntity, "(浏览记录:文章)");
|
|
}
|
|
|
|
mAdapter.statNewsViews(newsEntity, position);
|
|
}
|
|
|
|
private void showDeleteDialog(String articleId) {
|
|
DialogHelper.showCenterDialog(requireContext(), "提示"
|
|
, "内容已被删除,是否取消收藏?"
|
|
, "取消收藏", "暂不"
|
|
, () -> CollectionUtils.INSTANCE.deleteCollection(requireContext(), articleId
|
|
, CollectionUtils.CollectionType.article, new CollectionUtils.OnCollectionListener() {
|
|
@Override
|
|
public void onSuccess() {
|
|
toast(R.string.collection_cancel);
|
|
mListViewModel.load(LoadType.REFRESH);
|
|
}
|
|
|
|
@Override
|
|
public void onError() {
|
|
toast(R.string.collection_cancel_failure);
|
|
}
|
|
}), () -> {
|
|
});
|
|
}
|
|
}
|