Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: stories - add text blocks #8

Merged
merged 14 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 +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 @@ -102,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 @@ -115,6 +131,33 @@ 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");
}
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
Expand Up @@ -18,18 +18,21 @@
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;
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 @@ -47,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 @@ -86,6 +86,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 +99,9 @@ public interface OnPageListener {
private RecyclerView products;
private ProductsAdapter products_adapter;

private int viewHeight;
private int viewTopOffset;

public StoryItemView(@NonNull Context context) {
super(context);
}
Expand Down Expand Up @@ -143,6 +147,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 @@ -196,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 @@ -478,7 +488,113 @@ public boolean onResourceReady(Drawable resource, Object model, Target<Drawable>
return false;
}
});
case "text_block":
var textView = new TextView(getContext());

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

textView.setText(element.text_input);

textView.setPadding(2, element.font_size, 2, element.font_size);

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);
}
}
}

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;
}
}
}

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;
};
}

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.LOLLIPOP ) {
textView.setTextColor(ColorStateList.valueOf(color));
} else {
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.
7 changes: 7 additions & 0 deletions personalizatio-sdk/src/main/res/layout/story_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,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