Part2 Ch3.2 비밀 다이어리(2)
MainActivity.kl
package fastcampus.aop.part2.chapter03
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.NumberPicker
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit
class MainActivity : AppCompatActivity() {
private val passwordNumberPicker1 : NumberPicker by lazy{
findViewById<NumberPicker>(R.id.passwordNumberPicker1).apply{
this.minValue = 0
this.maxValue = 9
}
}
private val passwordNumberPicker2 : NumberPicker by lazy{
findViewById<NumberPicker>(R.id.passwordNumberPicker2).apply{
this.minValue = 0
this.maxValue = 9
}
}
private val passwordNumberPicker3 : NumberPicker by lazy{
findViewById<NumberPicker>(R.id.passwordNumberPicker3).apply{
this.minValue = 0
this.maxValue = 9
}
}
private val openButton : Button by lazy{
findViewById(R.id.openButton)
}
private val changePasswordButton : Button by lazy{
findViewById(R.id.changePasswordButton)
}
private var changePasswordMode = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
passwordNumberPicker1
passwordNumberPicker2
passwordNumberPicker3
initOpenButton()
initChangePasswordButton()
}
private fun initOpenButton(){
openButton.setOnClickListener {
if(changePasswordMode){
Toast.makeText(this, "지금은 비밀번호 변경 중입니다!", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
if(ifPasswordMatch()){
}
else{
showPasswordFailAlertDialog()
}
}
}
private fun initChangePasswordButton(){
changePasswordButton.setOnClickListener {
if(changePasswordMode) {
val passwordFromUser = "${passwordNumberPicker1.value}${passwordNumberPicker2.value}${passwordNumberPicker3.value}"
val myPasswordPreferences = getSharedPreferences("aop.part2.chapter03.myPasswordFile", Context.MODE_PRIVATE)
myPasswordPreferences.edit(commit = true){
putString("myPasswordKey", passwordFromUser)
}
Toast.makeText(this, "비밀번호가 변경되었습니다!", Toast.LENGTH_SHORT).show()
changePasswordMode = false
changePasswordNumberPickerColor(getColor(R.color.passwordNumberPickerColor1))
}
else{
if(ifPasswordMatch()) {
Toast.makeText(this, "비밀번호 변경 모드로 바뀝니다!", Toast.LENGTH_SHORT).show()
changePasswordMode = true
changePasswordNumberPickerColor(getColor(R.color.passwordNumberPickerColor2))
}
else{
showPasswordFailAlertDialog()
}
}
}
}
private fun ifPasswordMatch() : Boolean{
val myPasswordPreferences = getSharedPreferences("aop.part2.chapter03.myPasswordFile", Context.MODE_PRIVATE)
val myPassword = myPasswordPreferences.getString("myPasswordKey", "000")
val passwordFromUser = "${passwordNumberPicker1.value}${passwordNumberPicker2.value}${passwordNumberPicker3.value}"
return myPassword == passwordFromUser
}
private fun showPasswordFailAlertDialog(){
AlertDialog.Builder(this)
.setTitle("실패")
.setMessage("비밀번호가 맞지 않습니다!")
.setPositiveButton("닫기"){ _, _ -> }
.create()
.show()
}
private fun changePasswordNumberPickerColor(passwordNumberPickerColor: Int){
passwordNumberPicker1.setBackgroundColor(passwordNumberPickerColor)
passwordNumberPicker2.setBackgroundColor(passwordNumberPickerColor)
passwordNumberPicker3.setBackgroundColor(passwordNumberPickerColor)
openButton.setBackgroundColor(passwordNumberPickerColor)
}
}