@@ -45,6 +45,150 @@ public void onBindViewHolder(ProjectViewHolder holder, int position) {
4545 });
4646 }
4747
48+ private void handleMenuItem (MenuItem item , int position ) {
49+ int id = item .getItemId ();
50+ if (id == R .id .remove ) {
51+ Project project = get (position );
52+ if (project != null ) {
53+ Project .removeProject (project );
54+ remove (position );
55+ }
56+ } else if (id == R .id .rename ) {
57+ RenameProjectDialog .show (context , this , get (position ));
58+ }
59+ }
60+
61+ @ Override
62+ public int getItemCount () {
63+ return items .size ();
64+ }
65+
66+ public int add (Project project ) {
67+ if (project == null ) return -1 ;
68+
69+ items .add (project );
70+ int position = items .size () - 1 ;
71+ notifyItemInserted (position );
72+ notifyChange ();
73+ return position ;
74+ }
75+
76+ public void update (Project project ) {
77+ int position = indexOf (project );
78+ if (position >= 0 ) {
79+ items .set (position , project );
80+ notifyItemChanged (position );
81+ notifyChange ();
82+ }
83+ }
84+
85+ public void add (int position , Project project ) {
86+ if (project == null || position < 0 || position > items .size ()) return ;
87+
88+ items .add (position , project );
89+ notifyItemInserted (position );
90+ notifyChange ();
91+ }
92+
93+ public Project get (int position ) {
94+ if (position < 0 || position >= items .size ()) return null ;
95+ return items .get (position );
96+ }
97+
98+ public int indexOf (Project project ) {
99+ return project != null ? items .indexOf (project ) : -1 ;
100+ }
101+
102+ public void remove (int position ) {
103+ if (position < 0 || position >= items .size ()) return ;
104+
105+ items .remove (position );
106+ notifyItemRemoved (position );
107+ notifyChange ();
108+ }
109+
110+ public void clear () {
111+ items .clear ();
112+ notifyDataSetChanged ();
113+ notifyChange ();
114+ }
115+
116+ public void addAll (Collection <Project > items ) {
117+ if (items == null ) return ;
118+
119+ int oldSize = this .items .size ();
120+ this .items .addAll (items );
121+ notifyItemRangeInserted (oldSize , items .size ());
122+ notifyChange ();
123+ }
124+
125+ public void setItems (Collection <Project > items ) {
126+ if (items == null ) return ;
127+
128+ this .items .clear ();
129+ this .items .addAll (items );
130+ notifyDataSetChanged ();
131+ notifyChange ();
132+ }
133+
134+ private void notifyChange () {
135+ if (onChangeListener != null ) {
136+ onChangeListener .onChange (items .size () > 0 );
137+ }
138+ }
139+
140+ public class ProjectViewHolder extends RecyclerView .ViewHolder {
141+ public final TextView name ;
142+ public final ImageButton more ;
143+
144+ public ProjectViewHolder (View view ) {
145+ super (view );
146+ name = view .findViewById (R .id .name );
147+ more = view .findViewById (R .id .more );
148+ }
149+ }
150+
151+ public interface OnChangeListener {
152+ void onChange (boolean hasItems );
153+ }
154+
155+ public interface OnProjectClickListener {
156+ void onClick (View view , Project project );
157+ }
158+
159+ public OnChangeListener getOnChangeListener () {
160+ return this .onChangeListener ;
161+ }
162+
163+ public void setOnChangeListener (OnChangeListener onChangeListener ) {
164+ this .onChangeListener = onChangeListener ;
165+ notifyChange ();
166+ }
167+
168+ @ Override
169+ public long getItemId (int position ) {
170+ return get (position ).hashCode ();
171+ }
172+
173+ public OnProjectClickListener getOnProjectClickListener () {
174+ return this .onProjectClickListener ;
175+ }
176+
177+ public void setOnProjectClickListener (OnProjectClickListener onProjectClickListener ) {
178+ this .onProjectClickListener = onProjectClickListener ;
179+ }
180+ }
181+ public void onBindViewHolder (ProjectViewHolder holder , int position ) {
182+ Project item = get (position );
183+ holder .name .setText (item .getName () != null ? item .getName () : "" );
184+ holder .more .setOnClickListener (
185+ v -> OverflowMenu .show (v , R .menu .project , i -> handleMenuItem (i , position )));
186+ holder .itemView .setOnClickListener (
187+ (view ) -> {
188+ if (onProjectClickListener != null ) onProjectClickListener .onClick (view , item );
189+ });
190+ }
191+
48192 private void handleMenuItem (MenuItem item , int position ) {
49193 int id = item .getItemId ();
50194 if (id == R .id .remove ) {
0 commit comments