Skip to content

Commit fa45782

Browse files
authored
Fix crash when adding a credit card (APPS-2155) (#241)
1 parent c8367f5 commit fa45782

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
66
### Changed
77
### Removed
88
### Fixed
9+
* ui: fix crash when adding a payment mehtod (APPS-2155)
910

1011
## [0.80.5]
1112
### Added

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

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import androidx.recyclerview.widget.RecyclerView;
2424

2525
import com.google.android.material.floatingactionbutton.FloatingActionButton;
26+
import com.google.android.material.progressindicator.CircularProgressIndicator;
2627

2728
import java.text.SimpleDateFormat;
2829
import java.util.ArrayList;
@@ -50,6 +51,8 @@ public class PaymentCredentialsListView extends FrameLayout implements PaymentCr
5051
private List<Type> types;
5152
private Project project;
5253
private View emptyState;
54+
private FloatingActionButton fab;
55+
private CircularProgressIndicator circularIndicator;
5356

5457
public PaymentCredentialsListView(Context context) {
5558
super(context);
@@ -69,17 +72,19 @@ public PaymentCredentialsListView(Context context, AttributeSet attrs, int defSt
6972
private void inflateView() {
7073
inflate(getContext(), R.layout.snabble_view_payment_credentials_list, this);
7174
if (isInEditMode()) return;
75+
emptyState = findViewById(R.id.empty_state);
76+
recyclerView = findViewById(R.id.recycler_view);
77+
fab = findViewById(R.id.fab);
78+
circularIndicator = findViewById(R.id.circular_indicator);
79+
7280
paymentCredentialsStore = Snabble.getInstance().getPaymentCredentialsStore();
7381

74-
recyclerView = findViewById(R.id.recycler_view);
7582
recyclerView.setAdapter(new PaymentCredentialsListView.Adapter());
7683

7784
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
7885
recyclerView.setLayoutManager(layoutManager);
7986
recyclerView.setItemAnimator(null);
8087

81-
FloatingActionButton fab = findViewById(R.id.fab);
82-
8388
final Project currentProject = Snabble.getInstance().getCheckedInProject().getLatestValue();
8489
final int primaryColor = RemoteThemingHelper.primaryColorForProject(getContext(), currentProject);
8590
final int onPrimaryColor = RemoteThemingHelper.onPrimaryColorForProject(getContext(), currentProject);
@@ -97,17 +102,11 @@ public void click() {
97102
Bundle bundle = new Bundle();
98103

99104
Project p = project;
100-
if (p == null) {
101-
p = Snabble.getInstance().getCheckedInProject().getValue();
102-
}
103-
104-
if (p == null) {
105-
throw new IllegalStateException("Cannot get current project. Did you forget to set the projectId to the PaymentCredentialsListFragment or PaymentCredentialsListView?");
105+
if (p != null) {
106+
bundle.putString(SelectPaymentMethodFragment.ARG_PROJECT_ID, p.getId());
107+
dialogFragment.setArguments(bundle);
108+
dialogFragment.show(((FragmentActivity) activity).getSupportFragmentManager(), null);
106109
}
107-
108-
bundle.putString(SelectPaymentMethodFragment.ARG_PROJECT_ID, p.getId());
109-
dialogFragment.setArguments(bundle);
110-
dialogFragment.show(((FragmentActivity) activity).getSupportFragmentManager(), null);
111110
} else {
112111
throw new RuntimeException("Host activity must be a Fragment Activity");
113112
}
@@ -124,19 +123,20 @@ public void click() {
124123
}
125124
}
126125
});
127-
128-
emptyState = findViewById(R.id.empty_state);
129126
}
130127

131128
public void setProject(Project project) {
132129
this.project = project;
130+
circularIndicator.setVisibility(View.GONE);
131+
onChanged();
133132
}
134133

135134
public void show(List<Type> types, Project project) {
136135
this.types = types;
137136
this.project = project;
138137

139138
entries.clear();
139+
circularIndicator.setVisibility(View.GONE);
140140
onChanged();
141141
}
142142

@@ -184,24 +184,27 @@ public void onChanged() {
184184
List<PaymentCredentials> paymentCredentials = paymentCredentialsStore.getAll();
185185

186186
for (PaymentCredentials pm : paymentCredentials) {
187-
boolean sameProjectOrNull = true;
187+
boolean sameProject = false;
188188
if (project != null) {
189-
sameProjectOrNull = project.getId().equals(pm.getProjectId());
189+
sameProject = project.getId().equals(pm.getProjectId());
190190
}
191191

192192
if (pm.getProjectId() == null && (pm.getType() == Type.SEPA || pm.getType() == Type.PAYONE_SEPA)) {
193-
sameProjectOrNull = true;
193+
sameProject = true;
194194
}
195195

196-
boolean sameTypeOrNull = true;
196+
boolean sameType = false;
197197
if (types != null) {
198198
if (project != null) {
199-
sameTypeOrNull = project.getAvailablePaymentMethods().contains(pm.getPaymentMethod());
199+
sameType = project.getAvailablePaymentMethods().contains(pm.getPaymentMethod());
200200
} else {
201-
sameTypeOrNull = types.contains(pm.getType());
201+
sameType = types.contains(pm.getType());
202202
}
203203
}
204-
if (pm.isAvailableInCurrentApp() && sameTypeOrNull && sameProjectOrNull) {
204+
205+
boolean isRetailer = Snabble.getInstance().getProjects().size() == 1;
206+
207+
if ((pm.isAvailableInCurrentApp() && sameType && sameProject) || isRetailer) {
205208
switch (pm.getType()) {
206209
case SEPA:
207210
case PAYONE_SEPA:
@@ -231,11 +234,15 @@ public void onChanged() {
231234
}
232235

233236
if (entries.size() == 0) {
234-
emptyState.setVisibility(View.VISIBLE);
237+
if (project != null) {
238+
emptyState.setVisibility(View.VISIBLE);
239+
fab.setVisibility(View.VISIBLE);
240+
}
235241
recyclerView.setVisibility(View.GONE);
236242
} else {
237243
emptyState.setVisibility(View.GONE);
238244
recyclerView.setVisibility(View.VISIBLE);
245+
fab.setVisibility(View.VISIBLE);
239246
}
240247

241248
recyclerView.getAdapter().notifyDataSetChanged();

ui/src/main/res/layout/snabble_view_payment_credentials_list.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
xmlns:tools="http://schemas.android.com/tools"
66
android:layout_width="match_parent"
77
android:layout_height="match_parent">
8+
9+
<com.google.android.material.progressindicator.CircularProgressIndicator
10+
android:id="@+id/circular_indicator"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:layout_gravity="center"
14+
app:indicatorSize="48dp"
15+
android:indeterminate="true"/>
16+
817
<androidx.recyclerview.widget.RecyclerView
918
android:layout_width="match_parent"
1019
android:layout_height="match_parent"
@@ -45,6 +54,7 @@
4554
android:layout_width="wrap_content"
4655
android:layout_height="wrap_content"
4756
android:layout_gravity="bottom|end"
57+
android:visibility="gone"
4858
app:tint="?attr/colorOnSecondary"
4959
android:src="@drawable/snabble_ic_add"
5060
android:layout_margin="16dp"/>

0 commit comments

Comments
 (0)