-
Notifications
You must be signed in to change notification settings - Fork 207
/
RepositoriesAdapter.java
216 lines (166 loc) · 5.7 KB
/
RepositoriesAdapter.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.arellomobile.mvp.sample.github.ui.adapters;
import java.util.ArrayList;
import java.util.List;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.arellomobile.mvp.MvpDelegate;
import com.arellomobile.mvp.presenter.InjectPresenter;
import com.arellomobile.mvp.presenter.PresenterType;
import com.arellomobile.mvp.presenter.ProvidePresenter;
import com.arellomobile.mvp.sample.github.R;
import com.arellomobile.mvp.sample.github.mvp.models.Repository;
import com.arellomobile.mvp.sample.github.mvp.presenters.RepositoryLikesPresenter;
import com.arellomobile.mvp.sample.github.mvp.presenters.RepositoryPresenter;
import com.arellomobile.mvp.sample.github.mvp.views.RepositoryLikesView;
import com.arellomobile.mvp.sample.github.mvp.views.RepositoryView;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Date: 22.01.2016
* Time: 17:04
*
* @author Yuri Shmakov
*/
public class RepositoriesAdapter extends MvpBaseAdapter implements RepositoryLikesView {
public static final int REPOSITORY_VIEW_TYPE = 0;
private static final int PROGRESS_VIEW_TYPE = 1;
@InjectPresenter(type = PresenterType.WEAK, tag = RepositoryLikesPresenter.TAG)
RepositoryLikesPresenter mRepositoryLikesPresenter;
private int mSelection = -1;
private List<Repository> mRepositories;
private List<Integer> mLiked;
private List<Integer> mLikesInProgress;
private boolean mMaybeMore;
private OnScrollToBottomListener mScrollToBottomListener;
public RepositoriesAdapter(MvpDelegate<?> parentDelegate, OnScrollToBottomListener scrollToBottomListener) {
super(parentDelegate, String.valueOf(0));
mScrollToBottomListener = scrollToBottomListener;
mRepositories = new ArrayList<>();
mLiked = new ArrayList<>();
mLikesInProgress = new ArrayList<>();
}
public void setRepositories(List<Repository> repositories, boolean maybeMore) {
mRepositories = new ArrayList<>(repositories);
dataSetChanged(maybeMore);
}
public void addRepositories(List<Repository> repositories, boolean maybeMore) {
mRepositories.addAll(repositories);
dataSetChanged(maybeMore);
}
public void updateLikes(List<Integer> inProgress, List<Integer> likedIds) {
mLikesInProgress = new ArrayList<>(inProgress);
mLiked = new ArrayList<>(likedIds);
notifyDataSetChanged();
}
public void setSelection(int selection) {
mSelection = selection;
notifyDataSetChanged();
}
private void dataSetChanged(boolean maybeMore) {
mMaybeMore = maybeMore;
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return position == mRepositories.size() ? PROGRESS_VIEW_TYPE : REPOSITORY_VIEW_TYPE;
}
@Override
public int getViewTypeCount() {
return 2;
}
public int getRepositoriesCount() {
return mRepositories.size();
}
@Override
public int getCount() {
return mRepositories.size() + (mMaybeMore ? 1 : 0);
}
@Override
public Repository getItem(int position) {
return mRepositories.get(position);
}
@Override
public long getItemId(int position) {
return getItem(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == PROGRESS_VIEW_TYPE) {
if (mScrollToBottomListener != null) {
mScrollToBottomListener.onScrollToBottom();
}
return new ProgressBar(parent.getContext());
}
RepositoryHolder holder;
if (convertView != null) {
holder = (RepositoryHolder) convertView.getTag();
} else {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_repository, parent, false);
holder = new RepositoryHolder(convertView);
convertView.setTag(holder);
}
final Repository item = getItem(position);
holder.bind(position, item);
return convertView;
}
public class RepositoryHolder implements RepositoryView {
@InjectPresenter
RepositoryPresenter mRepositoryPresenter;
private Repository mRepository;
@BindView(R.id.item_repository_text_view_name)
TextView nameTextView;
@BindView(R.id.item_repository_image_button_like)
ImageButton likeImageButton;
View view;
private MvpDelegate mMvpDelegate;
@ProvidePresenter
RepositoryPresenter provideRepositoryPresenter() {
return new RepositoryPresenter(mRepository);
}
RepositoryHolder(View view) {
this.view = view;
ButterKnife.bind(this, view);
}
void bind(int position, Repository repository) {
if (getMvpDelegate() != null) {
getMvpDelegate().onSaveInstanceState();
getMvpDelegate().onDetach();
getMvpDelegate().onDestroyView();
mMvpDelegate = null;
}
mRepository = repository;
getMvpDelegate().onCreate();
getMvpDelegate().onAttach();
view.setBackgroundResource(position == mSelection ? R.color.colorAccent : android.R.color.transparent);
likeImageButton.setOnClickListener(v -> mRepositoryLikesPresenter.toggleLike(repository.getId()));
boolean isInProgress = mLikesInProgress.contains(repository.getId());
likeImageButton.setEnabled(!isInProgress);
likeImageButton.setSelected(mLiked.contains(repository.getId()));
}
@Override
public void showRepository(Repository repository) {
nameTextView.setText(repository.getName());
}
@Override
public void updateLike(boolean isInProgress, boolean isLiked) {
// pass
}
MvpDelegate getMvpDelegate() {
if (mRepository == null) {
return null;
}
if (mMvpDelegate == null) {
mMvpDelegate = new MvpDelegate<>(this);
mMvpDelegate.setParentDelegate(RepositoriesAdapter.this.getMvpDelegate(), String.valueOf(mRepository.getId()));
}
return mMvpDelegate;
}
}
public interface OnScrollToBottomListener {
void onScrollToBottom();
}
}