[패캠 안드로이드] Part2 Ch3.2 비밀 다이어리(2)

0
post-thumbnail
post-custom-banner

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,2,3 초기화
        passwordNumberPicker1
        passwordNumberPicker2
        passwordNumberPicker3
        //openButton 속성 초기화
        initOpenButton()
        //changePasswordButton 속성 초기화
        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) {
                //User가 NumberPicker로 입력한 비밀번호 가져오기
                val passwordFromUser = "${passwordNumberPicker1.value}${passwordNumberPicker2.value}${passwordNumberPicker3.value}"

                //sharedPreference에 저장된 비밀번호 변경하기
                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{
        //sharedPreference에 저장된 비밀번호 가져오기
        val myPasswordPreferences = getSharedPreferences("aop.part2.chapter03.myPasswordFile", Context.MODE_PRIVATE)
        val myPassword = myPasswordPreferences.getString("myPasswordKey", "000")

        //User가 NumberPicker로 입력한 비밀번호 가져오기
        val passwordFromUser = "${passwordNumberPicker1.value}${passwordNumberPicker2.value}${passwordNumberPicker3.value}"

        return myPassword == passwordFromUser
    }

    private fun showPasswordFailAlertDialog(){
        AlertDialog.Builder(this)
            .setTitle("실패")
            .setMessage("비밀번호가 맞지 않습니다!")
            .setPositiveButton("닫기"){ _, _ -> } //사용되지 않는 인자(dialog, which) _로 표현
            .create()
            .show()
    }

    private fun changePasswordNumberPickerColor(passwordNumberPickerColor: Int){
        passwordNumberPicker1.setBackgroundColor(passwordNumberPickerColor)
        passwordNumberPicker2.setBackgroundColor(passwordNumberPickerColor)
        passwordNumberPicker3.setBackgroundColor(passwordNumberPickerColor)
        openButton.setBackgroundColor(passwordNumberPickerColor)
    }
}
profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글