admin
2022-01-07 8dfe5354073b700af45d5cb472dd5f003e6f3f25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
149
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
package com.ysvideo.zhibo.app.ui;
 
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
 
import com.google.gson.reflect.TypeToken;
import com.ysvideo.zhibo.app.R;
import com.ysvideo.zhibo.app.db.MySQLiteOpenHelper;
import com.ysvideo.zhibo.app.db.WatchHistoryTable;
import com.ysvideo.zhibo.app.entity.video.VideoInfo;
import com.ysvideo.zhibo.app.entity.video.WatchHistory;
import com.ysvideo.zhibo.app.ui.video.adapter.DownloadAdapter2;
import com.ysvideo.zhibo.app.ui.video.adapter.WatchHistoryAdapter;
import com.ysvideo.zhibo.app.util.JsonUtil;
 
import java.util.ArrayList;
import java.util.List;
 
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import me.jingbin.library.ByRecyclerView;
 
public class WatchHistoryFragment extends Fragment {
 
    private WatchHistoryAdapter mAdapter;
 
    private DownloadAdapter2.IGetDeleteCallback getdeleteNumber = null;
 
    private ByRecyclerView mRecyclerView;
 
    private View empty;
 
    private List<WatchHistory> historyList = new ArrayList<>();
 
    int page = 1;
 
    public void setDeleteCallback(
            DownloadAdapter2.IGetDeleteCallback getdeleteNumber) {
        this.getdeleteNumber = getdeleteNumber;
        if (mAdapter != null)
            mAdapter.setDeleteCallback(getdeleteNumber);
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_watch_history, container,
                false);
    }
 
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
 
        empty = view.findViewById(R.id.fl_empty);
        empty.setVisibility(View.GONE);
 
        mRecyclerView = view.findViewById(R.id.recyclerView);
        mAdapter = new WatchHistoryAdapter(historyList, getContext());
        if (getdeleteNumber != null)
            mAdapter.setDeleteCallback(getdeleteNumber);
 
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
 
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                List<WatchHistory> list = getWatchHistory(page, 20);
                mRecyclerView.loadMoreComplete();
                historyList.addAll(list);
                mAdapter.notifyDataSetChanged();
            }
        });
 
        mRecyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                requestData();
            }
        });
        mRecyclerView.setRefreshing(true);
    }
 
    private void requestData() {
        List<WatchHistory> list = getWatchHistory(page, 20);
        mRecyclerView.setRefreshing(false);
        if (page == 1) {
            historyList.clear();
        }
        historyList.addAll(list);
        if (historyList.size() == 0) {
            empty.setVisibility(View.VISIBLE);
        } else {
            empty.setVisibility(View.GONE);
        }
        mAdapter.notifyDataSetChanged();
    }
 
 
    @Override
    public void onResume() {
        super.onResume();
        mAdapter.setSelectedMode(false);
    }
 
    @Override
    public void onPause() {
        super.onPause();
    }
 
 
    public void deleteWatchHistory() {
        List<Long> selectedIds = mAdapter.getSelectedIds();
        if (!selectedIds.isEmpty()) {
            for (int i = 0; i < selectedIds.size(); i++) {
                getContext().getContentResolver().delete(
                        WatchHistoryTable.CONTENT_URI,
                        WatchHistoryTable._ID + " = ? ",
                        new String[]{String.valueOf(selectedIds.get(i))});
            }
            for (int i = 0; i < historyList.size(); i++) {
                if (selectedIds.contains(historyList.get(i).getId())) {
                    historyList.remove(i--);
                }
            }
 
            mAdapter.getSelectedIds().clear();
 
 
            mAdapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getContext(), "请选择要删除的记录", Toast.LENGTH_LONG)
                    .show();
        }
    }
 
    public boolean isSelectedMode() {
        return mAdapter.isSelectedMode();
    }
 
    public void setSelectedMode(boolean selectedMode) {
        mAdapter.setSelectedMode(selectedMode);
    }
 
    public void selectAll(boolean isSelected) {
        mAdapter.selectAll(isSelected);
    }
 
    public int getCount() {
        return mAdapter.getItemCount();
    }
 
 
    private List<WatchHistory> getWatchHistory(int page, int pageSize) {
        List<WatchHistory> videoInfoList = new ArrayList<>();
 
        SQLiteDatabase database = new MySQLiteOpenHelper(getContext()).getReadableDatabase();
        Cursor cursor = database.query(WatchHistoryTable.TABLE_NAME, new String[]{WatchHistoryTable._ID, WatchHistoryTable.VIDEO_ID, WatchHistoryTable.VIDEO_DETAIL_ID, WatchHistoryTable.VIDEO_DETAIL}, null, null, null, null, WatchHistoryTable.UPDATE_TIME + " DESC", (page - 1) * pageSize + "," + pageSize);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            do {
                WatchHistory watchHistory = new WatchHistory();
 
                Long id = cursor.getLong(cursor
                        .getColumnIndex(WatchHistoryTable._ID));
                String videoId = cursor.getString(cursor
                        .getColumnIndex(WatchHistoryTable.VIDEO_ID));
                String videoDetailId = cursor.getString(cursor
                        .getColumnIndex(WatchHistoryTable.VIDEO_DETAIL_ID));
 
                if (cursor
                        .getColumnIndex(WatchHistoryTable.UPDATE_TIME) > -1) {
                    String watchTime = cursor.getLong(cursor
                            .getColumnIndex(WatchHistoryTable.UPDATE_TIME)) + "";
                    watchHistory.setWatchTime(watchTime);
                }
                watchHistory.setVideoDetailId(videoDetailId);
                watchHistory.setId(id);
                watchHistory.setVideoId(videoId);
 
 
                String videoInfoStr = cursor.getString(cursor
                        .getColumnIndexOrThrow(WatchHistoryTable.VIDEO_DETAIL));
                VideoInfo videoInfo = JsonUtil.videoGson.fromJson(videoInfoStr, new TypeToken<VideoInfo>() {
                }.getType());
                watchHistory.setVideoInfo(videoInfo);
                videoInfoList.add(watchHistory);
            } while (cursor.moveToNext());
        }
        cursor.close();
        return videoInfoList;
    }
 
 
}