diff --git a/app/src/main/java/com/doubtless/doubtless/screens/main/MainActivity.kt b/app/src/main/java/com/doubtless/doubtless/screens/main/MainActivity.kt index f1d70a8..c506136 100644 --- a/app/src/main/java/com/doubtless/doubtless/screens/main/MainActivity.kt +++ b/app/src/main/java/com/doubtless/doubtless/screens/main/MainActivity.kt @@ -1,14 +1,19 @@ package com.doubtless.doubtless.screens.main import android.os.Bundle +import android.os.Handler +import android.os.Looper import androidx.appcompat.app.AppCompatActivity import com.doubtless.doubtless.R import com.doubtless.doubtless.databinding.ActivityMainBinding import com.doubtless.doubtless.navigation.BackPressDispatcher import com.doubtless.doubtless.navigation.OnBackPressListener +import com.google.android.material.snackbar.Snackbar + class MainActivity : AppCompatActivity(), BackPressDispatcher { + private var doubleBackToExitPressedOnce: Boolean = false private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { @@ -52,8 +57,26 @@ class MainActivity : AppCompatActivity(), BackPressDispatcher { } } - if (!backPressConsumed) - super.onBackPressed() + if (!backPressConsumed) { + when { + doubleBackToExitPressedOnce -> super.onBackPressed() + else -> { + this.doubleBackToExitPressedOnce = true + Snackbar.make( + binding.container, + R.string.press_again_to_exit, + Snackbar.LENGTH_SHORT + ).show() + Handler(Looper.getMainLooper()).postDelayed({ + doubleBackToExitPressedOnce = false + }, BACKPRESS_DELAY) + } + } + } + } + + companion object { + private const val BACKPRESS_DELAY = 2000L } } \ No newline at end of file diff --git a/app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt b/app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt index 1c9064a..b8e336f 100644 --- a/app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt +++ b/app/src/main/java/com/doubtless/doubtless/utils/Extensions.kt @@ -4,7 +4,9 @@ import android.animation.AnimatorInflater import android.content.Context import android.view.View import android.view.inputmethod.InputMethodManager +import android.widget.Toast import androidx.annotation.AnimatorRes +import androidx.annotation.StringRes fun View.addStateListAnimation(@AnimatorRes animation: Int) { this.stateListAnimator = AnimatorInflater.loadStateListAnimator( @@ -16,4 +18,20 @@ fun View.addStateListAnimation(@AnimatorRes animation: Int) { fun View.hideSoftKeyboard() { val imm = this.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager imm?.hideSoftInputFromWindow(this.windowToken, 0) +} + +fun Context.shortToast(@StringRes msg: Int) { + Toast.makeText( + this, + msg, + Toast.LENGTH_SHORT + ).show() +} + +fun Context.longToast(@StringRes msg: Int) { + Toast.makeText( + this, + msg, + Toast.LENGTH_LONG + ).show() } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 181b759..a61bbc8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,4 +20,5 @@ Placements Please select at max 3 tags Please sign in again! + Press again to exit \ No newline at end of file