Skip to content

Commit

Permalink
- Fixed color problems
Browse files Browse the repository at this point in the history
- Simplified library
- Fixed image view size problem
- Deleted some functions for simplicity sake
- Cleaned code
  • Loading branch information
Ido-Barnea committed Apr 16, 2022
1 parent 3026b63 commit c339e92
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 182 deletions.
177 changes: 29 additions & 148 deletions Dialoger/src/main/java/com/barnea/dialoger/Dialoger.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.barnea.dialoger

import android.app.ActionBar
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.Log
import android.view.*
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat

class Dialoger(
Expand All @@ -21,25 +25,18 @@ class Dialoger(

companion object {
const val TYPE_MESSAGE = 0
const val TYPE_FAILURE = 1
const val TYPE_SUCCESS = 2
const val TYPE_LOADING = 3
const val TYPE_LOADING = 1

const val WIDTH_SMALL = 800
const val WIDTH_MEDIUM = 900
const val WIDTH_LARGE = 1000

private const val COLOR_SUCCESS = "#4BB543"
private const val COLOR_FAILURE = "#cc0000"
const val dialogWidth = 900
}

private val context = context_

private val dialog = AlertDialog.Builder(context).create()
@SuppressLint("InflateParams")
private var dialogView: View = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null)

private var buttonOnClickListener: (() -> Unit)? = null
private var dialogWidth = WIDTH_MEDIUM

init {
dialogSetup(dialogType)
Expand All @@ -50,14 +47,13 @@ class Dialoger(
* @return Dialoger object
*/
fun show(): Dialoger {
if (getDialogTitle().text.isNullOrBlank()) getDialogTitle().height = 0
if (getDialogText().text.isNullOrBlank()) getDialogText().height = 0
if (getDialogTitle().text.isNullOrBlank()) getDialogTitle().visibility = View.GONE
if (getDialogText().text.isNullOrBlank()) getDialogText().visibility = View.GONE
if (getDialogImageView().drawable == null) getDialogImageView().visibility = View.GONE

dialog.setView(dialogView) // set dialog view after editing the view
dialog.show() // show the dialog

setDialogWidth(dialogWidth)

return this
}

Expand Down Expand Up @@ -102,59 +98,18 @@ class Dialoger(
private fun setDialogComponentsAccordingToDialogType(dialogType: Int){
when (dialogType){
TYPE_MESSAGE -> {
setTitleColor(Color.BLACK)
setTextColor(Color.BLACK)
}

TYPE_SUCCESS -> {
setTitleColor(COLOR_SUCCESS)
setTextColor(COLOR_SUCCESS)
setButtonBackgroundColor(COLOR_SUCCESS)
setButtonTextColor(Color.WHITE)

getDialogTitle().text = context.resources.getString(R.string.success)
getDialogText().text = ""
}

TYPE_FAILURE -> {
setTitleColor(COLOR_FAILURE)
setTextColor(COLOR_FAILURE)
setButtonBackgroundColor(COLOR_FAILURE)
setButtonTextColor(Color.WHITE)

getDialogTitle().text = context.resources.getString(R.string.error_title)
getDialogText().text = context.resources.getString(R.string.error_text)
getDialogProgressBar().visibility = View.GONE
getDialogButton().visibility = View.VISIBLE
}

TYPE_LOADING -> {
setTitleColor(Color.BLACK)
setTextColor(Color.BLACK)
getDialogProgressBar().visibility = View.VISIBLE
getDialogButton().visibility = View.GONE
setCanceledOnTouchOutside(false)

getDialogTitle().text = context.resources.getString(R.string.loading)
getDialogText().text = ""
}
}
}

/**
* sets the width of the alert dialog popup
* @param width the width of the alert dialog popup
* @return Dialoger object
*/
fun setDialogWidth(width: Int): Dialoger {
this.dialogWidth = width

val lp = WindowManager.LayoutParams()
lp.copyFrom(dialog.window?.attributes)
lp.width = width
dialog.window?.attributes = lp

return this
}

/**
* sets buttonOnClickListener to the callback parameter
* @param callback the callback for the dialog onClickListener
Expand All @@ -176,7 +131,7 @@ class Dialoger(
/**
* @return Dialoger object
*/
fun setText(text: String): Dialoger {
fun setDescription(text: String): Dialoger {
getDialogText().text = text
return this
}
Expand All @@ -189,30 +144,12 @@ class Dialoger(
return this
}

/**
* @param color the chosen color hex code
* @return Dialoger object
*/
fun setBackgroundColor(color: String): Dialoger {
getDialogBackground().background.setTint(Color.parseColor(color))
return this
}

/**
* @param color the chosen color id
* @return Dialoger object
*/
fun setBackgroundColor(color: Int): Dialoger {
getDialogBackground().background.setTint(color)
return this
}

/**
* @param color the chosen color hex code
* @return Dialoger object
*/
fun setButtonBackgroundColor(color: String): Dialoger {
getDialogButton().backgroundTintList = ColorStateList.valueOf(Color.parseColor(color))
getDialogBackground().backgroundTintList = intToColorStateList(color)
return this
}

Expand All @@ -221,16 +158,7 @@ class Dialoger(
* @return Dialoger object
*/
fun setButtonBackgroundColor(color: Int): Dialoger {
getDialogButton().backgroundTintList = ColorStateList.valueOf(color)
return this
}

/**
* @param color the chosen color hex code
* @return Dialoger object
*/
fun setTitleColor(color: String): Dialoger {
getDialogTitle().setTextColor(Color.parseColor(color))
getDialogButton().backgroundTintList = intToColorStateList(color)
return this
}

Expand All @@ -239,34 +167,16 @@ class Dialoger(
* @return Dialoger object
*/
fun setTitleColor(color: Int): Dialoger {
getDialogTitle().setTextColor(color)
return this
}

/**
* @param color the chosen color hex code
* @return Dialoger object
*/
fun setTextColor(color: String): Dialoger {
getDialogText().setTextColor(Color.parseColor(color))
getDialogTitle().setTextColor(ContextCompat.getColor(context, color))
return this
}

/**
* @param color the chosen color id
* @return Dialoger object
*/
fun setTextColor(color: Int): Dialoger {
getDialogText().setTextColor(color)
return this
}

/**
* @param color the chosen color hex code
* @return Dialoger object
*/
fun setButtonTextColor(color: String): Dialoger {
getDialogButton().setTextColor(Color.parseColor(color))
fun setDescriptionColor(color: Int): Dialoger {
getDialogText().setTextColor(ContextCompat.getColor(context, color))
return this
}

Expand All @@ -275,25 +185,16 @@ class Dialoger(
* @return Dialoger object
*/
fun setButtonTextColor(color: Int): Dialoger {
getDialogButton().setTextColor(color)
return this
}

/**
* @param color the chosen color hex code
* @return Dialoger object
*/
fun setProgressBarColor(color: String): Dialoger {
getDialogProgressBar().indeterminateDrawable.setTint(Color.parseColor(color))
getDialogButton().setTextColor(ContextCompat.getColor(context, color))
return this
}

/**
* @param color the chosen color id
* @return Dialoger object
*/
fun setProgressBarColor(color: Int): Dialoger {
getDialogProgressBar().indeterminateDrawable.setTint(color)
fun setLoadingProgressBarColor(color: Int): Dialoger {
getDialogProgressBar().indeterminateDrawable.setTintList(intToColorStateList(color))
return this
}

Expand Down Expand Up @@ -325,40 +226,20 @@ class Dialoger(
}

/**
* Default gravity is center
* ProgressBar gravity stays as center, to change that use setProgressBarGravity
* @param gravity the gravity of all the children in the dialog
* @return Dialoger object
*/
fun setGravity(gravity: Int): Dialoger {
getDialogBackground().gravity = gravity
return this
}

/**
* Default gravity is center
* @param gravity the gravity of all the children in the dialog
* @param isCanceledOnTouchOutside whether the dialog will be dismissed on touch outside of it or not
* @return Dialoger object
*/
fun setProgressBarGravity(gravity: Int): Dialoger {
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
this.gravity = gravity
}

getDialogProgressBar().layoutParams = params
fun setCanceledOnTouchOutside(isCanceledOnTouchOutside: Boolean): Dialoger {
dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside)
return this
}

/**
* @param isCanceledOnTouchOutside whether the dialog will be dismissed on touch outside of it or not
* @return Dialoger object
* @param color the int color code
* @return the color state list of the color code
*/
fun setCanceledOnTouchOutside(isCanceledOnTouchOutside: Boolean): Dialoger {
dialog.setCanceledOnTouchOutside(isCanceledOnTouchOutside)
return this
private fun intToColorStateList(color: Int): ColorStateList {
return ContextCompat.getColorStateList(context, color)!!
}

/**
Expand Down
40 changes: 21 additions & 19 deletions Dialoger/src/main/res/layout/layout_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,59 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@drawable/rounded_background"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">

<ImageView
android:id="@+id/dialogImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_width="200dp"
android:layout_height="200dp"
android:contentDescription="@string/default_image_content_description"
android:maxHeight="100dp"
android:scaleType="centerCrop"
android:paddingBottom="16dp"
android:contentDescription="@string/default_image_content_description" />
android:scaleType="centerCrop" />

<TextView
android:id="@+id/dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_title"
android:textStyle="bold"
android:textSize="20sp"
android:textAlignment="inherit"
android:textColor="@color/black" />
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/dialogText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_text"
android:textAlignment="inherit"
android:layout_marginTop="8dp"
android:textSize="16sp"
android:textColor="@color/black" />
android:textAlignment="inherit"
android:textColor="@color/black"
android:textSize="16sp" />

<ProgressBar
android:id="@+id/dialogProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone"
android:layout_gravity="center"
android:layout_marginTop="24dp" />
android:layout_marginTop="24dp"
android:indeterminate="true"
android:visibility="gone" />

<Button
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/dialogButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:gravity="center"
android:padding="12dp"
android:background="@drawable/rounded_background"
android:backgroundTint="@color/black"
android:text="@string/default_button_text"
android:textColor="@color/white"
android:backgroundTint="@color/black" />
android:textColor="@color/white" />

</LinearLayout>
6 changes: 0 additions & 6 deletions Dialoger/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="default_title">Default Title</string>
<string name="default_text">Default text default text default text default text</string>
<string name="default_image_content_description">Dialog Image</string>
<string name="default_button_text">OK</string>
<string name="loading">Loading…</string>
<string name="success">Success!</string>
<string name="error_title">Error</string>
<string name="error_text">Oh no! It looks like something went wrong!</string>
</resources>
Loading

0 comments on commit c339e92

Please sign in to comment.