/
AccountConfigurationFragment.kt
149 lines (123 loc) · 5.65 KB
/
AccountConfigurationFragment.kt
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package fr.bux.rollingdashboard
import android.os.Bundle
import android.text.Editable
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.work.WorkManager
import fr.bux.rollingdashboard.databinding.AccountConfigurationFragmentBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
fun String.toEditable(): Editable = Editable.Factory.getInstance().newEditable(this)
fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return
if (!isAdded) return // Fragment not attached to an Activity
activity?.runOnUiThread(action)
}
/**
* A simple [Fragment] subclass as the second destination in the navigation.
*/
class AccountConfigurationFragment : Fragment() {
private var _binding: AccountConfigurationFragmentBinding? = null
private val binding get() = _binding!!
private val viewModel: AccountConfigurationViewModel by activityViewModels {
AccountConfigurationViewModelFactory(
(activity?.application as RollingDashboardApplication).account_configuration_repository
)
}
lateinit var accountConfiguration: AccountConfiguration
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = AccountConfigurationFragmentBinding.inflate(inflater, container, false)
// Set text input hints
binding.textInputServerAddress.setHint(R.string.text_input_server_address_hint)
binding.textInputUserName.setHint(R.string.text_input_user_name_hint)
binding.passwordPassword.setHint(R.string.text_input_password_hint)
return binding.root
}
private fun isEntryValid(): Boolean {
return viewModel.isEntryValid(
binding.textInputServerAddress.text.toString(),
binding.textInputUserName.text.toString(),
binding.passwordPassword.text.toString(),
)
}
private fun save() {
if (isEntryValid()) {
Toast.makeText(context, R.string.saving, Toast.LENGTH_LONG).show()
// Determine if it is the first account configuration
var itsFirst = true
lifecycleScope.launch {
withContext(Dispatchers.Default) {
val accountConfiguration = viewModel.get()
itsFirst = accountConfiguration != null
}
}
println("Insert account configuration in database")
val networkGrabEach = 3600 // FIXME : determine value for real
viewModel.insert(
AccountConfiguration(
server_address = binding.textInputServerAddress.text.toString(),
user_name = binding.textInputUserName.text.toString(),
password = binding.passwordPassword.text.toString(),
notify_hungry = binding.switchNotificateHungry.isChecked,
notify_thirsty = binding.switchThirst.isChecked,
notify_ap = binding.switchMaxAp.isChecked,
network_grab_each = networkGrabEach,
)
)
if (itsFirst) {
println("It is the first account configuration, enqueue work request")
val workManager = WorkManager.getInstance(requireContext())
workManager.enqueue(
(activity?.application as RollingDashboardApplication).buildPeriodicGrabCharacterWorkRequest()
)
} else {
println("It is not the first account configuration, require immediate grab")
// FIXME BS NOW : Do it, but ... how ? :)
}
Toast.makeText(context, R.string.account_configuration_saved, Toast.LENGTH_LONG).show()
findNavController().navigate(R.id.action_AccountConfigurationFragment_to_DashboardFragment)
} else {
Toast.makeText(context, R.string.wrong_inputs, Toast.LENGTH_LONG).show()
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.buttonGoBackMain.setOnClickListener {
findNavController().navigate(R.id.action_AccountConfigurationFragment_to_DashboardFragment)
}
// Fill form with existing account configuration
lifecycleScope.launch {
withContext(Dispatchers.Default) {
val accountConfiguration = viewModel.get();
if (accountConfiguration != null) {
runOnUiThread {
binding.textInputServerAddress.text = accountConfiguration.server_address.toEditable()
binding.textInputUserName.text = accountConfiguration.user_name.toEditable()
binding.passwordPassword.text = accountConfiguration.password.toEditable()
binding.switchNotificateHungry.isChecked = accountConfiguration.notify_hungry
binding.switchThirst.isChecked = accountConfiguration.notify_thirsty
binding.switchMaxAp.isChecked = accountConfiguration.notify_ap
}
}
}
}
binding.buttonSave.setOnClickListener {
save()
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}