diff --git a/README.md b/README.md index 9353c1c..7b5f9a4 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 } ``` @@ -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) @@ -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 diff --git a/build.gradle.kts b/build.gradle.kts index d4dac0f..74ea495 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") } diff --git a/library/src/main/java/com/github/pgreze/reactions/RoundedView.kt b/library/src/main/java/com/github/pgreze/reactions/RoundedView.kt index d742b58..a0af7cf 100644 --- a/library/src/main/java/com/github/pgreze/reactions/RoundedView.kt +++ b/library/src/main/java/com/github/pgreze/reactions/RoundedView.kt @@ -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 /** @@ -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) } } diff --git a/library/src/main/java/com/github/pgreze/reactions/model.kt b/library/src/main/java/com/github/pgreze/reactions/model.kt index 96e1224..1afebfb 100644 --- a/library/src/main/java/com/github/pgreze/reactions/model.kt +++ b/library/src/main/java/com/github/pgreze/reactions/model.kt @@ -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 /** @@ -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 } @@ -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 @@ -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) = this.also { @@ -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 } @@ -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, @@ -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) ) } diff --git a/sample/src/main/java/com/github/pgreze/reactions/sample/KotlinSamples.kt b/sample/src/main/java/com/github/pgreze/reactions/sample/KotlinSamples.kt index 870bb6b..fdb2927 100644 --- a/sample/src/main/java/com/github/pgreze/reactions/sample/KotlinSamples.kt +++ b/sample/src/main/java/com/github/pgreze/reactions/sample/KotlinSamples.kt @@ -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 { @@ -34,12 +31,12 @@ 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 -> @@ -47,7 +44,9 @@ fun MainActivity.setup() { true } findViewById(R.id.top_right_btn).setOnTouchListener(popup1) +} +fun MainActivity.setupRight() { // Config DSL + listener in popup constructor val config = reactionConfig(this) { reactionsIds = intArrayOf( @@ -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") diff --git a/sample/src/main/java/com/github/pgreze/reactions/sample/MainActivity.java b/sample/src/main/java/com/github/pgreze/reactions/sample/MainActivity.java index 5a970d3..35148f5 100644 --- a/sample/src/main/java/com/github/pgreze/reactions/sample/MainActivity.java +++ b/sample/src/main/java/com/github/pgreze/reactions/sample/MainActivity.java @@ -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) @@ -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) @@ -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) @@ -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) @@ -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); } } diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 932d715..6cf8fef 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -55,7 +55,7 @@ android:text="Right" />