Skip to content

Commit

Permalink
[ADD/#22] initOnBackPressed 확장함수 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed May 26, 2024
1 parent 76e9ad4 commit 1d99498
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/src/main/java/kr/genti/core/extension/ActivityExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowInsets
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.core.content.ContextCompat

fun Activity.setStatusBarColorFromResource(colorResId: Int) {
Expand All @@ -32,3 +35,23 @@ fun Activity.getWindowHeight(): Int {
displayMetrics.heightPixels
}
}

fun ComponentActivity.initOnBackPressedListener(
view: View,
delay: Long = 2000L,
) {
var backPressedTime = 0L
val onBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (System.currentTimeMillis() - backPressedTime >= delay) {
backPressedTime = System.currentTimeMillis()
view.context.toast("버튼을 한번 더 누르면 종료됩니다.")
} else {
finish()
}
}
}

this.onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
}
21 changes: 21 additions & 0 deletions core/src/main/java/kr/genti/core/extension/FragmentExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.genti.core.extension

import android.view.View
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
Expand Down Expand Up @@ -44,6 +45,26 @@ fun Fragment.setStatusBarColor(colorResId: Int) {
}
}

fun Fragment.initOnBackPressedListener(
view: View,
delay: Long = 2000L,
) {
var backPressedTime = 0L
val onBackPressedCallback =
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (System.currentTimeMillis() - backPressedTime >= delay) {
backPressedTime = System.currentTimeMillis()
view.context.toast("버튼을 한번 더 누르면 종료됩니다.")
} else {
requireActivity().finish()
}
}
}

requireActivity().onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
}

val Fragment.viewLifeCycle
get() = viewLifecycleOwner.lifecycle

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.core.view.updatePadding
import dagger.hilt.android.AndroidEntryPoint
import kr.genti.core.base.BaseActivity
import kr.genti.core.extension.colorOf
import kr.genti.core.extension.initOnBackPressedListener
import kr.genti.core.extension.setOnSingleClickListener
import kr.genti.presentation.R
import kr.genti.presentation.databinding.ActivityLoginBinding
Expand All @@ -23,6 +24,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_login
super.onCreate(savedInstanceState)

initLoginBtnListener()
initOnBackPressedListener(binding.root)
setStatusBarTransparent()
setNavigationBarGreen()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class CreateViewModel
val script = MutableLiveData<String>()
var plusImage: Uri = Uri.EMPTY
val isWritten = MutableLiveData(false)

val selectedAngle = MutableLiveData<Int>(-1)
val selectedFrame = MutableLiveData<Int>(-1)
val isSelected = MutableLiveData(false)

var uriList = listOf<Uri>()
var isCompleted = MutableLiveData(false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.navigation.fragment.findNavController
import coil.load
import dagger.hilt.android.AndroidEntryPoint
import kr.genti.core.base.BaseFragment
import kr.genti.core.extension.initOnBackPressedListener
import kr.genti.core.extension.setOnSingleClickListener
import kr.genti.core.extension.stringOf
import kr.genti.presentation.R
Expand All @@ -36,6 +37,7 @@ class DefineFragment() : BaseFragment<FragmentDefineBinding>(R.layout.fragment_d

private fun initView() {
binding.vm = viewModel
initOnBackPressedListener(binding.root)
}

private fun initCreateBtnListener() {
Expand Down

0 comments on commit 1d99498

Please sign in to comment.