Skip to content

Commit e50e917

Browse files
committed
do not allow maxSizeMM to be bigger than the view itself
1 parent 73684a8 commit e50e917

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutOfflineView.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55
import android.graphics.Bitmap;
6+
import android.graphics.Rect;
67
import android.util.AttributeSet;
78
import android.util.DisplayMetrics;
89
import android.view.Gravity;
@@ -48,6 +49,7 @@ public class CheckoutOfflineView extends FrameLayout implements Checkout.OnCheck
4849
private View upArrow;
4950
private CodeListViewAdapter viewPagerAdapter;
5051
private boolean isSmallDevice;
52+
private View viewPagerContainer;
5153

5254
public CheckoutOfflineView(Context context) {
5355
super(context);
@@ -72,11 +74,6 @@ private void init() {
7274

7375
maxSizeMm = options.maxSizeMm;
7476

75-
// too large, use normal scaling too save memory
76-
if (maxSizeMm > 100) {
77-
maxSizeMm = 0;
78-
}
79-
8077
encodedCodesGenerator = new EncodedCodesGenerator(options);
8178

8279
paidButton = findViewById(R.id.paid);
@@ -111,6 +108,8 @@ public void click() {
111108
upArrow = findViewById(R.id.arrow);
112109

113110
viewPager = findViewById(R.id.view_pager);
111+
viewPagerContainer = findViewById(R.id.view_pager_container);
112+
114113
viewPagerAdapter = new CodeListViewAdapter();
115114
viewPager.setAdapter(viewPagerAdapter);
116115
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@@ -148,8 +147,6 @@ public void onPageSelected(int position) {
148147
checkoutId.setVisibility(View.GONE);
149148
}
150149

151-
project.getAssets().get("checkout-offline", this::setHelperImage);
152-
153150
checkout = SnabbleUI.getProject().getCheckout();
154151
onStateChanged(checkout.getState());
155152
}
@@ -174,6 +171,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
174171
project.getAssets().get("checkout-offline", this::setHelperImage);
175172
});
176173
}
174+
175+
viewPagerAdapter.notifyDataSetChanged();
177176
}
178177
}
179178

@@ -279,15 +278,25 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
279278
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
280279
int margin = Math.round(16 * getResources().getDisplayMetrics().density);
281280
if (maxSizeMm > 0) {
282-
// TODO what if maxSizeMm is bigger than screen?
283281
DisplayMetrics dm = getResources().getDisplayMetrics();
284282
float pixelsPerMmX = dm.xdpi / 25.4f;
283+
285284
int size = (int) (pixelsPerMmX * maxSizeMm);
285+
Rect rect = new Rect();
286+
viewPagerContainer.getGlobalVisibleRect(rect);
287+
288+
int w = rect.width();
289+
int h = rect.height();
290+
291+
if (size > w || size > h) {
292+
size = ViewGroup.LayoutParams.MATCH_PARENT;
293+
}
286294

287295
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
288296
ViewGroup.LayoutParams.MATCH_PARENT,
289297
size,
290298
Gravity.CENTER);
299+
291300
lp.leftMargin = margin;
292301
lp.rightMargin = margin;
293302
holder.barcodeView.setLayoutParams(lp);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
android:layout_marginTop="16dp"
1313
android:layout_marginLeft="16dp"
1414
android:layout_marginRight="16dp"
15+
android:layout_alignParentTop="true"
1516
android:orientation="vertical">
1617

1718
<ImageView
@@ -43,12 +44,17 @@
4344
android:textSize="17sp"/>
4445
</LinearLayout>
4546

46-
<androidx.viewpager2.widget.ViewPager2
47-
android:id="@+id/view_pager"
47+
<FrameLayout
48+
android:id="@+id/view_pager_container"
4849
android:layout_width="match_parent"
4950
android:layout_height="match_parent"
5051
android:layout_below="@+id/helper_container"
51-
android:layout_above="@+id/checkout_id" />
52+
android:layout_above="@+id/checkout_id">
53+
<androidx.viewpager2.widget.ViewPager2
54+
android:id="@+id/view_pager"
55+
android:layout_width="match_parent"
56+
android:layout_height="match_parent" />
57+
</FrameLayout>
5258

5359
<TextView
5460
android:id="@+id/checkout_id"

0 commit comments

Comments
 (0)