Skip to content

Commit a24de7b

Browse files
authored
Option to limit payment methods offered to add (#173)
1 parent 944b55d commit a24de7b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file.
77
### Removed
88
### Fixed
99

10+
## [0.72.4]
11+
### Added
12+
* core, io: Option to limit payment methods offered to add (#173)
13+
1014
## [0.72.3]
1115
### Fixed
1216
* core, ui: Bind Giropay to its project (#172)

core/src/main/java/io/snabble/sdk/PaymentMethod.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.snabble.sdk
22

3+
import android.os.Parcel
4+
import android.os.Parcelable
35
import androidx.annotation.Nullable
46
import com.google.gson.annotations.SerializedName
57

@@ -31,7 +33,7 @@ enum class PaymentMethod(
3133
* Declares if the payment method can only be aborted with a confirmation by the backend
3234
*/
3335
val needsAbortConfirmation: Boolean,
34-
) {
36+
) : Parcelable {
3537

3638
@SerializedName("qrCodePOS")
3739
QRCODE_POS(
@@ -168,8 +170,18 @@ enum class PaymentMethod(
168170
needsAbortConfirmation = true
169171
);
170172

173+
override fun describeContents(): Int = 0
174+
175+
override fun writeToParcel(dest: Parcel, flags: Int) = dest.writeInt(ordinal)
176+
171177
companion object {
172178

179+
@JvmField
180+
val CREATOR = object : Parcelable.Creator<PaymentMethod> {
181+
override fun createFromParcel(parcel: Parcel) = PaymentMethod.values()[parcel.readInt()]
182+
override fun newArray(size: Int) = arrayOfNulls<PaymentMethod>(size)
183+
}
184+
173185
/**
174186
* Converts a payment method from its string representation.
175187
*/

ui/src/main/java/io/snabble/sdk/ui/payment/SelectPaymentMethodFragment.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.snabble.sdk.ui.payment;
22

33
import android.os.Bundle;
4+
import android.os.Parcelable;
45
import android.view.LayoutInflater;
56
import android.view.View;
67
import android.view.ViewGroup;
@@ -18,6 +19,7 @@
1819
import java.util.HashSet;
1920
import java.util.List;
2021
import java.util.Set;
22+
import java.util.stream.Collectors;
2123

2224
import io.snabble.sdk.PaymentMethod;
2325
import io.snabble.sdk.Project;
@@ -27,18 +29,23 @@
2729

2830
public 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

Comments
 (0)