Skip to content

Commit 97dfa95

Browse files
committed
Refactoring
1 parent 1fd7ad5 commit 97dfa95

File tree

6 files changed

+125
-45
lines changed

6 files changed

+125
-45
lines changed

app/src/main/java/com/maliotis/ioslikealert/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.maliotis.ioslikealert
22

33
import android.app.Dialog
4+
import android.graphics.Color
5+
import android.graphics.Typeface
46
import androidx.appcompat.app.AppCompatActivity
57
import android.os.Bundle
68
import android.util.Log
@@ -28,7 +30,7 @@ class MainActivity : AppCompatActivity() {
2830

2931
})
3032
.negativeText("Done")
31-
.transparency(0.0f)
33+
.cornerRadius(10f)
3234
.isCancellable(false)
3335
.buildAndShow()
3436

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<color name="colorPrimary">#6200EE</color>
44
<color name="colorPrimaryDark">#3700B3</color>
55
<color name="colorAccent">#03DAC5</color>
6+
<color name="name11">#38D3D3D3</color>
67
</resources>

iosalert/src/main/java/com/maliotis/iosalert/IOSAlert.kt

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package com.maliotis.iosalert
22

3+
import android.animation.ValueAnimator
34
import android.app.Activity
4-
import android.app.Dialog
5-
import android.content.res.Resources
65
import android.graphics.Color
76
import android.graphics.Typeface
87
import android.graphics.drawable.ColorDrawable
98
import android.graphics.drawable.GradientDrawable
109
import android.os.Bundle
11-
import android.util.Log
12-
import android.util.TypedValue
13-
import android.view.LayoutInflater
14-
import android.view.View
15-
import android.view.ViewGroup
10+
import android.view.*
1611
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
17-
import android.view.ViewOutlineProvider
1812
import android.widget.Button
1913
import android.widget.LinearLayout
2014
import android.widget.TextView
2115
import androidx.annotation.FloatRange
2216
import androidx.annotation.IntRange
2317
import androidx.appcompat.app.AppCompatActivity
24-
import androidx.core.graphics.toColor
2518
import androidx.fragment.app.DialogFragment
2619
import com.maliotis.iosalert.blurview.BlurView
2720
import com.maliotis.iosalert.blurview.RenderScriptBlur
@@ -42,7 +35,11 @@ class IOSAlert private constructor(private val activity1: Activity,
4235
var iosNegativeClickListener: IOSClickListener?): DialogFragment() {
4336

4437
var blurRadius: Float = 25f
45-
var backgroundColor: Int? = null
38+
var backgroundColor: Int = 0xDFFFFFFF.toInt()
39+
var tintButtons: Boolean = true
40+
var tintButtonsColor: Int = 0
41+
var cornerRadius: Float = 10f
42+
4643

4744
/**
4845
* Builder class to build a dialog
@@ -54,8 +51,11 @@ class IOSAlert private constructor(private val activity1: Activity,
5451
var typeface: Typeface? = null
5552
var positiveText: String? = null
5653
var negativeText: String? = null
57-
var backgroundColor: Int? = null
54+
var backgroundColor: Int = 0xDFFFFFFF.toInt()
5855
var isCancellable: Boolean = true
56+
var tintButtons: Boolean = true
57+
var tintButtonsColor: Int = 0x38D3D3D3
58+
var cornerRadius: Float = 10f
5959
var iosPositiveClickListener: IOSClickListener = object: IOSClickListener {}
6060
var iosNegativeClickListener: IOSClickListener? = null
6161
private var blurRadius: Float = 25f
@@ -107,6 +107,16 @@ class IOSAlert private constructor(private val activity1: Activity,
107107
*/
108108
fun backgroundColor(color: Int) = apply { backgroundColor = color }
109109

110+
/**
111+
* Set tintButtons
112+
*/
113+
fun tintButtons(tintButtons: Boolean) = apply { this.tintButtons = tintButtons }
114+
115+
/**
116+
* Set tintButtonsColor
117+
*/
118+
fun tintButtonsColor(tintButtonsColor: Int) = apply { this.tintButtonsColor = tintButtonsColor }
119+
110120
/**
111121
* Sets the fragments isCancellable attribute to true or false
112122
* [DialogFragment.isCancelable]
@@ -121,6 +131,8 @@ class IOSAlert private constructor(private val activity1: Activity,
121131
blurRadius = radius
122132
}
123133

134+
fun cornerRadius(cornerRadius: Float) = apply { this.cornerRadius = cornerRadius }
135+
124136
/**
125137
* Will build and return the iOSAlert instance with the parameters specified in the Builder
126138
*/
@@ -129,6 +141,9 @@ class IOSAlert private constructor(private val activity1: Activity,
129141
iosAlert!!.blurRadius = blurRadius
130142
iosAlert!!.backgroundColor = backgroundColor
131143
iosAlert!!.isCancelable = isCancellable
144+
iosAlert!!.tintButtons = tintButtons
145+
iosAlert!!.tintButtonsColor = tintButtonsColor
146+
iosAlert!!.cornerRadius = cornerRadius
132147
return iosAlert!!
133148
}
134149

@@ -151,7 +166,7 @@ class IOSAlert private constructor(private val activity1: Activity,
151166
val blurView: BlurView = v.findViewById(R.id.blurView)
152167
val titleTextView: TextView = v.findViewById(R.id.popUpTitle)
153168
val bodyTextView: TextView = v.findViewById(R.id.popUpBody)
154-
val okButton: Button = v.findViewById(R.id.popUpOkButton)
169+
val positiveButton: Button = v.findViewById(R.id.popUpOkButton)
155170
var negativeButton: Button? = null
156171

157172
if (body.isNullOrEmpty()) blurView.findViewById<LinearLayout>(R.id.layoutInsideBlurView).removeView(bodyTextView)
@@ -161,7 +176,7 @@ class IOSAlert private constructor(private val activity1: Activity,
161176
titleTextView.text = title
162177
positiveText = positiveText ?: "OK"
163178

164-
val positiveClickListener = getOkButtonClickListener()
179+
val positiveClickListener = getPositiveButtonClickListener()
165180
if (negativeText != null || iosNegativeClickListener != null) {
166181
// Add a negative button
167182
negativeButton = getNegativeButton()
@@ -170,8 +185,11 @@ class IOSAlert private constructor(private val activity1: Activity,
170185
linearLayout.addView(negativeButton, 0)
171186

172187
}
173-
okButton.setOnClickListener(positiveClickListener)
188+
positiveButton.setOnClickListener(positiveClickListener)
189+
if (tintButtons)
190+
positiveButton.setOnTouchListener(getOnTouchTintListener())
174191
// set typeface
192+
setTypefaceSettings(titleTextView, bodyTextView, positiveButton, negativeButton)
175193

176194
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
177195

@@ -181,12 +199,12 @@ class IOSAlert private constructor(private val activity1: Activity,
181199
val rootView = decorView.findViewById<View>(android.R.id.content) as ViewGroup
182200
val windowBackground = decorView.background
183201

184-
if (backgroundColor != null) {
185-
val backgroundDrawable = GradientDrawable()
186-
backgroundDrawable.cornerRadius = dpToPixel(10f)
187-
backgroundDrawable.setColor(backgroundColor!!)
188-
blurView.background = backgroundDrawable
189-
}
202+
203+
val backgroundDrawable = GradientDrawable()
204+
backgroundDrawable.cornerRadius = dpToPixel(cornerRadius, resources)
205+
backgroundDrawable.setColor(backgroundColor)
206+
blurView.background = backgroundDrawable
207+
190208

191209
blurView.setupWith(rootView)
192210
.setFrameClearDrawable(windowBackground)
@@ -200,7 +218,20 @@ class IOSAlert private constructor(private val activity1: Activity,
200218
return v
201219
}
202220

203-
private fun getOkButtonClickListener(): View.OnClickListener {
221+
/**
222+
* Check if the view exists and if it is of type TextView or Subclass of that assign the typeface
223+
*/
224+
private fun setTypefaceSettings(vararg views: View?) {
225+
for (v in views) {
226+
if (v != null) {
227+
if (v is TextView) {
228+
v.typeface = typeface
229+
}
230+
}
231+
}
232+
}
233+
234+
private fun getPositiveButtonClickListener(): View.OnClickListener {
204235
return View.OnClickListener {
205236
iosPositiveClickListener.onClick(dialog)
206237
}
@@ -214,41 +245,59 @@ class IOSAlert private constructor(private val activity1: Activity,
214245
}
215246
}
216247

248+
private fun getOnTouchTintListener(): View.OnTouchListener {
249+
return View.OnTouchListener { v, event ->
250+
if (event.action == MotionEvent.ACTION_DOWN) {
251+
val background = ColorDrawable()
252+
val anim = ValueAnimator.ofArgb(Color.TRANSPARENT, tintButtonsColor)
253+
anim.addUpdateListener {
254+
val value = it.animatedValue as Int
255+
background.color = value
256+
v.background = background
257+
258+
}
259+
anim.duration = 150
260+
anim.start()
261+
262+
} else if (event.action == MotionEvent.ACTION_UP) {
263+
264+
val background = ColorDrawable()
265+
val anim = ValueAnimator.ofArgb(tintButtonsColor, Color.TRANSPARENT)
266+
anim.addUpdateListener {
267+
val value = it.animatedValue as Int
268+
background.color = value
269+
v.background = background
270+
271+
}
272+
anim.duration = 150
273+
anim.start()
274+
}
275+
276+
false
277+
}
278+
}
279+
217280
private fun getNegativeButton(): Button {
218281
return Button(activity1).apply {
219282
setTextColor(Color.RED)
220283
text = negativeText ?: "CANCEL"
221284
if (this@IOSAlert.typeface != null)
222285
typeface = this@IOSAlert.typeface
223286

224-
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, dpToPixel(44f).toInt(), 0.5f)
225-
setBackgroundColor(Color.TRANSPARENT)
287+
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT, 0.5f)
288+
background = ColorDrawable(Color.TRANSPARENT)
226289
setOnClickListener(getNegativeButtonClickListener())
290+
if (tintButtons)
291+
setOnTouchListener(getOnTouchTintListener())
227292
}
228293
}
229294

230295
private fun getLineSeparator(): View {
231296
return View(activity1).apply {
232-
layoutParams = LinearLayout.LayoutParams(dpToPixel(0.5f).toInt(), MATCH_PARENT)
297+
layoutParams = LinearLayout.LayoutParams(dpToPixel(0.5f, resources).toInt(), MATCH_PARENT)
233298
setBackgroundColor(Color.parseColor("#9FAAAAAA"))
234299

235300
}
236301
}
237302

238-
private fun dpToPixel(dp: Float): Float {
239-
val r: Resources = resources
240-
return TypedValue.applyDimension(
241-
TypedValue.COMPLEX_UNIT_DIP,
242-
dp,
243-
r.displayMetrics
244-
)
245-
246-
}
247-
248-
}
249-
250-
interface IOSClickListener {
251-
fun onClick(dialog: Dialog?) {
252-
dialog?.dismiss()
253-
}
254303
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.maliotis.iosalert
2+
3+
import android.app.Dialog
4+
5+
/**
6+
* Created by petrosmaliotis on 27/05/2020.
7+
*/
8+
interface IOSClickListener {
9+
fun onClick(dialog: Dialog?) {
10+
dialog?.dismiss()
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.maliotis.iosalert
2+
3+
import android.content.res.Resources
4+
import android.util.TypedValue
5+
6+
/**
7+
* Created by petrosmaliotis on 27/05/2020.
8+
*/
9+
10+
fun dpToPixel(dp: Float, resources: Resources): Float {
11+
val r: Resources = resources
12+
return TypedValue.applyDimension(
13+
TypedValue.COMPLEX_UNIT_DIP,
14+
dp,
15+
r.displayMetrics
16+
)
17+
18+
}

iosalert/src/main/res/layout/popup_layout.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,17 @@
5656
<LinearLayout
5757
android:id="@+id/linear_layout"
5858
android:layout_width="match_parent"
59-
android:layout_height="wrap_content">
59+
android:layout_height="44dp">
6060

6161

6262

6363
<Button
6464
android:id="@+id/popUpOkButton"
6565
android:layout_width="match_parent"
66-
android:layout_height="44dp"
66+
android:layout_height="match_parent"
6767
android:layout_weight="0.5"
6868
android:background="@android:color/transparent"
69-
android:foreground="?android:attr/selectableItemBackground"
7069
android:text="ok"
71-
7270
android:textColor="#2196F3"
7371
android:textSize="18sp" />
7472

0 commit comments

Comments
 (0)