forked from streetcomplete/StreetComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddOpeningHoursForm.kt
194 lines (165 loc) · 8.21 KB
/
AddOpeningHoursForm.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package de.westnordost.streetcomplete.quests.opening_hours
import android.os.Bundle
import android.view.Menu.NONE
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.PopupMenu
import androidx.core.view.isGone
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import de.westnordost.osm_opening_hours.parser.toOpeningHours
import de.westnordost.osm_opening_hours.parser.toOpeningHoursOrNull
import de.westnordost.streetcomplete.R
import de.westnordost.streetcomplete.databinding.QuestOpeningHoursBinding
import de.westnordost.streetcomplete.databinding.QuestOpeningHoursCommentBinding
import de.westnordost.streetcomplete.osm.opening_hours.parser.toOpeningHoursRows
import de.westnordost.streetcomplete.quests.AbstractOsmQuestForm
import de.westnordost.streetcomplete.quests.AnswerItem
import de.westnordost.streetcomplete.quests.opening_hours.adapter.OpeningHoursAdapter
import de.westnordost.streetcomplete.quests.opening_hours.adapter.OpeningMonthsRow
import de.westnordost.streetcomplete.quests.opening_hours.adapter.OpeningWeekdaysRow
import de.westnordost.streetcomplete.view.AdapterDataChangedWatcher
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
class AddOpeningHoursForm : AbstractOsmQuestForm<OpeningHoursAnswer>() {
override val contentLayoutResId = R.layout.quest_opening_hours
private val binding by contentViewBinding(QuestOpeningHoursBinding::bind)
override val buttonPanelAnswers get() =
if (isDisplayingPreviousOpeningHours) {
listOf(
AnswerItem(R.string.quest_generic_hasFeature_no) { setAsResurvey(false) },
AnswerItem(R.string.quest_generic_hasFeature_yes) {
applyAnswer(RegularOpeningHours(
element.tags["opening_hours"]!!.toOpeningHours(lenient = true)
))
}
)
} else {
emptyList()
}
override val otherAnswers = listOf(
AnswerItem(R.string.quest_openingHours_no_sign) { confirmNoSign() },
AnswerItem(R.string.quest_openingHours_answer_no_regular_opening_hours) { showInputCommentDialog() },
AnswerItem(R.string.quest_openingHours_answer_247) { showConfirm24_7Dialog() },
AnswerItem(R.string.quest_openingHours_answer_seasonal_opening_hours) {
setAsResurvey(false)
openingHoursAdapter.changeToMonthsMode()
}
)
private lateinit var openingHoursAdapter: OpeningHoursAdapter
private var isDisplayingPreviousOpeningHours: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
openingHoursAdapter = OpeningHoursAdapter(requireContext())
openingHoursAdapter.firstDayOfWorkweek = countryInfo.firstDayOfWorkweek
openingHoursAdapter.regularShoppingDays = countryInfo.regularShoppingDays
openingHoursAdapter.locale = countryInfo.userPreferredLocale
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (savedInstanceState != null) {
onLoadInstanceState(savedInstanceState)
} else {
initStateFromTags()
}
openingHoursAdapter.registerAdapterDataObserver(AdapterDataChangedWatcher { checkIsFormComplete() })
binding.openingHoursList.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
binding.openingHoursList.adapter = openingHoursAdapter
binding.openingHoursList.isNestedScrollingEnabled = false
checkIsFormComplete()
binding.addTimesButton.setOnClickListener { onClickAddButton(it) }
}
private fun onClickAddButton(v: View) {
val rows = openingHoursAdapter.rows
val addMonthAvailable = rows.any { it is OpeningMonthsRow }
val addTimeAvailable = rows.isNotEmpty() && rows.last() is OpeningWeekdaysRow
val addOffDayAvailable = rows.isNotEmpty() && rows.last() is OpeningWeekdaysRow
if (addMonthAvailable || addTimeAvailable || addOffDayAvailable) {
val popup = PopupMenu(requireContext(), v)
if (addTimeAvailable) popup.menu.add(NONE, 0, NONE, R.string.quest_openingHours_add_hours)
popup.menu.add(NONE, 1, NONE, R.string.quest_openingHours_add_weekdays)
if (addOffDayAvailable) popup.menu.add(NONE, 2, NONE, R.string.quest_openingHours_add_off_days)
if (addMonthAvailable) popup.menu.add(NONE, 3, NONE, R.string.quest_openingHours_add_months)
popup.setOnMenuItemClickListener { item ->
when (item.itemId) {
0 -> openingHoursAdapter.addNewHours()
1 -> openingHoursAdapter.addNewWeekdays()
2 -> openingHoursAdapter.addNewOffDays()
3 -> openingHoursAdapter.addNewMonths()
}
true
}
popup.show()
} else {
openingHoursAdapter.addNewWeekdays()
}
}
private fun initStateFromTags() {
val oh = element.tags["opening_hours"]
val rows = oh?.toOpeningHoursOrNull(lenient = true)?.toOpeningHoursRows()
if (rows != null) {
openingHoursAdapter.rows = rows.toMutableList()
setAsResurvey(true)
} else {
setAsResurvey(false)
}
}
private fun onLoadInstanceState(savedInstanceState: Bundle) {
openingHoursAdapter.rows = Json.decodeFromString(savedInstanceState.getString(OPENING_HOURS_DATA)!!)
setAsResurvey(savedInstanceState.getBoolean(IS_DISPLAYING_PREVIOUS_HOURS))
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(OPENING_HOURS_DATA, Json.encodeToString(openingHoursAdapter.rows))
outState.putBoolean(IS_DISPLAYING_PREVIOUS_HOURS, isDisplayingPreviousOpeningHours)
}
override fun onClickOk() {
applyAnswer(RegularOpeningHours(openingHoursAdapter.createOpeningHours()))
}
private fun showInputCommentDialog() {
val dialogBinding = QuestOpeningHoursCommentBinding.inflate(layoutInflater)
AlertDialog.Builder(requireContext())
.setTitle(R.string.quest_openingHours_comment_title)
.setView(dialogBinding.root)
.setPositiveButton(android.R.string.ok) { _, _ ->
val txt = dialogBinding.commentInput.text.toString().replace("\"", "").trim()
if (txt.isEmpty()) {
AlertDialog.Builder(requireContext())
.setMessage(R.string.quest_openingHours_emptyAnswer)
.setPositiveButton(android.R.string.ok, null)
.show()
} else {
applyAnswer(DescribeOpeningHours(txt))
}
}
.setNegativeButton(android.R.string.cancel, null)
.show()
}
private fun setAsResurvey(resurvey: Boolean) {
openingHoursAdapter.isEnabled = !resurvey
isDisplayingPreviousOpeningHours = resurvey
binding.addTimesButton.isGone = resurvey
updateButtonPanel()
}
private fun showConfirm24_7Dialog() {
AlertDialog.Builder(requireContext())
.setMessage(R.string.quest_openingHours_24_7_confirmation)
.setPositiveButton(R.string.quest_generic_hasFeature_yes) { _, _ ->
applyAnswer(AlwaysOpen)
}
.setNegativeButton(R.string.quest_generic_hasFeature_no, null)
.show()
}
private fun confirmNoSign() {
AlertDialog.Builder(requireContext())
.setTitle(R.string.quest_generic_confirmation_title)
.setPositiveButton(R.string.quest_generic_confirmation_yes) { _, _ -> applyAnswer(NoOpeningHoursSign) }
.setNegativeButton(R.string.quest_generic_confirmation_no, null)
.show()
}
override fun isFormComplete() = openingHoursAdapter.rows.isNotEmpty() && !isDisplayingPreviousOpeningHours
companion object {
private const val OPENING_HOURS_DATA = "oh_data"
private const val IS_DISPLAYING_PREVIOUS_HOURS = "oh_is_displaying_previous_hours"
}
}