Skip to content

Commit

Permalink
Follow up PR 22 (#23)
Browse files Browse the repository at this point in the history
* Normalize code

* Upgrade AGP to 4.1.1

* Rename samples
  • Loading branch information
pgreze authored Nov 18, 2020
1 parent 18b82a2 commit 33d5d13
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 77 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ val popup = reactionPopup(this, ::onReactionSelected) {
// Alternative with drawable resource id array
reactionsIds = intArrayOf(R.drawable.img1, R.drawable.img2, R.drawable.img3)

// Optional popup/item styles
// Optional popup style
popupGravity = PopupGravity.DEFAULT
popupMargin = resources.getDimensionPixelSize(R.dimen.horizontal_margin)
popupCornerRadius = TypedValue.applyDimension(COMPLEX_UNIT_DIP, cornerSizeInDp.toFloat(), resources.displayMetrics)
popupColor = Color.WHITE
popupAlphaValue = 230

// Optional item style
reactionSize = resources.getDimensionPixelSize(R.dimen.item_size)
horizontalMargin = resources.getDimensionPixelSize(R.dimen.item_margin)
verticalMargin = resources.getDimensionPixelSize(R.dimen.item_margin)
Expand All @@ -118,15 +122,6 @@ val popup = reactionPopup(this, ::onReactionSelected) {
textColor = Color.BLACK
textHorizontalPadding = resources.getDimension(R.dimen.text_padding)
textVerticalPadding = resources.getDimension(R.dimen.text_padding)
// Applying the popup rounded corners radius in density pixels
cornerSizeInDp = 8
/*
* Applying popup background alpha value
* Value must be in between 0 to 255 where
* 0 - Transparent (100% Transparency)
* 255 - Opaque (0% Transparency)
*/
popupAlphaValue = 255
}
```

Expand All @@ -145,9 +140,14 @@ Java:
.withVerticalReactionMargin(margin / 2)
// Override popup gravity
.withPopupGravity(PopupGravity.PARENT_RIGHT)
// Margin between items (default: R.dimen.reactions_item_margin)
.withPopupMargin(margin)
// Popup corners radius (default: 90)
.withCornerRadius(getResources().getDimensionPixelSize(R.dimen.corner_radius))
// Change popup color (default: white)
.withPopupColor(Color.LTGRAY)
// Popup background alpha value between 0 (full transparent) and 255 (full opaque) (default: 230)
.withPopupAlphaValue(255)
// Item text provider / string array (default: no texts)
.withReactionTexts(position -> descriptions[position])
.withReactionTexts(R.array.descriptions)
Expand All @@ -161,15 +161,6 @@ Java:
.withTextVerticalPadding(0)
// Text size (default: 8sp)
.withTextSize(getResources().getDimension(R.dimen.text_size))
// Applying the popup rounded corners radius in density pixels
.withCornerSizeInDp(8)
/*
* Applying popup background alpha value
* Value must be in between 0 to 255 where
* 0 - Transparent (100% Transparency)
* 255 - Opaque (0% Transparency)
*/
.withPopupAlphaValue(255)
```

# Credits
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.1")
classpath("com.android.tools.build:gradle:4.1.0")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10")
classpath("com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5")
}
Expand Down
25 changes: 6 additions & 19 deletions library/src/main/java/com/github/pgreze/reactions/RoundedView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.Log
import android.util.TypedValue
import android.view.View

/**
Expand All @@ -15,33 +14,21 @@ import android.view.View
@SuppressLint("ViewConstructor")
class RoundedView(context: Context, private val config: ReactionsConfig) : View(context) {

private val tag = RoundedView::class.java.simpleName

private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = config.popupColor
style = Paint.Style.FILL
alpha = config.popupAlphaValue
}

private var offset = 0f

override fun onSizeChanged(w: Int, h: Int, oldW: Int, oldH: Int) {
Log.d(tag, "onSizeChanged: w = $w; h = $h; oldW = $oldW; oldH = $oldH")
offset = 0f
Log.d(tag, "onSizeChanged: padding left = " + paddingLeft + "; padding right = " + paddingRight +
"; padding top = " + paddingTop + "; padding bottom = " + paddingBottom)
Log.d(tag, "onSizeChanged: xStart = " + (x + offset) + "; cornerSize = " + config.cornerSizeInDp + "; x = " + x)
}

private val rect = RectF()

override fun onDraw(canvas: Canvas) {
// Draw the background rounded rectangle
rect.left = offset
rect.right = width.toFloat() - offset
rect.top = offset
rect.bottom = height.toFloat() - offset
val cornerSizeInPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, config.cornerSizeInDp.toFloat(), context.resources.displayMetrics)
canvas.drawRoundRect(rect, cornerSizeInPx, cornerSizeInPx, paint)
rect.left = 0f
rect.right = width.toFloat()
rect.top = 0f
rect.bottom = height.toFloat()
val popupCornerRadius = config.popupCornerRadius.toFloat()
canvas.drawRoundRect(rect, popupCornerRadius, popupCornerRadius, paint)
}
}
41 changes: 22 additions & 19 deletions library/src/main/java/com/github/pgreze/reactions/model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.github.pgreze.reactions
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.annotation.ArrayRes
import androidx.annotation.ColorInt
import androidx.annotation.IntRange
import androidx.annotation.Px
import androidx.core.content.ContextCompat
import android.widget.ImageView
import kotlin.math.roundToInt

/**
Expand All @@ -34,19 +35,21 @@ data class ReactionsConfig(
@Px val reactionSize: Int,
@Px val horizontalMargin: Int,
@Px val verticalMargin: Int,

/** Horizontal gravity compare to parent view or screen */
val popupGravity: PopupGravity,
/** Margin between dialog and screen border used by [PopupGravity] screen related values. */
val popupMargin: Int,
val popupCornerRadius: Int,
@ColorInt val popupColor: Int,
@IntRange(from = 0, to = 255) val popupAlphaValue: Int,

val reactionTextProvider: ReactionTextProvider,
val textBackground: Drawable,
@ColorInt val textColor: Int,
val textHorizontalPadding: Int,
val textVerticalPadding: Int,
val textSize: Float,
val popupAlphaValue: Int,
val cornerSizeInDp: Number
val textSize: Float
)

private val NO_TEXT_PROVIDER: ReactionTextProvider = { _ -> null }
Expand Down Expand Up @@ -91,9 +94,13 @@ class ReactionsConfigBuilder(val context: Context) {

var popupMargin: Int = horizontalMargin

var popupCornerRadius: Int = 90

@ColorInt
var popupColor: Int = Color.WHITE

var popupAlpha: Int = 230

var reactionTextProvider: ReactionTextProvider = NO_TEXT_PROVIDER

var reactionTexts: Int
Expand All @@ -111,10 +118,6 @@ class ReactionsConfigBuilder(val context: Context) {

var textSize: Float = 0f

var popupAlphaValue: Int = 230

var cornerSizeInDp: Number = 8

// Builder pattern for Java

fun withReactions(reactions: Collection<Reaction>) = this.also {
Expand Down Expand Up @@ -157,10 +160,18 @@ class ReactionsConfigBuilder(val context: Context) {
this.popupMargin = popupMargin
}

fun withPopupCornerRadius(popupCornerRadius: Int) = this.also {
this.popupCornerRadius = popupCornerRadius
}

fun withPopupColor(@ColorInt popupColor: Int) = this.also {
this.popupColor = popupColor
}

fun withPopupAlpha(@IntRange(from = 0, to = 255) popupAlpha: Int) = this.also {
this.popupAlpha = popupAlpha
}

fun withTextBackground(textBackground: Drawable) = this.also {
this.textBackground = textBackground
}
Expand All @@ -181,20 +192,14 @@ class ReactionsConfigBuilder(val context: Context) {
this.textSize = textSize
}

fun withPopupAlphaValue(popupAlphaValue: Int) = this.also {
this.popupAlphaValue = popupAlphaValue
}

fun withCornerSizeInDp(cornerSizeInDp: Number) = this.also {
this.cornerSizeInDp = cornerSizeInDp
}

fun build() = ReactionsConfig(
reactions = reactions.takeIf { it.isNotEmpty() }
?: throw IllegalArgumentException("Empty reactions"),
popupGravity = popupGravity,
popupMargin = popupMargin,
popupCornerRadius = popupCornerRadius,
popupColor = popupColor,
popupAlphaValue = popupAlpha,
reactionSize = reactionSize,
horizontalMargin = horizontalMargin,
verticalMargin = verticalMargin,
Expand All @@ -207,8 +212,6 @@ class ReactionsConfigBuilder(val context: Context) {
textVerticalPadding = textVerticalPadding.takeIf { it != 0 }
?: context.resources.getDimension(R.dimen.reactions_text_vertical_padding).roundToInt(),
textSize = textSize.takeIf { it != 0f }
?: context.resources.getDimension(R.dimen.reactions_text_size),
popupAlphaValue = popupAlphaValue,
cornerSizeInDp = cornerSizeInDp
?: context.resources.getDimension(R.dimen.reactions_text_size)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import com.github.pgreze.reactions.dsl.reactionConfig
import com.github.pgreze.reactions.dsl.reactionPopup
import com.github.pgreze.reactions.dsl.reactions

fun MainActivity.setup() {
val size = resources.getDimensionPixelSize(R.dimen.crypto_item_size)
val margin = resources.getDimensionPixelSize(R.dimen.crypto_item_margin)

fun MainActivity.setupTopRight() {
// Popup DSL + listener via function
val popup1 = reactionPopup(this, ::onReactionSelected) {
reactions {
Expand All @@ -34,20 +31,22 @@ fun MainActivity.setup() {
drawable { drawable(R.drawable.ic_crypto_zec) }
}
reactionTexts = R.array.crypto_symbols
popupCornerRadius = 40
popupColor = Color.LTGRAY
reactionSize = size
horizontalMargin = margin
popupAlpha = 255
reactionSize = resources.getDimensionPixelSize(R.dimen.crypto_item_size)
horizontalMargin = resources.getDimensionPixelSize(R.dimen.crypto_item_margin)
verticalMargin = horizontalMargin / 2
popupAlphaValue = 255
cornerSizeInDp = 40
}
// Setter also available
popup1.reactionSelectedListener = { position ->
toast("$position selected")
true
}
findViewById<View>(R.id.top_right_btn).setOnTouchListener(popup1)
}

fun MainActivity.setupRight() {
// Config DSL + listener in popup constructor
val config = reactionConfig(this) {
reactionsIds = intArrayOf(
Expand All @@ -70,7 +69,7 @@ fun MainActivity.setup() {
textHorizontalPadding = 0
textVerticalPadding = 0
textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14f, resources.displayMetrics)
popupAlphaValue = 255
popupAlpha = 255
}
val popup2 = ReactionPopup(this, config) { position -> true.also {
toast("$position selected")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sample1();
sample2();
sample3();

KotlinSamplesKt.setup(this);
sampleCenterLeft();
sampleTopLeft();
sampleBottomLeft();
KotlinSamplesKt.setupTopRight(this);
KotlinSamplesKt.setupRight(this);
}

private void sample1() {
private void sampleCenterLeft() {
ReactionPopup popup = new ReactionPopup(
this,
new ReactionsConfigBuilder(this)
Expand All @@ -45,7 +45,7 @@ private void sample1() {
findViewById(R.id.facebook_btn).setOnTouchListener(popup);
}

private void sample2() {
private void sampleTopLeft() {
ReactionPopup popup = new ReactionPopup(
this,
new ReactionsConfigBuilder(this)
Expand All @@ -54,6 +54,7 @@ private void sample2() {
R.drawable.ic_fb_love,
R.drawable.ic_fb_laugh,
})
.withPopupAlpha(20)
.withReactionTexts(position -> strings[position])
.withTextBackground(new ColorDrawable(Color.TRANSPARENT))
.withTextColor(Color.BLACK)
Expand All @@ -66,7 +67,7 @@ private void sample2() {
findViewById(R.id.top_btn).setOnTouchListener(popup);
}

private void sample3() {
private void sampleBottomLeft() {
int margin = getResources().getDimensionPixelSize(R.dimen.crypto_item_margin);

ReactionPopup popup = new ReactionPopup(this, new ReactionsConfigBuilder(this)
Expand Down Expand Up @@ -98,6 +99,6 @@ private void sample3() {
return position != 3;
});

findViewById(R.id.crypto_btn).setOnTouchListener(popup);
findViewById(R.id.crypto_bottom_left).setOnTouchListener(popup);
}
}
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
android:text="Right" />

<Button
android:id="@+id/crypto_btn"
android:id="@+id/crypto_bottom_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
Expand Down

0 comments on commit 33d5d13

Please sign in to comment.