2323import androidx .recyclerview .widget .RecyclerView ;
2424
2525import com .google .android .material .floatingactionbutton .FloatingActionButton ;
26+ import com .google .android .material .progressindicator .CircularProgressIndicator ;
2627
2728import java .text .SimpleDateFormat ;
2829import 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 ();
0 commit comments