1010import androidx .lifecycle .LiveData ;
1111import androidx .lifecycle .MutableLiveData ;
1212
13+ import org .jetbrains .annotations .NotNull ;
14+
1315import java .io .Serializable ;
1416import java .lang .ref .WeakReference ;
1517import java .util .ArrayList ;
1618import java .util .Collections ;
17- import java .util .HashMap ;
1819import java .util .HashSet ;
1920import java .util .List ;
20- import java .util .Map ;
2121import java .util .Set ;
2222
2323import io .snabble .sdk .PaymentMethod ;
2929import io .snabble .sdk .payment .PaymentCredentials ;
3030import io .snabble .sdk .payment .PaymentCredentialsStore ;
3131import io .snabble .sdk .ui .R ;
32+ import io .snabble .sdk .ui .payment .PaymentMethodMetaDataHelper ;
3233import io .snabble .sdk .utils .GsonHolder ;
3334
3435public class PaymentSelectionHelper {
@@ -41,17 +42,13 @@ public static PaymentSelectionHelper getInstance() {
4142 public static class Entry implements Serializable {
4243 String text ;
4344 String hint ;
44- transient int iconResId ;
45+ public transient int iconResId ;
4546 public PaymentCredentials paymentCredentials ;
4647 public PaymentMethod paymentMethod ;
4748 boolean isAvailable ;
4849 boolean isAdded = true ;
4950 }
5051
51- private final Map <PaymentMethod , Integer > icons = new HashMap <>();
52- private final Map <PaymentMethod , String > names = new HashMap <>();
53- private final List <PaymentMethod > paymentMethodsSortPriority = new ArrayList <>();
54-
5552 private final Application application ;
5653 private final MutableLiveData <Entry > selectedEntry ;
5754 private Project project ;
@@ -63,6 +60,9 @@ public static class Entry implements Serializable {
6360 private PaymentCredentials lastAddedPaymentCredentials ;
6461 private boolean googlePayIsReady = false ;
6562
63+ @ NotNull
64+ private PaymentMethodMetaDataHelper metaDataHelper ;
65+
6666 private final ShoppingCart .ShoppingCartListener shoppingCartListener =
6767 new ShoppingCart .SimpleShoppingCartListener () {
6868 @ Override
@@ -74,56 +74,7 @@ public void onChanged(ShoppingCart list) {
7474 private PaymentSelectionHelper () {
7575 application = Snabble .getInstance ().getApplication ();
7676
77- icons .put (PaymentMethod .DE_DIRECT_DEBIT , R .drawable .snabble_ic_payment_select_sepa );
78- icons .put (PaymentMethod .VISA , R .drawable .snabble_ic_payment_select_visa );
79- icons .put (PaymentMethod .MASTERCARD , R .drawable .snabble_ic_payment_select_mastercard );
80- icons .put (PaymentMethod .AMEX , R .drawable .snabble_ic_payment_select_amex );
81- icons .put (PaymentMethod .PAYDIREKT , R .drawable .snabble_ic_payment_select_paydirekt );
82- icons .put (PaymentMethod .TEGUT_EMPLOYEE_CARD , R .drawable .snabble_ic_payment_select_tegut );
83- icons .put (PaymentMethod .LEINWEBER_CUSTOMER_ID , R .drawable .snabble_ic_payment_select_leinweber );
84- icons .put (PaymentMethod .CUSTOMERCARD_POS , R .drawable .snabble_ic_payment_select_pos );
85- icons .put (PaymentMethod .GATEKEEPER_TERMINAL , R .drawable .snabble_ic_payment_select_sco );
86- icons .put (PaymentMethod .QRCODE_POS , R .drawable .snabble_ic_payment_select_pos );
87- icons .put (PaymentMethod .QRCODE_OFFLINE , R .drawable .snabble_ic_payment_select_pos );
88- icons .put (PaymentMethod .POST_FINANCE_CARD , R .drawable .snabble_ic_payment_select_postfinance );
89- icons .put (PaymentMethod .TWINT , R .drawable .snabble_ic_payment_select_twint );
90- icons .put (PaymentMethod .GOOGLE_PAY , R .drawable .snabble_ic_payment_select_gpay );
91- icons .put (PaymentMethod .PAYONE_SEPA , R .drawable .snabble_ic_payment_select_sepa );
92- icons .put (PaymentMethod .EXTERNAL_BILLING , R .drawable .ic_snabble_external_billing );
93-
94- names .put (PaymentMethod .DE_DIRECT_DEBIT , "SEPA-Lastschrift" );
95- names .put (PaymentMethod .VISA , "VISA" );
96- names .put (PaymentMethod .MASTERCARD , "Mastercard" );
97- names .put (PaymentMethod .AMEX , "American Express" );
98- names .put (PaymentMethod .PAYDIREKT , "Paydirekt" );
99- names .put (PaymentMethod .TEGUT_EMPLOYEE_CARD , "Tegut... Mitarbeiterkarte" );
100- names .put (PaymentMethod .LEINWEBER_CUSTOMER_ID , "Leinweber Rechnungskauf" );
101- names .put (PaymentMethod .GATEKEEPER_TERMINAL , application .getString (R .string .Snabble_Payment_payAtSCO ));
102- names .put (PaymentMethod .QRCODE_POS , application .getString (R .string .Snabble_Payment_payAtCashDesk ));
103- names .put (PaymentMethod .CUSTOMERCARD_POS , application .getString (R .string .Snabble_Payment_payAtCashDesk ));
104- names .put (PaymentMethod .QRCODE_OFFLINE , application .getString (R .string .Snabble_Payment_payAtCashDesk ));
105- names .put (PaymentMethod .POST_FINANCE_CARD , "PostFinance Card" );
106- names .put (PaymentMethod .TWINT , "Twint" );
107- names .put (PaymentMethod .GOOGLE_PAY , "Google Pay" );
108- names .put (PaymentMethod .PAYONE_SEPA , "SEPA-Lastschrift" );
109- names .put (PaymentMethod .EXTERNAL_BILLING , application .getString (R .string .Snabble_Payment_ExternalBilling_title ));
110-
111- paymentMethodsSortPriority .add (PaymentMethod .GOOGLE_PAY );
112- paymentMethodsSortPriority .add (PaymentMethod .DE_DIRECT_DEBIT );
113- paymentMethodsSortPriority .add (PaymentMethod .VISA );
114- paymentMethodsSortPriority .add (PaymentMethod .MASTERCARD );
115- paymentMethodsSortPriority .add (PaymentMethod .AMEX );
116- paymentMethodsSortPriority .add (PaymentMethod .TWINT );
117- paymentMethodsSortPriority .add (PaymentMethod .POST_FINANCE_CARD );
118- paymentMethodsSortPriority .add (PaymentMethod .PAYDIREKT );
119- paymentMethodsSortPriority .add (PaymentMethod .PAYONE_SEPA );
120- paymentMethodsSortPriority .add (PaymentMethod .GATEKEEPER_TERMINAL );
121- paymentMethodsSortPriority .add (PaymentMethod .EXTERNAL_BILLING );
122- paymentMethodsSortPriority .add (PaymentMethod .TEGUT_EMPLOYEE_CARD );
123- paymentMethodsSortPriority .add (PaymentMethod .LEINWEBER_CUSTOMER_ID );
124- paymentMethodsSortPriority .add (PaymentMethod .CUSTOMERCARD_POS );
125- paymentMethodsSortPriority .add (PaymentMethod .QRCODE_POS );
126- paymentMethodsSortPriority .add (PaymentMethod .QRCODE_OFFLINE );
77+ metaDataHelper = new PaymentMethodMetaDataHelper (application );
12778
12879 selectedEntry = new MutableLiveData <>();
12980
@@ -185,7 +136,7 @@ private void update() {
185136 Entry lastEntry = null ;
186137 if (last != null ) {
187138 lastEntry = GsonHolder .get ().fromJson (last , Entry .class );
188- Integer iconResId = icons . get (lastEntry .paymentMethod );
139+ Integer iconResId = metaDataHelper . iconFor (lastEntry .paymentMethod );
189140 if (iconResId != null ) {
190141 lastEntry .iconResId = iconResId ;
191142 }
@@ -292,11 +243,11 @@ private void updateEntries() {
292243 for (PaymentMethod paymentMethod : projectPaymentMethods ) {
293244 if (paymentMethod .isOfflineMethod ()) {
294245 final Entry e = new Entry ();
295- e .text = names . get (paymentMethod );
246+ e .text = metaDataHelper . labelFor (paymentMethod );
296247 e .paymentMethod = paymentMethod ;
297248 e .isAvailable = true ;
298249
299- Integer iconResId = icons . get (paymentMethod );
250+ Integer iconResId = metaDataHelper . iconFor (paymentMethod );
300251 if (iconResId != null ) {
301252 e .iconResId = iconResId ;
302253 entries .add (e );
@@ -338,7 +289,7 @@ private void updateEntries() {
338289 continue ;
339290 }
340291
341- e .text = names . get (e .paymentMethod );
292+ e .text = metaDataHelper . labelFor (e .paymentMethod );
342293 e .paymentCredentials = pc ;
343294
344295 if (availablePaymentMethodsList .contains (e .paymentMethod )) {
@@ -356,7 +307,7 @@ private void updateEntries() {
356307 continue ;
357308 }
358309
359- Integer iconResId = icons . get (e .paymentMethod );
310+ Integer iconResId = metaDataHelper . iconFor (e .paymentMethod );
360311 if (iconResId != null ) {
361312 e .iconResId = iconResId ;
362313 entries .add (e );
@@ -383,7 +334,7 @@ private void updateEntries() {
383334
384335 final Entry e = new Entry ();
385336
386- e .text = names . get (pm );
337+ e .text = metaDataHelper . labelFor (pm );
387338 e .paymentMethod = pm ;
388339 e .isAvailable = true ;
389340 e .isAdded = !pm .isRequiringCredentials ();
@@ -392,16 +343,16 @@ private void updateEntries() {
392343 e .hint = application .getString (R .string .Snabble_Shoppingcart_noPaymentData );
393344 }
394345
395- Integer iconResId = icons . get (pm );
346+ Integer iconResId = metaDataHelper . iconFor (pm );
396347 if (iconResId != null ) {
397348 e .iconResId = iconResId ;
398349 entries .add (e );
399350 }
400351 }
401352
402353 Collections .sort (entries , (o1 , o2 ) -> {
403- int p1 = paymentMethodsSortPriority .indexOf (o1 .paymentMethod );
404- int p2 = paymentMethodsSortPriority .indexOf (o2 .paymentMethod );
354+ int p1 = metaDataHelper .indexOf (o1 .paymentMethod );
355+ int p2 = metaDataHelper .indexOf (o2 .paymentMethod );
405356
406357 return Integer .compare (p1 , p2 );
407358 });
0 commit comments