-
-
Notifications
You must be signed in to change notification settings - Fork 235
feat: split complex loyaltyCardViewActivity #3072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
2fcffed
e2e9c8b
72b0ede
1b044a7
470e677
f62ecdf
3b9c69b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import android.view.View | |
| import android.widget.RemoteViews | ||
| import androidx.core.widget.RemoteViewsCompat | ||
| import protect.card_locker.DBHelper.LoyaltyCardArchiveFilter | ||
| import protect.card_locker.cardview.LoyaltyCardViewActivity | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import seems unnecessary, right?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is used for example in line |
||
|
|
||
| class ListWidget : AppWidgetProvider() { | ||
| fun updateAll(context: Context) { | ||
|
|
@@ -129,4 +130,4 @@ class ListWidget : AppWidgetProvider() { | |
|
|
||
| return remoteViews | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,9 @@ | |
| import java.util.Collections; | ||
| import java.util.LinkedList; | ||
|
|
||
| class ShortcutHelper { | ||
| import protect.card_locker.cardview.LoyaltyCardViewActivity; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import seems unnecessary, right?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently is used for example in I removed |
||
|
|
||
| public class ShortcutHelper { | ||
| /** | ||
| * This variable controls the maximum number of shortcuts available. | ||
| * It is made public only to make testing easier and should not be | ||
|
|
@@ -43,33 +45,36 @@ class ShortcutHelper { | |
| * based on the lastUsed field. Archived cards are excluded from the shortcuts | ||
| * list. The list keeps at most maxShortcuts number of elements. | ||
| */ | ||
| static void updateShortcuts(Context context) { | ||
| public static void updateShortcuts(Context context) { | ||
| if (maxShortcuts == -1) { | ||
| maxShortcuts = ShortcutManagerCompat.getMaxShortcutCountPerActivity(context); | ||
| } | ||
| LinkedList<ShortcutInfoCompat> finalList = new LinkedList<>(); | ||
| SQLiteDatabase database = new DBHelper(context).getReadableDatabase(); | ||
| Cursor loyaltyCardCursor = DBHelper.getLoyaltyCardCursor( | ||
| DBHelper dbHelper = new DBHelper(context); | ||
| SQLiteDatabase database = dbHelper.getReadableDatabase(); | ||
| try (Cursor loyaltyCardCursor = DBHelper.getLoyaltyCardCursor( | ||
| database, | ||
| "", | ||
| null, | ||
| DBHelper.LoyaltyCardOrder.LastUsed, | ||
| DBHelper.LoyaltyCardOrderDirection.Ascending, | ||
| DBHelper.LoyaltyCardArchiveFilter.Unarchived | ||
| ); | ||
|
|
||
| int rank = 0; | ||
|
|
||
| while (rank < maxShortcuts && loyaltyCardCursor.moveToNext()) { | ||
| int id = loyaltyCardCursor.getInt(loyaltyCardCursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID)); | ||
| LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(context, database, id); | ||
|
|
||
| ShortcutInfoCompat updatedShortcut = createShortcutBuilder(context, loyaltyCard) | ||
| .setRank(rank) | ||
| .build(); | ||
|
|
||
| finalList.addLast(updatedShortcut); | ||
| rank++; | ||
| )) { | ||
| int rank = 0; | ||
|
|
||
| while (rank < maxShortcuts && loyaltyCardCursor.moveToNext()) { | ||
| int id = loyaltyCardCursor.getInt(loyaltyCardCursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbIds.ID)); | ||
| LoyaltyCard loyaltyCard = DBHelper.getLoyaltyCard(context, database, id); | ||
|
|
||
| ShortcutInfoCompat updatedShortcut = createShortcutBuilder(context, loyaltyCard) | ||
| .setRank(rank) | ||
| .build(); | ||
|
|
||
| finalList.addLast(updatedShortcut); | ||
| rank++; | ||
| } | ||
| } finally { | ||
| dbHelper.close(); | ||
| } | ||
|
|
||
| ShortcutManagerCompat.setDynamicShortcuts(context, finalList); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package protect.card_locker.cardview; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| final class LoyaltyCardImageNavigator { | ||
| private final List<LoyaltyCardImageType> imageTypes; | ||
| private int currentIndex; | ||
|
|
||
| LoyaltyCardImageNavigator(List<LoyaltyCardImageType> imageTypes, int currentIndex) { | ||
| this.imageTypes = new ArrayList<>(imageTypes); | ||
| this.currentIndex = clampIndex(currentIndex); | ||
| } | ||
|
|
||
| LoyaltyCardImageType getCurrent() { | ||
| if (isEmpty()) { | ||
| return LoyaltyCardImageType.NONE; | ||
| } | ||
| return imageTypes.get(currentIndex); | ||
| } | ||
|
|
||
| boolean isEmpty() { | ||
| return imageTypes.isEmpty(); | ||
| } | ||
|
|
||
| int size() { | ||
| return imageTypes.size(); | ||
| } | ||
|
|
||
| boolean remove(LoyaltyCardImageType type) { | ||
| int removedIndex = imageTypes.indexOf(type); | ||
| if (removedIndex == -1) { | ||
| return false; | ||
| } | ||
|
|
||
| imageTypes.remove(removedIndex); | ||
| currentIndex = clampIndex(currentIndex); | ||
| return true; | ||
| } | ||
|
|
||
| boolean canGoPrevious() { | ||
| return currentIndex > 0; | ||
| } | ||
|
|
||
| boolean canGoNext() { | ||
| return currentIndex < imageTypes.size() - 1; | ||
| } | ||
|
|
||
| boolean movePrevious() { | ||
| if (!canGoPrevious()) { | ||
| return false; | ||
| } | ||
| currentIndex--; | ||
| return true; | ||
| } | ||
|
|
||
| boolean moveNext(boolean overflow) { | ||
| if (isEmpty()) { | ||
| return false; | ||
| } | ||
| if (canGoNext()) { | ||
| currentIndex++; | ||
| return true; | ||
| } | ||
| if (overflow) { | ||
| currentIndex = 0; | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| int getCurrentIndex() { | ||
| return currentIndex; | ||
| } | ||
|
|
||
| LoyaltyCardImageType peekNext(boolean overflow) { | ||
| if (isEmpty()) { | ||
| return LoyaltyCardImageType.NONE; | ||
| } | ||
|
|
||
| if (canGoNext()) { | ||
| return imageTypes.get(currentIndex + 1); | ||
| } | ||
|
|
||
| return overflow ? imageTypes.get(0) : getCurrent(); | ||
| } | ||
|
|
||
| private int clampIndex(int index) { | ||
| if (imageTypes.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| return Math.max(0, Math.min(index, imageTypes.size() - 1)); | ||
| } | ||
| } | ||
|
|
||
| enum LoyaltyCardImageType { | ||
| NONE, | ||
| ICON, | ||
| BARCODE, | ||
| IMAGE_FRONT, | ||
| IMAGE_BACK | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import seems unnecessary, right?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is used for example in line