PopUpDialog

나고수·2022년 3월 6일
0

1일1공부

목록 보기
12/68

팝업다이어로그를 만들어보자
팝업 오늘하루다시보지않기 를 구현하는 방법
참고

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return

        val currentCalendar = Calendar.getInstance()
        val readDate = sharedPref.getLong("readDate", 0)
        currentCalendar.timeInMillis = readDate
        currentCalendar.get(Calendar.DAY_OF_MONTH)

//오늘날짜 - 저장된(읽은)날짜 !=0 이면 오늘은 '오늘하루보지않기'를 누른적이 없는거임 
        if (Calendar.getInstance()
                .get(Calendar.DAY_OF_MONTH) - currentCalendar.get(Calendar.DAY_OF_MONTH) != 0
        ) PopupDialog().show(this.supportFragmentManager, "popUpDialog")
    }
}
class PopupDialog(
) : DialogFragment(), View.OnClickListener {

    private lateinit var binding: DialogMainPopupBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = DialogMainPopupBinding.inflate(inflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        Glide.with(this)
            .load(R.drawable.taco)
            .into(binding.image)

        binding.btnNegative.setOnClickListener(this)
        binding.btnPositive.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            binding.btnNegative.id -> onNegativeButtonClickListener()
            binding.image.id -> onPositiveButtonClickListener()
        }
    }

    //오늘하루 보지 않기 
    //보통 sharedPreferences 오늘 날짜를 저장하고, 앱 여는 날의 날짜와 sharedpreferences에 저장된 날짜를 비교해서 다시보지 않기를 구현하는 경우가 많은듯.
    //아예 다시보지않기면 sharedPrefence에 저장된 값이 있냐없냐로만 판단하면 될듯. 
    private fun onNegativeButtonClickListener() {
        val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
        with(sharedPref.edit()) {
            putLong("readDate", System.currentTimeMillis())
            apply()
        }
        dismiss()
    }

    private fun onPositiveButtonClickListener() {
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.naver.com"))
        startActivity(intent)
        dismiss()
    }
}
profile
되고싶다

0개의 댓글