11package io .snabble .sdk .ui .payment ;
22
33import android .os .Bundle ;
4+ import android .os .Parcelable ;
45import android .view .LayoutInflater ;
56import android .view .View ;
67import android .view .ViewGroup ;
1819import java .util .HashSet ;
1920import java .util .List ;
2021import java .util .Set ;
22+ import java .util .stream .Collectors ;
2123
2224import io .snabble .sdk .PaymentMethod ;
2325import io .snabble .sdk .Project ;
2729
2830public class SelectPaymentMethodFragment extends BottomSheetDialogFragment {
2931 public static final String ARG_PROJECT_ID = "projectId" ;
32+ public static final String ARG_SUPPORTED_METHODS = "supportedMethods" ;
3033
3134 private List <Entry > entries ;
3235 private Set <PaymentMethod > paymentMethods ;
3336 private String projectId ;
3437
38+ @ Nullable
39+ private List <PaymentMethod > supportedMethods ;
40+
3541 @ Override
3642 public void onCreate (@ Nullable Bundle savedInstanceState ) {
3743 super .onCreate (savedInstanceState );
3844
3945 Bundle args = getArguments ();
4046 if (args != null ) {
4147 projectId = args .getString (ARG_PROJECT_ID , null );
48+ supportedMethods = convertToList (args .getParcelableArray (ARG_SUPPORTED_METHODS ), PaymentMethod .class );
4249 }
4350 }
4451
@@ -51,7 +58,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5158 Set <PaymentMethod > availablePaymentMethods = new HashSet <>();
5259 for (Project project : Snabble .getInstance ().getProjects ()) {
5360 if (project .getId ().equals (projectId )) {
54- availablePaymentMethods .addAll (project .getAvailablePaymentMethods ());
61+ final List <PaymentMethod > methods = project .getAvailablePaymentMethods ()
62+ .stream ()
63+ .filter (this ::isSupportedMethod )
64+ .collect (Collectors .toList ());
65+ availablePaymentMethods .addAll (methods );
5566 }
5667 }
5768
@@ -188,6 +199,13 @@ public void click() {
188199 return v ;
189200 }
190201
202+ private boolean isSupportedMethod (@ NonNull final PaymentMethod method ) {
203+ if (supportedMethods != null ) {
204+ return supportedMethods .contains (method );
205+ }
206+ return true ;
207+ }
208+
191209 private String getUsableAtText (PaymentMethod ... paymentMethods ) {
192210 List <Project > projects = Snabble .getInstance ().getProjects ();
193211 if (projects .size () == 1 ) {
@@ -219,6 +237,20 @@ private String getUsableAtText(PaymentMethod... paymentMethods) {
219237 return getResources ().getString (R .string .Snabble_Payment_usableAt , sb .toString ());
220238 }
221239
240+ @ SuppressWarnings ("unchecked" )
241+ @ Nullable
242+ private <T > List <T > convertToList (final Parcelable [] parcels , final Class <T > clazz ) {
243+ if (parcels == null ) return null ;
244+
245+ final List <T > list = new ArrayList <>(parcels .length );
246+ for (final Parcelable parcel : parcels ) {
247+ if (parcel .getClass () == clazz ) {
248+ list .add (((T ) parcel ));
249+ }
250+ }
251+ return list ;
252+ }
253+
222254 private static class Entry {
223255 int drawableRes ;
224256 String text ;
0 commit comments