diff --git a/CHANGELOG.md b/CHANGELOG.md index 12a7bbdd8..15fb0f9a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Changed ### Removed ### Fixed +* ui: fix crash when adding a payment mehtod (APPS-2155) ## [0.80.5] ### Added diff --git a/ui/src/main/java/io/snabble/sdk/ui/payment/PaymentCredentialsListView.java b/ui/src/main/java/io/snabble/sdk/ui/payment/PaymentCredentialsListView.java index 0cc0d172a..1ab5e6f2d 100644 --- a/ui/src/main/java/io/snabble/sdk/ui/payment/PaymentCredentialsListView.java +++ b/ui/src/main/java/io/snabble/sdk/ui/payment/PaymentCredentialsListView.java @@ -23,6 +23,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.google.android.material.progressindicator.CircularProgressIndicator; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -50,6 +51,8 @@ public class PaymentCredentialsListView extends FrameLayout implements PaymentCr private List types; private Project project; private View emptyState; + private FloatingActionButton fab; + private CircularProgressIndicator circularIndicator; public PaymentCredentialsListView(Context context) { super(context); @@ -69,17 +72,19 @@ public PaymentCredentialsListView(Context context, AttributeSet attrs, int defSt private void inflateView() { inflate(getContext(), R.layout.snabble_view_payment_credentials_list, this); if (isInEditMode()) return; + emptyState = findViewById(R.id.empty_state); + recyclerView = findViewById(R.id.recycler_view); + fab = findViewById(R.id.fab); + circularIndicator = findViewById(R.id.circular_indicator); + paymentCredentialsStore = Snabble.getInstance().getPaymentCredentialsStore(); - recyclerView = findViewById(R.id.recycler_view); recyclerView.setAdapter(new PaymentCredentialsListView.Adapter()); LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); recyclerView.setLayoutManager(layoutManager); recyclerView.setItemAnimator(null); - FloatingActionButton fab = findViewById(R.id.fab); - final Project currentProject = Snabble.getInstance().getCheckedInProject().getLatestValue(); final int primaryColor = RemoteThemingHelper.primaryColorForProject(getContext(), currentProject); final int onPrimaryColor = RemoteThemingHelper.onPrimaryColorForProject(getContext(), currentProject); @@ -97,17 +102,11 @@ public void click() { Bundle bundle = new Bundle(); Project p = project; - if (p == null) { - p = Snabble.getInstance().getCheckedInProject().getValue(); - } - - if (p == null) { - throw new IllegalStateException("Cannot get current project. Did you forget to set the projectId to the PaymentCredentialsListFragment or PaymentCredentialsListView?"); + if (p != null) { + bundle.putString(SelectPaymentMethodFragment.ARG_PROJECT_ID, p.getId()); + dialogFragment.setArguments(bundle); + dialogFragment.show(((FragmentActivity) activity).getSupportFragmentManager(), null); } - - bundle.putString(SelectPaymentMethodFragment.ARG_PROJECT_ID, p.getId()); - dialogFragment.setArguments(bundle); - dialogFragment.show(((FragmentActivity) activity).getSupportFragmentManager(), null); } else { throw new RuntimeException("Host activity must be a Fragment Activity"); } @@ -124,12 +123,12 @@ public void click() { } } }); - - emptyState = findViewById(R.id.empty_state); } public void setProject(Project project) { this.project = project; + circularIndicator.setVisibility(View.GONE); + onChanged(); } public void show(List types, Project project) { @@ -137,6 +136,7 @@ public void show(List types, Project project) { this.project = project; entries.clear(); + circularIndicator.setVisibility(View.GONE); onChanged(); } @@ -184,24 +184,27 @@ public void onChanged() { List paymentCredentials = paymentCredentialsStore.getAll(); for (PaymentCredentials pm : paymentCredentials) { - boolean sameProjectOrNull = true; + boolean sameProject = false; if (project != null) { - sameProjectOrNull = project.getId().equals(pm.getProjectId()); + sameProject = project.getId().equals(pm.getProjectId()); } if (pm.getProjectId() == null && (pm.getType() == Type.SEPA || pm.getType() == Type.PAYONE_SEPA)) { - sameProjectOrNull = true; + sameProject = true; } - boolean sameTypeOrNull = true; + boolean sameType = false; if (types != null) { if (project != null) { - sameTypeOrNull = project.getAvailablePaymentMethods().contains(pm.getPaymentMethod()); + sameType = project.getAvailablePaymentMethods().contains(pm.getPaymentMethod()); } else { - sameTypeOrNull = types.contains(pm.getType()); + sameType = types.contains(pm.getType()); } } - if (pm.isAvailableInCurrentApp() && sameTypeOrNull && sameProjectOrNull) { + + boolean isRetailer = Snabble.getInstance().getProjects().size() == 1; + + if ((pm.isAvailableInCurrentApp() && sameType && sameProject) || isRetailer) { switch (pm.getType()) { case SEPA: case PAYONE_SEPA: @@ -231,11 +234,15 @@ public void onChanged() { } if (entries.size() == 0) { - emptyState.setVisibility(View.VISIBLE); + if (project != null) { + emptyState.setVisibility(View.VISIBLE); + fab.setVisibility(View.VISIBLE); + } recyclerView.setVisibility(View.GONE); } else { emptyState.setVisibility(View.GONE); recyclerView.setVisibility(View.VISIBLE); + fab.setVisibility(View.VISIBLE); } recyclerView.getAdapter().notifyDataSetChanged(); diff --git a/ui/src/main/res/layout/snabble_view_payment_credentials_list.xml b/ui/src/main/res/layout/snabble_view_payment_credentials_list.xml index 0d3353f8d..3bf1905c3 100644 --- a/ui/src/main/res/layout/snabble_view_payment_credentials_list.xml +++ b/ui/src/main/res/layout/snabble_view_payment_credentials_list.xml @@ -5,6 +5,15 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> + + +