Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BookmarkListAdapter.java « bookmarks « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 55340b3742ed344bd13358ba6007147a789f35d2 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package com.mapswithme.maps.bookmarks;

import android.content.res.Resources;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
import com.mapswithme.maps.bookmarks.data.BookmarkInfo;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.SortedBlock;
import com.mapswithme.maps.content.DataSource;
import com.mapswithme.maps.widget.recycler.RecyclerClickListener;
import com.mapswithme.maps.widget.recycler.RecyclerLongClickListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BookmarkListAdapter extends RecyclerView.Adapter<Holders.BaseBookmarkHolder>
{
  // view types
  static final int TYPE_TRACK = 0;
  static final int TYPE_BOOKMARK = 1;
  static final int TYPE_SECTION = 2;
  static final int TYPE_DESC = 3;

  @NonNull
  private final DataSource<BookmarkCategory> mDataSource;
  @Nullable
  private List<Long> mSearchResults;
  @Nullable
  private List<SortedBlock> mSortedResults;

  @SuppressWarnings("NullableProblems")
  @NonNull
  private SectionsDataSource mSectionsDataSource;

  @Nullable
  private RecyclerClickListener mMoreListener;
  @Nullable
  private RecyclerClickListener mClickListener;
  @Nullable
  private RecyclerLongClickListener mLongClickListener;

  static class SectionPosition
  {
    static final int INVALID_POSITION = -1;

    private final int mSectionIndex;
    private final int mItemIndex;

    SectionPosition(int sectionInd, int itemInd)
    {
      mSectionIndex = sectionInd;
      mItemIndex = itemInd;
    }

    int getSectionIndex()
    {
      return mSectionIndex;
    }

    int getItemIndex()
    {
      return mItemIndex;
    }

    boolean isTitlePosition()
    {
      return mSectionIndex != INVALID_POSITION && mItemIndex == INVALID_POSITION;
    }

    boolean isItemPosition()
    {
      return mSectionIndex != INVALID_POSITION && mItemIndex != INVALID_POSITION;
    }
  }

  public static abstract class SectionsDataSource
  {
    @NonNull
    private final DataSource<BookmarkCategory> mDataSource;

    SectionsDataSource(@NonNull DataSource<BookmarkCategory> dataSource)
    {
      mDataSource = dataSource;
    }

    public BookmarkCategory getCategory() { return mDataSource.getData(); }

    boolean hasDescription()
    {
      return !mDataSource.getData().getAnnotation().isEmpty() ||
             !mDataSource.getData().getDescription().isEmpty();
    }

    public abstract int getSectionsCount();
    public abstract boolean isEditable(int sectionIndex);
    public abstract boolean hasTitle(int sectionIndex);
    @Nullable
    public abstract String getTitle(int sectionIndex, @NonNull Resources rs);
    public abstract int getItemsCount(int sectionIndex);
    public abstract int getItemsType(int sectionIndex);
    public abstract long getBookmarkId(@NonNull SectionPosition pos);
    public abstract long getTrackId(@NonNull SectionPosition pos);
    public abstract void onDelete(@NonNull SectionPosition pos);
  }

  private static class CategorySectionsDataSource extends SectionsDataSource
  {
    private int mSectionsCount;
    private int mDescriptionSectionIndex;
    private int mBookmarksSectionIndex;
    private int mTracksSectionIndex;

    CategorySectionsDataSource(@NonNull DataSource<BookmarkCategory> dataSource)
    {
      super(dataSource);
      calculateSections();
    }

    private void calculateSections()
    {
      mDescriptionSectionIndex = SectionPosition.INVALID_POSITION;
      mBookmarksSectionIndex = SectionPosition.INVALID_POSITION;
      mTracksSectionIndex = SectionPosition.INVALID_POSITION;

      mSectionsCount = 0;
      if (hasDescription())
        mDescriptionSectionIndex = mSectionsCount++;
      if (getCategory().getTracksCount() > 0)
        mTracksSectionIndex = mSectionsCount++;
      if (getCategory().getBookmarksCount() > 0)
        mBookmarksSectionIndex = mSectionsCount++;
    }

    @Override
    public int getSectionsCount() { return mSectionsCount; }

    @Override
    public boolean isEditable(int sectionIndex)
    {
      return sectionIndex != mDescriptionSectionIndex && !getCategory().isFromCatalog();
    }

    @Override
    public boolean hasTitle(int sectionIndex) { return true; }

    @Nullable
    public String getTitle(int sectionIndex, @NonNull Resources rs)
    {
      if (sectionIndex == mDescriptionSectionIndex)
        return rs.getString(R.string.description);
      if (sectionIndex == mTracksSectionIndex)
        return rs.getString(R.string.tracks_title);
      return rs.getString(R.string.bookmarks);
    }

    @Override
    public int getItemsCount(int sectionIndex)
    {
      if (sectionIndex == mDescriptionSectionIndex)
        return 1;
      if (sectionIndex == mTracksSectionIndex)
        return getCategory().getTracksCount();
      if (sectionIndex == mBookmarksSectionIndex)
        return getCategory().getBookmarksCount();
      return 0;
    }

    @Override
    public int getItemsType(int sectionIndex)
    {
      if (sectionIndex == mDescriptionSectionIndex)
        return TYPE_DESC;
      if (sectionIndex == mTracksSectionIndex)
        return TYPE_TRACK;
      if (sectionIndex == mBookmarksSectionIndex)
        return TYPE_BOOKMARK;
      throw new AssertionError("Invalid section index: " + sectionIndex);
    }

    @Override
    public void onDelete(@NonNull SectionPosition pos)
    {
      calculateSections();
    }

    @Override
    public long getBookmarkId(@NonNull SectionPosition pos)
    {
      return BookmarkManager.INSTANCE.getBookmarkIdByPosition(getCategory().getId(),
                                                              pos.getItemIndex());
    }

    @Override
    public long getTrackId(@NonNull SectionPosition pos)
    {
      return BookmarkManager.INSTANCE.getTrackIdByPosition(getCategory().getId(),
                                                           pos.getItemIndex());
    }
  }

  private static class SearchResultsSectionsDataSource extends SectionsDataSource
  {
    @NonNull
    private final List<Long> mSearchResults;

    SearchResultsSectionsDataSource(@NonNull DataSource<BookmarkCategory> dataSource,
                                    @NonNull List<Long> searchResults)
    {
      super(dataSource);
      mSearchResults = searchResults;
    }

    @Override
    public int getSectionsCount() { return 1; }

    @Override
    public boolean isEditable(int sectionIndex) { return true; }

    @Override
    public boolean hasTitle(int sectionIndex) { return false; }

    @Nullable
    public String getTitle(int sectionIndex, @NonNull Resources rs) { return null; }

    @Override
    public int getItemsCount(int sectionIndex) { return mSearchResults.size(); }

    @Override
    public int getItemsType(int sectionIndex) { return TYPE_BOOKMARK; }

    @Override
    public void onDelete(@NonNull SectionPosition pos)
    {
      mSearchResults.remove(pos.getItemIndex());
    }

    @Override
    public long getBookmarkId(@NonNull SectionPosition pos)
    {
      return mSearchResults.get(pos.getItemIndex());
    }

    @Override
    public long getTrackId(@NonNull SectionPosition pos)
    {
      throw new AssertionError("Tracks unsupported in search results.");
    }
  }

  private static class SortedSectionsDataSource extends SectionsDataSource
  {
    @NonNull
    private List<SortedBlock> mSortedBlocks;

    SortedSectionsDataSource(@NonNull DataSource<BookmarkCategory> dataSource,
                             @NonNull List<SortedBlock> sortedBlocks)
    {
      super(dataSource);
      mSortedBlocks = sortedBlocks;
    }

    private boolean isDescriptionSection(int sectionIndex)
    {
      return hasDescription() && sectionIndex == 0;
    }

    @NonNull
    private SortedBlock getSortedBlock(int sectionIndex)
    {
      if (isDescriptionSection(sectionIndex))
        throw new IllegalArgumentException("Invalid section index for sorted block.");
      int blockIndex = sectionIndex - (hasDescription() ? 1 : 0);
      return mSortedBlocks.get(blockIndex);
    }

    @Override
    public int getSectionsCount()
    {
      return mSortedBlocks.size() + (hasDescription() ? 1 : 0);
    }

    @Override
    public boolean isEditable(int sectionIndex)
    {
      return !isDescriptionSection(sectionIndex);
    }

    @Override
    public boolean hasTitle(int sectionIndex) { return true; }

    @Nullable
    public String getTitle(int sectionIndex, @NonNull Resources rs)
    {
      if (isDescriptionSection(sectionIndex))
        return rs.getString(R.string.description);
      return getSortedBlock(sectionIndex).getName();
    }

    @Override
    public int getItemsCount(int sectionIndex)
    {
      if (isDescriptionSection(sectionIndex))
        return 1;
      SortedBlock block = getSortedBlock(sectionIndex);
      if (block.isBookmarksBlock())
        return block.getBookmarkIds().size();
      return block.getTrackIds().size();
    }

    @Override
    public int getItemsType(int sectionIndex)
    {
      if (isDescriptionSection(sectionIndex))
        return TYPE_DESC;
      if (getSortedBlock(sectionIndex).isBookmarksBlock())
        return TYPE_BOOKMARK;
      return TYPE_TRACK;
    }

    @Override
    public void onDelete(@NonNull SectionPosition pos)
    {
      if (isDescriptionSection(pos.getSectionIndex()))
        throw new IllegalArgumentException("Delete failed. Invalid section index.");

      int blockIndex = pos.getSectionIndex() - (hasDescription() ? 1 : 0);
      SortedBlock block = mSortedBlocks.get(blockIndex);
      if (block.isBookmarksBlock())
      {
        block.getBookmarkIds().remove(pos.getItemIndex());
        if (block.getBookmarkIds().isEmpty())
          mSortedBlocks.remove(blockIndex);
        return;
      }

      block.getTrackIds().remove(pos.getItemIndex());
      if (block.getTrackIds().isEmpty())
        mSortedBlocks.remove(blockIndex);
    }

    public long getBookmarkId(@NonNull SectionPosition pos)
    {
      return getSortedBlock(pos.getSectionIndex()).getBookmarkIds().get(pos.getItemIndex());
    }

    public long getTrackId(@NonNull SectionPosition pos)
    {
      return getSortedBlock(pos.getSectionIndex()).getTrackIds().get(pos.getItemIndex());
    }
  }

  BookmarkListAdapter(@NonNull DataSource<BookmarkCategory> dataSource)
  {
    mDataSource = dataSource;
    refreshSections();
  }

  private void refreshSections()
  {
    if (mSearchResults != null)
      mSectionsDataSource = new SearchResultsSectionsDataSource(mDataSource, mSearchResults);
    else if (mSortedResults != null)
      mSectionsDataSource = new SortedSectionsDataSource(mDataSource, mSortedResults);
    else
      mSectionsDataSource = new CategorySectionsDataSource(mDataSource);
  }

  private SectionPosition getSectionPosition(int position)
  {
    int startSectionRow = 0;
    boolean hasTitle;
    int sectionsCount = mSectionsDataSource.getSectionsCount();
    for (int i = 0; i < sectionsCount; ++i)
    {
      hasTitle = mSectionsDataSource.hasTitle(i);
      int sectionRowsCount = mSectionsDataSource.getItemsCount(i) + (hasTitle ? 1 : 0);
      if (startSectionRow == position && hasTitle)
        return new SectionPosition(i, SectionPosition.INVALID_POSITION);
      if (startSectionRow + sectionRowsCount > position)
        return new SectionPosition(i, position - startSectionRow - (hasTitle ? 1 : 0));
      startSectionRow += sectionRowsCount;
    }
    return new SectionPosition(SectionPosition.INVALID_POSITION, SectionPosition.INVALID_POSITION);
  }

  void setSearchResults(@Nullable long[] searchResults)
  {
    if (searchResults != null)
    {
      mSearchResults = new ArrayList<Long>(searchResults.length);
      for (long id : searchResults)
        mSearchResults.add(id);
    }
    else
    {
      mSearchResults = null;
    }
    refreshSections();
  }

  void setSortedResults(@Nullable SortedBlock[] sortedResults)
  {
    if (sortedResults != null)
      mSortedResults = new ArrayList<>(Arrays.asList(sortedResults));
    else
      mSortedResults = null;
    refreshSections();
  }

  public void setOnClickListener(@Nullable RecyclerClickListener listener)
  {
    mClickListener = listener;
  }

  void setOnLongClickListener(@Nullable RecyclerLongClickListener listener)
  {
    mLongClickListener = listener;
  }

  void setMoreListener(@Nullable RecyclerClickListener listener)
  {
    mMoreListener = listener;
  }

  @Override
  @NonNull
  public Holders.BaseBookmarkHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
  {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    Holders.BaseBookmarkHolder holder = null;
    switch (viewType)
    {
      case TYPE_TRACK:
        Holders.TrackViewHolder trackHolder =
            new Holders.TrackViewHolder(inflater.inflate(R.layout.item_track, parent,
                                                         false));
        trackHolder.setOnClickListener(mClickListener);
        holder = trackHolder;
        break;
      case TYPE_BOOKMARK:
        Holders.BookmarkViewHolder bookmarkHolder =
            new Holders.BookmarkViewHolder(inflater.inflate(R.layout.item_bookmark, parent,
                                                            false));
        bookmarkHolder.setOnClickListener(mClickListener);
        bookmarkHolder.setOnLongClickListener(mLongClickListener);
        bookmarkHolder.setMoreListener(mMoreListener);
        holder = bookmarkHolder;
        break;
      case TYPE_SECTION:
        TextView tv = (TextView) inflater.inflate(R.layout.item_category_title, parent, false);
        holder = new Holders.SectionViewHolder(tv);
        break;
      case TYPE_DESC:
        View desc = inflater.inflate(R.layout.item_category_description, parent, false);
        holder = new Holders.DescriptionViewHolder(desc, mSectionsDataSource.getCategory());
        break;
    }

    if (holder == null)
      throw new AssertionError("Unsupported view type: " + viewType);

    return holder;
  }

  @Override
  public void onBindViewHolder(@NonNull Holders.BaseBookmarkHolder holder, int position)
  {
    SectionPosition sp = getSectionPosition(position);
    holder.bind(sp, mSectionsDataSource);
  }

  @Override
  public int getItemViewType(int position)
  {
    SectionPosition sp = getSectionPosition(position);
    if (sp.isTitlePosition())
      return TYPE_SECTION;
    if (sp.isItemPosition())
      return mSectionsDataSource.getItemsType(sp.getSectionIndex());
    throw new IllegalArgumentException("Position not found: " + position);
  }

  @Override
  public long getItemId(int position)
  {
    return position;
  }

  @Override
  public int getItemCount()
  {
    int itemCount = 0;
    int sectionsCount = mSectionsDataSource.getSectionsCount();
    for (int i = 0; i < sectionsCount; ++i)
    {
      int sectionItemsCount = mSectionsDataSource.getItemsCount(i);
      if (sectionItemsCount == 0)
        continue;
      itemCount += sectionItemsCount;
      if (mSectionsDataSource.hasTitle(i))
        ++itemCount;
    }
    return itemCount;
  }

  void onDelete(int position)
  {
    SectionPosition sp = getSectionPosition(position);
    mSectionsDataSource.onDelete(sp);
    // In case of the search results editing reset cached sorted blocks.
    if (isSearchResults())
      mSortedResults = null;
  }

  boolean isSearchResults()
  {
    return mSearchResults != null;
  }

  public Object getItem(int position)
  {
    if (getItemViewType(position) == TYPE_DESC)
      throw new UnsupportedOperationException("Not supported here! Position = " + position);

    SectionPosition pos = getSectionPosition(position);
    if (getItemViewType(position) == TYPE_TRACK)
    {
      final long trackId = mSectionsDataSource.getTrackId(pos);
      return BookmarkManager.INSTANCE.getTrack(trackId);
    }
    else
    {
      final long bookmarkId = mSectionsDataSource.getBookmarkId(pos);
      BookmarkInfo info = BookmarkManager.INSTANCE.getBookmarkInfo(bookmarkId);
      if (info == null)
        throw new RuntimeException("Bookmark no longer exists " + bookmarkId);
      return info;
    }
  }
}