ThankActivity.java 10.6 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11
package com.yidianling.dynamic.thank;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


konghaorui committed
12
import com.ydl.ydlcommon.bean.StatusBarOptions;
konghaorui committed
13 14
import com.yidianling.common.tools.LogUtil;
import com.yidianling.common.tools.ToastUtil;
15
import com.yidianling.dynamic.bean.DynamicConstants;
konghaorui committed
16 17 18 19 20 21 22 23 24
import com.yidianling.dynamic.R;
import com.yidianling.dynamic.common.net.DynamicApiUtils;
import com.yidianling.dynamic.model.Command;
import com.yidianling.dynamic.thank.data.ThxData;
import com.yidianling.dynamic.thank.data.ThxListDate;
import com.yidianling.dynamic.thank.view.ThankHeadView;
import com.ydl.ydlcommon.base.BaseActivity;
import com.ydl.ydlcommon.data.http.RxUtils;

konghaorui committed
25 26
import org.jetbrains.annotations.NotNull;

konghaorui committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import de.greenrobot.event.EventBus;
import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.loadmore.LoadMoreUIHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.header.MaterialHeader;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 感谢TA
 * Created by softrice on 15/11/25.
 */
public class ThankActivity extends BaseActivity implements PtrHandler, LoadMoreHandler {


万齐军 committed
46 47 48
    private PtrFrameLayout store_house_ptr_frame;
    private LoadMoreListViewContainer load_more_list_view_container;
    private ListView lv_content;
konghaorui committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    ThxZanListAdapter adapter;

    int page = 1;
    boolean hasMore = true;
    ThankHeadView headView;
    WorryDetailAnswer worryDetailAnswer;

    int answerId;

    int listNum;

    ThxData thxData;

    WorryDetailAnswer worryDetailAnswerForEventBus;

konghaorui committed
64 65 66 67 68
    @NotNull
    @Override
    public StatusBarOptions getStatusViewOptions() {
        return new StatusBarOptions(true, true);
    }
konghaorui committed
69 70 71 72 73 74 75

    void init() {
        // header
        final MaterialHeader header = new MaterialHeader(this);
        int[] colors = getResources().getIntArray(R.array.ydl_colors);
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
konghaorui committed
76
        int padding = (int) getResources().getDimension(R.dimen.dynamic_default_dis_size);
konghaorui committed
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
        header.setPadding(0, padding, 0, padding);
        header.setPtrFrameLayout(store_house_ptr_frame);

        store_house_ptr_frame.setHeaderView(header);
        store_house_ptr_frame.setPtrHandler(this);
        store_house_ptr_frame.addPtrUIHandler(header);

        // 创建
        ISLoadMoreFooterView footerView = new ISLoadMoreFooterView(this);
        footerView.setVisibility(View.GONE);

        load_more_list_view_container.setLoadMoreView(footerView);
        load_more_list_view_container.setLoadMoreUIHandler(footerView);
        load_more_list_view_container.setLoadMoreHandler(this);


        adapter = new ThxZanListAdapter(this);

        headView = new ThankHeadView(this);
        if (worryDetailAnswer != null) {
            headView.setWorryDetailAnswer(worryDetailAnswer);
        }
        headView.setAnswerId(answerId);
        headView.setlistNum(listNum);
        getHeadDate();
        lv_content.addHeaderView(headView);

        lv_content.setAdapter(adapter);
        getData(false);
    }


//    @Override
//    protected void onResume() {
//        super.onResume();
//        getData(false);
//    }

    private void getHeadDate() {
        Command.GetThxHeadDate cmd = new Command.GetThxHeadDate(answerId);
        DynamicApiUtils.getThxHeadData(cmd)
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp -> {
                    try {
                        thxData = resp;
                        headView.setData(thxData);
                        if (thxData.countPage <= 1) {
                            hasMore = false;
                        } else {
                            hasMore = true;
                            page = 1;
                        }

                        if (thxData.zanList == null || thxData.zanList.size() == 0) {
                            setListViewNull();
                        } else {
                            adapter.setDataList(thxData.zanList);
                        }

                    } catch (Exception e) {
                        LogUtil.d(e.getMessage());
                    }
                }, throwable -> DynamicApiUtils.handleError(ThankActivity.this, throwable));
    }

    private void setListViewNull() {
        load_more_list_view_container.loadMoreFinish(true, false);
    }


严久程 committed
149
   public void getData(final boolean loadMore) {
konghaorui committed
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
        if (loadMore && !hasMore) {
            load_more_list_view_container.loadMoreFinish(false, false);
            return;
        }
        if (loadMore) {
            page++;
        } else {
            page = 1;
            hasMore = true;
        }
        Command.GetZanList cmd = new Command.GetZanList(page, answerId);
        DynamicApiUtils.getZanList(cmd)
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp -> {
                    load_more_list_view_container.loadMoreFinish(false, true);
                    try {
                        ThxListDate thxListDate = resp;
                        if (thxListDate.array_name == null ||
                                thxListDate.array_name.zanList == null ||
                                thxListDate.array_name.zanList.size() < DynamicConstants.THX_REPLY_PAGE_SIZE) {
                            hasMore = false;
                            load_more_list_view_container.loadMoreFinish(false, false);
                        }
                        if (!loadMore) {
                            adapter.setDataList(thxListDate.array_name.zanList);
                        } else if (thxListDate.array_name.zanList != null) {
                            adapter.addDataList(thxListDate.array_name.zanList);
                        }
                        if (page == 1 && (thxListDate.array_name == null ||
                                thxListDate.array_name.zanList == null ||
                                thxListDate.array_name.zanList.size() == 0)) {
                            setListViewNull();
                        }
                    } catch (Exception e) {
                        LogUtil.d(e.getMessage());
                    }
                }, throwable -> DynamicApiUtils.handleError(ThankActivity.this, throwable));
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, lv_content, header);
    }

    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        frame.postDelayed(new Runnable() {
            @Override
            public void run() {
                store_house_ptr_frame.refreshComplete();
            }
        }, 1800);
        getData(false);
    }


    @Override
    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
        getData(true);
    }

    @Override
    protected int layoutResId() {
konghaorui committed
215
        return R.layout.dynamic_activity_thank;
konghaorui committed
216 217 218 219
    }

    @Override
    protected void initDataAndEvent() {
万齐军 committed
220
        bindView();
konghaorui committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234

        listNum = getIntent().getIntExtra("listNum", 0);
        answerId = getIntent().getIntExtra("answerId", 0);

        try {
            worryDetailAnswer = (WorryDetailAnswer) getIntent().getSerializableExtra("worryDetailAnswer");
        } catch (Exception e) {
            e.printStackTrace();
        }

        init();
        EventBus.getDefault().register(this);
    }

万齐军 committed
235 236 237 238 239
    private void bindView() {
        store_house_ptr_frame = findViewById(R.id.store_house_ptr_frame);
        load_more_list_view_container = findViewById(R.id.load_more_list_view_container);
        lv_content = findViewById(R.id.lv_content);
    }
konghaorui committed
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

    class ISLoadMoreFooterView extends RelativeLayout
            implements LoadMoreUIHandler {

        private TextView mTextView;

        public ISLoadMoreFooterView(Context context) {
            super(context);
            setupViews();
        }

        private void setupViews() {
            LayoutInflater.from(getContext()).inflate(in.srain.cube.R.layout.cube_views_load_more_default_footer, this);
            mTextView = (TextView) findViewById(in.srain.cube.R.id.cube_views_load_more_default_footer_text_view);
            mTextView.setText("");
        }

        @Override
        public void onLoading(LoadMoreContainer container) {
            setVisibility(VISIBLE);
            mTextView.setText("正在加载...");
            mTextView.setTextColor(0xFF545454);
        }

        @Override
        public void onLoadFinish(LoadMoreContainer container, boolean empty, boolean hasMore) {
            if (!hasMore) {
                setVisibility(VISIBLE);
                if (empty) {
                    mTextView.setText("小小的鼓励,满满的收获~");
                    mTextView.setTextColor(0xFFC0C0C0);
                } else {
                    mTextView.setText("感谢大家的鼓励:)");
                    mTextView.setTextColor(0xFFC0C0C0);
                }
            } else {
                setVisibility(VISIBLE);
                mTextView.setText("感谢大家的鼓励:)");
                mTextView.setTextColor(0xFFC0C0C0);
            }
        }

        @Override
        public void onWaitToLoadMore(LoadMoreContainer container) {
            mTextView.setText("");
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == RESULT_OK) {
konghaorui committed
293
            ToastUtil.toastImg(this, R.drawable.dynamic_dialog_send_thx_success);
konghaorui committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
            if (worryDetailAnswerForEventBus != null) {
                worryDetailAnswerForEventBus.pIsZan = true;
                worryDetailAnswerForEventBus.pZan++;
            }
            finish();
        }
    }


    public void onEvent(WorryDetailAnswer worryDetailAnswer) {
        worryDetailAnswerForEventBus = worryDetailAnswer;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}