Skip to content

Commit

Permalink
feat: added adding text blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
xeewii committed May 24, 2024
1 parent 67b64b3 commit 42bea2d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ static class Element {
public String label_show;
public ArrayList<Product> products = new ArrayList<>();
public Product item;
public String text_input;
public int y_offset;

public Element(@NonNull JSONObject json) throws JSONException {
type = json.getString("type");
Expand Down Expand Up @@ -115,6 +117,12 @@ public Element(@NonNull JSONObject json) throws JSONException {
if( json.has("item") && type.equals("product") ) {
item = new Product(json.getJSONObject("item"));
}
if( json.has("text_input") ) {
text_input = json.getString("text_input");
}
if( json.has("y_offset") ) {
y_offset = json.getInt("y_offset");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.personalizatio.stories;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
Expand All @@ -12,12 +13,14 @@
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -86,6 +89,7 @@ public interface OnPageListener {

//Элементы управления
private Button button;
private FrameLayout text_blocks_layout;
public ImageButton reload;
public View reload_layout;
private TextView reload_text;
Expand All @@ -98,6 +102,8 @@ public interface OnPageListener {
private RecyclerView products;
private ProductsAdapter products_adapter;

private int screenHeight;

public StoryItemView(@NonNull Context context) {
super(context);
}
Expand Down Expand Up @@ -143,6 +149,7 @@ private void init() {

//Элементы управления
button = findViewById(android.R.id.button1);
text_blocks_layout = findViewById(R.id.text_blocks_layout);
reload = findViewById(R.id.reload);
reload_layout = findViewById(R.id.reload_layout);
reload_text = findViewById(R.id.reload_text);
Expand Down Expand Up @@ -171,6 +178,8 @@ private void init() {
product_discount_box = findViewById(R.id.product_discount_box);
product_image = findViewById(R.id.product_image);
promocode_text = findViewById(R.id.promocode_text);

screenHeight = getScreenHeight(getContext());
}

/**
Expand Down Expand Up @@ -478,7 +487,47 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
return false;
}
});
case "text_block":
var textView = new TextView(getContext());
textView.setLayoutParams(new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));

textView.setText(element.text_input);

var y = screenHeight * element.y_offset / 100f;
textView.setY(y);

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
textView.setBackgroundTintList(ColorStateList.valueOf(element.background == null ? button.getContext().getResources().getColor(R.color.primary) : Color.parseColor(element.background)));
} else {
textView.setBackgroundColor(element.background == null ? button.getContext().getResources().getColor(R.color.primary) : Color.parseColor(element.background));
}
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
textView.setTextColor(ColorStateList.valueOf(element.color == null ? button.getContext().getResources().getColor(R.color.white) : Color.parseColor(element.color)));
} else {
textView.setTextColor(element.color == null ? button.getContext().getResources().getColor(R.color.white) : Color.parseColor(element.color));
}

text_blocks_layout.addView(textView);

break;
}
}
}

public static int getScreenHeight(Context context) {
var activity = (Activity) context;

if( activity == null ) return 0;

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ) {
var windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
return windowMetrics.getBounds().height();
} else {
var displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
}
}
6 changes: 6 additions & 0 deletions personalizatio-sdk/src/main/res/layout/story_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/text_blocks_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down

0 comments on commit 42bea2d

Please sign in to comment.