-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputDataSettingsFragment.kt
136 lines (106 loc) · 4.18 KB
/
InputDataSettingsFragment.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
package sk.drevari.optitimb.ui.settings.inputData
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.observe
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.fragment_setting_input_data.*
import sk.drevari.optitimb.R
import sk.drevari.optitimb.data.RemoveClick
import sk.drevari.optitimb.repo.db.entity.METRIC_HEARTWOOD
class InputDataSettingsFragment : Fragment() {
lateinit var adapterHW: HeartwoodAdapter
lateinit var adapterWEB: WEBAdapter
private lateinit var viewModel: InputDataSettingsViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProvider(this).get(InputDataSettingsViewModel::class.java)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_setting_input_data, container, false)
}
private fun getRBByIndex(index: Int) = when (index) {
1 -> rbBSDiameter
2 -> rbSTDiameter
3 -> rbMTDiameter
else -> rbBTDiameter
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rangeHWR.tickBottomLabels =
createTickLabel(rangeHWR.tickInterval.toInt(), rangeHWR.tickStart.toInt(), rangeHWR.tickEnd.toInt())
rangeWEB.tickBottomLabels =
createTickLabel(rangeWEB.tickInterval.toInt(), rangeWEB.tickStart.toInt(), rangeWEB.tickEnd.toInt())
viewModel.centerInsertion.observe(viewLifecycleOwner) {
tvLblHWRange.text = getString(if (it == METRIC_HEARTWOOD.DIAM) getLengthLabelAccordingMetric() else R.string.PERCENT)
}
viewModel.diamInsertion.observe(viewLifecycleOwner) {
getRBByIndex(it).isChecked = true
}
setupHW()
setupWEB()
viewModel.heartWoodList.observe(viewLifecycleOwner) {
adapterHW.setData(it as ArrayList)
}
viewModel.waneyEdgeBoard.observe(viewLifecycleOwner) {
adapterWEB.setData(it as ArrayList)
}
}
private fun setupHW() {
adapterHW = HeartwoodAdapter(context!!)
rvHWRange.layoutManager = LinearLayoutManager(context!!)
rvHWRange.itemAnimator = DefaultItemAnimator()
rvHWRange.adapter = adapterHW
ibAddHWR.setOnClickListener(::onClick_ibAddHWR)
}
private fun setupWEB() {
adapterWEB = WEBAdapter(context!!)
rvWEBRange.layoutManager = LinearLayoutManager(context!!)
rvWEBRange.itemAnimator = DefaultItemAnimator()
rvWEBRange.adapter = adapterWEB
ibAddWEB.setOnClickListener(::onClick_ibAddWEB)
}
override fun onStart() {
super.onStart()
adapterHW.listener = object: RemoveClick {
override fun removeUUID(id: Int) {
viewModel.removeHeartWood(id)
}
}
adapterWEB.listener = object: RemoveClick {
override fun removeUUID(id: Int) {
viewModel.removeWEB(id)
}
}
}
override fun onStop() {
super.onStop()
adapterHW.listener = null
}
fun onClick_ibAddHWR(view2: View) {
viewModel.addHeartWood(rangeHWR.leftPinValue.toInt(), rangeHWR.rightPinValue.toInt())
}
fun onClick_ibAddWEB(view2: View) {
viewModel.addWEBoard(rangeWEB.leftPinValue.toInt(), rangeWEB.rightPinValue.toInt())
}
private fun createTickLabel(interval: Int, start: Int, end: Int): Array<String> {
val size = ((end - start) / interval) + 1
val labels = Array(size) { it.toString() }
var inx = 0
for (i in start..end step interval) {
labels[inx++] = "$i"
}
return labels
}
private fun getLengthLabelAccordingMetric(): Int {
return if (viewModel.isSi) R.string.cm else R.string.inch
}
}