summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/ui/NewInputProfileDialogFragment.kt
blob: 6e52bea80e284e7a557b303bf51a574b81b3683c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// SPDX-FileCopyrightText: 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.features.settings.ui

import android.app.Dialog
import android.os.Bundle
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.yuzu.yuzu_emu.databinding.DialogEditTextBinding
import org.yuzu.yuzu_emu.features.settings.model.view.InputProfileSetting
import org.yuzu.yuzu_emu.R

class NewInputProfileDialogFragment : DialogFragment() {
    private var position = 0

    private val settingsViewModel: SettingsViewModel by activityViewModels()

    private lateinit var binding: DialogEditTextBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        position = requireArguments().getInt(POSITION)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        binding = DialogEditTextBinding.inflate(layoutInflater)

        val setting = settingsViewModel.clickedItem as InputProfileSetting
        return MaterialAlertDialogBuilder(requireContext())
            .setTitle(R.string.enter_profile_name)
            .setPositiveButton(android.R.string.ok) { _, _ ->
                val profileName = binding.editText.text.toString()
                if (!setting.isProfileNameValid(profileName)) {
                    Toast.makeText(
                        requireContext(),
                        R.string.invalid_profile_name,
                        Toast.LENGTH_SHORT
                    ).show()
                    return@setPositiveButton
                }

                if (!setting.createProfile(profileName)) {
                    Toast.makeText(
                        requireContext(),
                        R.string.profile_name_already_exists,
                        Toast.LENGTH_SHORT
                    ).show()
                } else {
                    settingsViewModel.setAdapterItemChanged(position)
                }
            }
            .setNegativeButton(android.R.string.cancel, null)
            .setView(binding.root)
            .show()
    }

    companion object {
        const val TAG = "NewInputProfileDialogFragment"

        const val POSITION = "Position"

        fun newInstance(
            settingsViewModel: SettingsViewModel,
            profileSetting: InputProfileSetting,
            position: Int
        ): NewInputProfileDialogFragment {
            settingsViewModel.clickedItem = profileSetting

            val args = Bundle()
            args.putInt(POSITION, position)
            val fragment = NewInputProfileDialogFragment()
            fragment.arguments = args
            return fragment
        }
    }
}