Skip to content

Commit

Permalink
Merge pull request #4 from mncinnovation/dev
Browse files Browse the repository at this point in the history
Extension, alert dialog, snackbar
  • Loading branch information
bayuwijdev committed Jul 21, 2021
2 parents d68cd8f + 81b927a commit 0932c5e
Show file tree
Hide file tree
Showing 14 changed files with 488 additions and 46 deletions.
16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions app/src/main/java/com/mncgroup/mnccore/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.mncgroup.mnccore.ui

import android.app.Activity
import android.os.Bundle
import android.widget.Toast
import com.mncgroup.core.util.ext.*
import com.mncgroup.mnccore.databinding.ActivityMainBinding

class MainActivity : Activity() {
Expand All @@ -12,5 +14,39 @@ class MainActivity : Activity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

with(binding){
btnShowSnackbar.setOnClickListener {
showSnackbar(it,"Ini snackbar"){
Toast.makeText(this@MainActivity, "closed snackbar", Toast.LENGTH_SHORT).show()
}
}
btnOpenAppSetting.setOnClickListener {
startActivity(Intents.createOpenAppSettingsIntent(this@MainActivity))
}
btnOpenLocationSetting.setOnClickListener {
startActivity(Intents.createOpenLocationSettings())
}
btnShowAppCompatAlertAction.setOnClickListener {
showAppCompatAlert("This is a message"){
Toast.makeText(this@MainActivity, "Test", Toast.LENGTH_SHORT).show()
}
}
btnShowInputName.setOnClickListener {
showAppCompatAlertInputAction("Please to Input Fullname","Input","Kirim","Kembali","Fullname",true) { input ->
Toast.makeText(this@MainActivity, "Namanya adalah : $input", Toast.LENGTH_SHORT).show()
}
}

btnShowDatePicker.setOnClickListener {

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
showDatePickerAction(null,null) { day, month, year ->
Toast.makeText(this@MainActivity, "selected date is " + day +
" / " + month +
" / " + year, Toast.LENGTH_SHORT).show();
}.show()
}
}
}
}
}
96 changes: 89 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent"
android:background="@color/white">

<TextView
android:id="@+id/tvMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Main"
/>
</RelativeLayout>
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_large"
android:text="Main" />
<Button
android:id="@+id/btnShowSnackbar"
style="@style/TextAppearance.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvMain"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_large"
android:padding="@dimen/padding_large"
android:text="Show snackbar"
android:textSize="@dimen/body2_font_size"
android:textStyle="bold" />

<Button
android:id="@+id/btnOpenAppSetting"
style="@style/TextAppearance.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvMain"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_large"
android:padding="@dimen/padding_large"
android:text="Open App Setting"
android:textSize="@dimen/body2_font_size"
android:textStyle="bold" />

<Button
android:id="@+id/btnOpenLocationSetting"
style="@style/TextAppearance.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnOpenAppSetting"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_large"
android:padding="@dimen/padding_large"
android:text="Open Location Setting"
android:textSize="@dimen/body2_font_size"
android:textStyle="bold" />

<Button
android:id="@+id/btnShowAppCompatAlertAction"
style="@style/TextAppearance.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnOpenLocationSetting"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_large"
android:padding="@dimen/padding_large"
android:text="Show Alert"
android:textSize="@dimen/body2_font_size"
android:textStyle="bold" />

<Button
android:id="@+id/btnShowInputName"
style="@style/TextAppearance.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnShowAppCompatAlertAction"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_large"
android:padding="@dimen/padding_large"
android:text="Show Dialog Input Name"
android:textSize="@dimen/body2_font_size"
android:textStyle="bold" />

<Button
android:id="@+id/btnShowDatePicker"
style="@style/TextAppearance.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnShowInputName"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_large"
android:padding="@dimen/padding_large"
android:text="Show Date Picker"
android:textSize="@dimen/body2_font_size"
android:textStyle="bold" />


</LinearLayout>
137 changes: 137 additions & 0 deletions core/src/main/java/com/mncgroup/core/util/ext/AlertDialogs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.mncgroup.core.util.ext

import android.app.DatePickerDialog
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.google.android.material.textfield.TextInputLayout
import com.mncgroup.core.R

fun Context.showAppCompatAlert(
messageToShow: String,
title: String? = getString(R.string.label_attention)
): AlertDialog = showAppCompatAlert(messageToShow, title) {}

fun Context.showAppCompatAlert(
messageToShow: String,
title: String? = getString(R.string.label_attention),
actionTitle: String? = getString(R.string.ok),
actionListener: () -> Unit
): AlertDialog = this.let { context ->
val builder = AlertDialog.Builder(context)
builder.apply {
setMessage(messageToShow)
setTitle(title)
setPositiveButton(actionTitle) { dialogInterface, i ->
dialogInterface.dismiss()
actionListener()
}
}
builder.create()
builder.show()
}

fun Context.showAppCompatAlertAction(
messageToShow: String,
title: String? = getString(R.string.label_attention),
actionTitle: String? = getString(R.string.ok),
dismissTitle: String? = getString(R.string.cancel),
actionListener: () -> Unit
): AlertDialog =
showAppCompatAlertAction(messageToShow, title, actionTitle, dismissTitle, actionListener) {}

fun Context.showAppCompatAlertAction(
messageToShow: String,
title: String? = getString(R.string.label_attention),
actionTitle: String? = getString(R.string.ok),
dismissTitle: String? = getString(R.string.cancel),
actionListener: () -> Unit, dismissListener: () -> Unit
): AlertDialog = this.let { context ->
val builder = AlertDialog.Builder(context)
builder.apply {
setMessage(messageToShow)
setTitle(title)
setPositiveButton(actionTitle) { dialogInterface, i ->
dialogInterface.dismiss()
actionListener()
}

setNegativeButton(dismissTitle) { dialogInterface, i ->
dialogInterface.dismiss()
dismissListener()
}
}
builder.create()
builder.show().apply {
getButton(DialogInterface.BUTTON_NEGATIVE)?.setTextColor(
ContextCompat.getColor(
context,
R.color.dust
)
)
}
}

fun Context.showAppCompatAlertInputAction(
messageToShow: String? = null,
title: String? = null,
actionTitle: String? = getString(R.string.title_submit),
negativeTitle: String? = getString(R.string.cancel),
textHint: String? = null, cancelable: Boolean = false,
actionListener: (input: String) -> Unit
): AlertDialog = showAppCompatAlertInputAction(messageToShow, title, actionTitle, negativeTitle, textHint, cancelable, actionListener, {})

fun Context.showAppCompatAlertInputAction(
messageToShow: String? = null,
title: String? = null,
actionTitle: String? = getString(R.string.title_submit),
negativeTitle: String? = getString(R.string.cancel),
textHint: String? = null, cancelable: Boolean = false,
actionListener: (input: String) -> Unit,
dismissListener: () -> Unit = {}
): AlertDialog = this.let { context ->
val builder = AlertDialog.Builder(context)
builder.apply {
if (title != null) {
this.setTitle(title)
}
if (messageToShow != null) {
this.setMessage(messageToShow)
}
val layoutInflater =
getSystemService(Context.LAYOUT_INFLATER_SERVICE) as android.view.LayoutInflater
val textInput = layoutInflater.inflate(R.layout.dialog_input, null) as TextInputLayout
textInput.hint = textHint

builder.setView(textInput)

setCancelable(cancelable)

negativeTitle?.let {
setNegativeButton(it) { p0, p1 ->
p0.dismiss()
dismissListener()
}
}

setPositiveButton(actionTitle) { p0, p1 ->
p0.dismiss()
actionListener(textInput.editText?.text.toString())
}

setOnCancelListener {
it.dismiss()
dismissListener()
}
}.create()

builder.show().apply {
getButton(DialogInterface.BUTTON_NEGATIVE)?.setTextColor(
ContextCompat.getColor(
context,
R.color.dust
)
)
}
}
39 changes: 34 additions & 5 deletions core/src/main/java/com/mncgroup/core/util/ext/DateUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

package com.mncgroup.core.util.ext

import android.app.DatePickerDialog
import android.content.Context
import android.os.Build
import android.widget.DatePicker
import androidx.annotation.RequiresApi
import androidx.annotation.StyleRes
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
Expand Down Expand Up @@ -31,12 +37,13 @@ fun Calendar.getMinutes() = this[Calendar.MINUTE]
fun Calendar.getSeconds() = this[Calendar.SECOND]
fun Calendar.getMilliseconds() = this[Calendar.MILLISECOND]

fun DateFormat.parseIndonesianCalendar(dateString: String): Calendar = Calendar.getInstance(indonesianLocale).apply {
val parsedDate = this@parseIndonesianCalendar.parse(dateString)
if (parsedDate != null) {
time = parsedDate
fun DateFormat.parseIndonesianCalendar(dateString: String): Calendar =
Calendar.getInstance(indonesianLocale).apply {
val parsedDate = this@parseIndonesianCalendar.parse(dateString)
if (parsedDate != null) {
time = parsedDate
}
}
}

fun Date.toSuccessString(): String = successDateFormat.format(this)

Expand All @@ -49,3 +56,25 @@ fun Int.secondToMinuteSecondString(): String {
val second = this % 60
return "${minute.asMinutesString()}:${second.asMinutesString()}"
}

@RequiresApi(Build.VERSION_CODES.N)
fun Context.showDatePickerAction(
@StyleRes style: Int? = null,
initYear: Int? = null,
actionListener: (day: Int, month: Int, year: Int) -> Unit
): DatePickerDialog = this.let {
val currentTime = Calendar.getInstance()
val year = initYear ?: currentTime.get(Calendar.YEAR)
val month = currentTime.get(Calendar.MONTH)
val day = currentTime.get(Calendar.DAY_OF_MONTH)

if (style != null) DatePickerDialog(
this, style, { _, year, month, dayOfMonth ->
actionListener(dayOfMonth, month + 1, year)
}, year, month, day
) else DatePickerDialog(
this, { _, year, month, dayOfMonth ->
actionListener(dayOfMonth, month + 1, year)
}, year, month, day
)
}
Loading

0 comments on commit 0932c5e

Please sign in to comment.