Skip to content

Commit

Permalink
feat: implemented fields in element story
Browse files Browse the repository at this point in the history
  • Loading branch information
xeewii committed May 27, 2024
1 parent f87ea4b commit e101412
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 43 deletions.
2 changes: 1 addition & 1 deletion personalizatio-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'signing'

version='1.8.9'
version='1.8.19'

android {
compileSdk 34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,20 @@ static class Element {
public String background;
public String color;
public Boolean text_bold = false;
public Boolean text_italic = false;
public String label_hide;
public String label_show;
public ArrayList<Product> products = new ArrayList<>();
public Product item;
public String text_input;
public int y_offset;
public String font_type;
public int font_size = 14;
public String text_align;
public String text_color;
public double text_line_spacing;
public String text_background_color = "#FFFFFF";
public String text_background_color_opacity = "0%";

public Element(@NonNull JSONObject json) throws JSONException {
type = json.getString("type");
Expand Down Expand Up @@ -104,6 +112,12 @@ public Element(@NonNull JSONObject json) throws JSONException {
if( json.has("text_bold") ) {
text_bold = json.getBoolean("text_bold");
}
if( json.has("bold") ) {
text_bold = json.getBoolean("bold");
}
if( json.has("italic") ) {
text_italic = json.getBoolean("italic");
}
if( json.has("labels") ) {
label_hide = json.getJSONObject("labels").getString("hide_carousel");
label_show = json.getJSONObject("labels").getString("show_carousel");
Expand All @@ -123,6 +137,27 @@ public Element(@NonNull JSONObject json) throws JSONException {
if( json.has("y_offset") ) {
y_offset = json.getInt("y_offset");
}
if( json.has("font_type") ) {
font_type = json.getString("font_type");
}
if( json.has("font_size") ) {
font_size = json.getInt("font_size");
}
if( json.has("text_align") ) {
text_align = json.getString("text_align");
}
if( json.has("text_color") ) {
text_color = json.getString("text_color");
}
if( json.has("text_line_spacing") ) {
text_line_spacing = json.getDouble("text_line_spacing");
}
if( json.has("text_background_color") ) {
text_background_color = json.getString("text_background_color");
}
if( json.has("text_background_color_opacity") ) {
text_background_color_opacity = json.getString("text_background_color_opacity");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 @@ -13,7 +12,6 @@
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;
Expand All @@ -27,12 +25,14 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.FontRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.view.ViewCompat;
import androidx.media3.ui.PlayerView;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -50,13 +50,10 @@
import com.google.android.material.shape.CornerFamily;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.personalizatio.OnLinkClickListener;
import com.personalizatio.Product;
import com.personalizatio.R;
import com.personalizatio.SDK;

import org.w3c.dom.Text;

final class StoryItemView extends ConstraintLayout {

public interface OnPageListener {
Expand Down Expand Up @@ -102,7 +99,8 @@ public interface OnPageListener {
private RecyclerView products;
private ProductsAdapter products_adapter;

private int screenHeight;
private int viewHeight;
private int viewTopOffset;

public StoryItemView(@NonNull Context context) {
super(context);
Expand Down Expand Up @@ -178,8 +176,6 @@ 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 All @@ -205,6 +201,11 @@ public void setOnTouchListener(OnTouchListener listener) {
super.setOnTouchListener((v, event) -> true);
}

public void setViewSize(int height, int topOffset) {
viewHeight = height;
viewTopOffset = topOffset;
}

/**
* Обновляет данные слайда
*
Expand Down Expand Up @@ -489,45 +490,111 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
});
case "text_block":
var textView = new TextView(getContext());
textView.setLayoutParams(new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));

var layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);

textView.setText(element.text_input);

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

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));
}
textView.setY(viewHeight * element.y_offset / 100f + viewTopOffset);

textView.setTextSize(element.font_size);

var typeface = ResourcesCompat.getFont(getContext(), getFontRes(element.font_type, element.text_bold, element.text_italic));
textView.setTypeface(typeface, getTypefaceStyle(element.text_bold, element.text_italic));

textView.setTextAlignment(getTextAlignment(element.text_align));

textView.setLineSpacing(textView.getLineHeight(), (float)element.text_line_spacing);

setTextBackgroundColor(textView, element.text_background_color, element.text_background_color_opacity);
setTextColor(textView, element.text_color);

text_blocks_layout.addView(textView);
}
}
}

break;
private @FontRes int getFontRes(String fontType, boolean bold, boolean italic) {
switch( fontType ) {
case "serif": {
if( bold && italic ) return R.font.droid_serif_bold_italic;
if( bold ) return R.font.droid_serif_bold;
if( italic ) return R.font.droid_serif_italic;
return R.font.droid_serif_regular;
}
case "sans-serif": {
if( bold ) return R.font.droid_sans_bold;
return R.font.droid_sans_regular;
}
case "monospaced":
default: {
return R.font.droid_sans_mono;
}
}
}

public static int getScreenHeight(Context context) {
var activity = (Activity) context;
private static int getTypefaceStyle(boolean bold, boolean italic) {
if( bold && italic ) return Typeface.BOLD_ITALIC;
if( bold ) return Typeface.BOLD;
if( italic ) return Typeface.ITALIC;
return Typeface.NORMAL;
}

private static int getTextAlignment(String textAlign) {
return switch (textAlign) {
case "center" -> View.TEXT_ALIGNMENT_CENTER;
case "right" -> View.TEXT_ALIGNMENT_TEXT_END;
default -> View.TEXT_ALIGNMENT_TEXT_START;
};
}

if( activity == null ) return 0;
private static void setTextColor(TextView textView, String colorString) {
if( !colorString.startsWith("#") ) {
colorString = "#FFFFFF";
}

var color = Color.parseColor(colorString);

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.R ) {
var windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
return windowMetrics.getBounds().height();
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
textView.setTextColor(ColorStateList.valueOf(color));
} else {
var displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
textView.setTextColor(color);
}
}

private static void setTextBackgroundColor(TextView textView, String colorString, String colorOpacityString) {
if( !colorString.startsWith("#") ) {
colorString = "#FFFFFF";
}

var colorOpacity = GetColorOpacity(colorOpacityString);
var colorOpacityValueString = Integer.toString(colorOpacity,16);
if( colorOpacityValueString.length() == 1) colorOpacityValueString = 0 + colorOpacityValueString;

var fullColorString = colorString.replace("#", "#" + colorOpacityValueString);

textView.setBackgroundColor(Color.parseColor(fullColorString));
}

private static int GetColorOpacity(String percentsString) {
var percents = 0;

try {
percents = Integer.parseInt(percentsString);
}
catch( NumberFormatException e ) {
try {
if (!percentsString.isEmpty()) {
percents = Integer.parseInt(percentsString.substring(0, percentsString.length() - 1));
}
}
catch( NumberFormatException ignored) {
}
}

return 255 * percents / 100;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.personalizatio.stories;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.media3.common.C;
import androidx.media3.common.PlaybackException;
Expand All @@ -21,7 +20,6 @@
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.personalizatio.OnLinkClickListener;
import com.personalizatio.R;
import com.personalizatio.SDK;

Expand Down Expand Up @@ -49,6 +47,8 @@ final class StoryView extends ConstraintLayout implements StoriesProgressView.St
//Heading
private final StoryDialog.OnProgressState state_listener;

private Pair<Integer, Integer> viewPagerSize = null;

@SuppressLint("ClickableViewAccessibility")
public StoryView(StoriesView stories_view, StoryDialog.OnProgressState state_listener) {
super(stories_view.getContext());
Expand Down Expand Up @@ -216,8 +216,16 @@ class PagerHolder extends RecyclerView.ViewHolder {

public PagerHolder(@NonNull View view) {
super(view);

story_item = (StoryItemView) view;
story_item.setStoriesView(stories_view);
if(viewPagerSize == null) {
var viewPagerHeight = mViewPager.getHeight();
var params = (ConstraintLayout.LayoutParams) storiesProgressView.getLayoutParams();
var viewPagerTopOffset = params.height + params.bottomMargin + params.topMargin;
viewPagerSize = new Pair<>(viewPagerHeight, viewPagerTopOffset);
}
story_item.setViewSize(viewPagerSize.first, viewPagerSize.second);
story_item.setOnPageListener(new StoryItemView.OnPageListener() {
@Override
public void onPrev() {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 7 additions & 6 deletions personalizatio-sdk/src/main/res/layout/story_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
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 Expand Up @@ -53,6 +47,13 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
android:id="@+id/text_blocks_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">
</FrameLayout>

<LinearLayout
android:id="@+id/reload_layout"
Expand Down

0 comments on commit e101412

Please sign in to comment.