117 lines
4.2 KiB
Java
117 lines
4.2 KiB
Java
package com.gh.gamecenter.collection;
|
|
|
|
import android.view.View;
|
|
|
|
import com.gh.common.history.HistoryDatabase;
|
|
import com.gh.common.util.CollectionUtils;
|
|
import com.gh.common.util.DialogUtils;
|
|
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.baselist.NormalListViewModel;
|
|
import com.gh.gamecenter.eventbus.EBCollectionChanged;
|
|
import com.gh.gamecenter.manager.UserManager;
|
|
import com.gh.gamecenter.qa.answer.detail.AnswerDetailActivity;
|
|
import com.gh.gamecenter.qa.entity.AnswerEntity;
|
|
import com.gh.gamecenter.qa.entity.Questions;
|
|
import com.gh.gamecenter.qa.questions.detail.QuestionsDetailActivity;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
import java.util.List;
|
|
|
|
import io.reactivex.Single;
|
|
|
|
/**
|
|
* Created by khy on 22/12/17.
|
|
*/
|
|
|
|
public class AnswerFragment extends ListFragment<AnswerEntity, NormalListViewModel> {
|
|
|
|
private AnswerAdapter mAdapter;
|
|
private Type mType;
|
|
|
|
public static AnswerFragment getInstance(Type type) {
|
|
AnswerFragment fragment = new AnswerFragment();
|
|
fragment.mType = type;
|
|
return fragment;
|
|
}
|
|
|
|
@Override
|
|
protected ListAdapter provideListAdapter() {
|
|
return mAdapter == null ? mAdapter = new AnswerAdapter(getContext(), this, mEntrance) : mAdapter;
|
|
}
|
|
|
|
@Override
|
|
public Single<List<AnswerEntity>> provideDataSingle(int page) {
|
|
if (mType == Type.COLLECTION) {
|
|
return Single.fromObservable(RetrofitManager.getInstance(getContext()).getApi().getCollectionAnswer(UserManager.getInstance().getUserId(), page));
|
|
} else {
|
|
return HistoryDatabase.Companion.getInstance().answerDao().getAnswersWithOffset(20, (page - 1) * 20);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onListClick(View view, int position, Object data) {
|
|
AnswerEntity entity;
|
|
switch (view.getId()) {
|
|
case R.id.footerview_item:
|
|
if (mAdapter.isNetworkError()) {
|
|
mListViewModel.load(LoadType.RETRY);
|
|
}
|
|
break;
|
|
case R.id.ask_answer_item_constraintlayout:
|
|
case R.id.ask_answer_item_content:
|
|
entity = (AnswerEntity) data;
|
|
if (entity.getActive()) {
|
|
startActivity(AnswerDetailActivity.getIntent(getContext(), entity.getId(), mEntrance, "我的收藏-回答"));
|
|
} else {
|
|
showDeleteDialog(entity.getId());
|
|
}
|
|
break;
|
|
case R.id.ask_answer_item_title:
|
|
entity = (AnswerEntity) data;
|
|
Questions questions = entity.getQuestions();
|
|
startActivity(QuestionsDetailActivity.getIntent(getContext(), questions.getId(), mEntrance, "我的收藏-回答"));
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
private void showDeleteDialog(String answerId) {
|
|
DialogUtils.showCancelAlertDialog(getContext(), "提示"
|
|
, "内容已被删除,是否取消收藏?"
|
|
, "取消收藏", "暂不"
|
|
, () -> CollectionUtils.INSTANCE.deleteCollection(getContext(), answerId
|
|
, CollectionUtils.CollectionType.answer, 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);
|
|
}
|
|
}), null);
|
|
}
|
|
|
|
// 收藏事件
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
public void onEventMainThread(EBCollectionChanged changed) {
|
|
if (changed.getCollectionType().equals(CollectionUtils.CollectionType.answer)) {
|
|
mListViewModel.load(LoadType.REFRESH);
|
|
}
|
|
}
|
|
|
|
public enum Type {
|
|
COLLECTION,
|
|
|
|
HISTORY
|
|
}
|
|
}
|