summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/clock/ui/binder/ClockSettingsBinder.kt
blob: d17cdf8a662899e2e1df6c2de2c1f109386b99f6 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.customization.picker.clock.ui.binder

import android.content.Context
import android.content.res.Configuration
import android.text.Spannable
import android.text.SpannableString
import android.text.style.TextAppearanceSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.RadioGroup.OnCheckedChangeListener
import android.widget.SeekBar
import androidx.core.view.doOnPreDraw
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.customization.picker.clock.shared.ClockSize
import com.android.customization.picker.clock.ui.adapter.ClockSettingsTabAdapter
import com.android.customization.picker.clock.ui.view.ClockCarouselView
import com.android.customization.picker.clock.ui.view.ClockHostView
import com.android.customization.picker.clock.ui.view.ClockViewFactory
import com.android.customization.picker.clock.ui.viewmodel.ClockSettingsViewModel
import com.android.customization.picker.color.ui.binder.ColorOptionIconBinder
import com.android.customization.picker.common.ui.view.ItemSpacing
import com.android.wallpaper.R
import com.android.wallpaper.picker.option.ui.binder.OptionItemBinder
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch

/** Bind between the clock settings screen and its view model. */
object ClockSettingsBinder {
    private const val SLIDER_ENABLED_ALPHA = 1f
    private const val SLIDER_DISABLED_ALPHA = .3f
    private const val COLOR_PICKER_ITEM_PREFIX_ID = 1234

    fun bind(
        view: View,
        viewModel: ClockSettingsViewModel,
        clockViewFactory: ClockViewFactory,
        lifecycleOwner: LifecycleOwner,
    ) {
        val clockHostView: ClockHostView = view.requireViewById(R.id.clock_host_view)
        val tabView: RecyclerView = view.requireViewById(R.id.tabs)
        val tabAdapter = ClockSettingsTabAdapter()
        tabView.adapter = tabAdapter
        tabView.layoutManager = LinearLayoutManager(view.context, RecyclerView.HORIZONTAL, false)
        tabView.addItemDecoration(ItemSpacing(ItemSpacing.TAB_ITEM_SPACING_DP))
        val colorOptionContainerListView: LinearLayout = view.requireViewById(R.id.color_options)
        val slider: SeekBar = view.requireViewById(R.id.slider)
        slider.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(p0: SeekBar?, progress: Int, fromUser: Boolean) {
                    if (fromUser) {
                        viewModel.onSliderProgressChanged(progress)
                    }
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                    seekBar?.progress?.let {
                        lifecycleOwner.lifecycleScope.launch { viewModel.onSliderProgressStop(it) }
                    }
                }
            }
        )

        val onCheckedChangeListener = OnCheckedChangeListener { _, id ->
            when (id) {
                R.id.radio_dynamic -> viewModel.setClockSize(ClockSize.DYNAMIC)
                R.id.radio_small -> viewModel.setClockSize(ClockSize.SMALL)
            }
        }
        val clockSizeRadioGroup =
            view.requireViewById<RadioGroup>(R.id.clock_size_radio_button_group)
        clockSizeRadioGroup.setOnCheckedChangeListener(onCheckedChangeListener)
        view.requireViewById<RadioButton>(R.id.radio_dynamic).text =
            getRadioText(
                view.context.applicationContext,
                view.resources.getString(R.string.clock_size_dynamic),
                view.resources.getString(R.string.clock_size_dynamic_description)
            )
        view.requireViewById<RadioButton>(R.id.radio_small).text =
            getRadioText(
                view.context.applicationContext,
                view.resources.getString(R.string.clock_size_small),
                view.resources.getString(R.string.clock_size_small_description)
            )

        val colorOptionContainer = view.requireViewById<View>(R.id.color_picker_container)
        lifecycleOwner.lifecycleScope.launch {
            lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                    viewModel.seedColor.collect { seedColor ->
                        viewModel.selectedClockId.value?.let { selectedClockId ->
                            clockViewFactory.updateColor(selectedClockId, seedColor)
                        }
                    }
                }

                launch { viewModel.tabs.collect { tabAdapter.setItems(it) } }

                launch {
                    viewModel.selectedTab.collect { tab ->
                        when (tab) {
                            ClockSettingsViewModel.Tab.COLOR -> {
                                colorOptionContainer.isVisible = true
                                clockSizeRadioGroup.isInvisible = true
                            }
                            ClockSettingsViewModel.Tab.SIZE -> {
                                colorOptionContainer.isInvisible = true
                                clockSizeRadioGroup.isVisible = true
                            }
                        }
                    }
                }

                launch {
                    viewModel.colorOptions.collect { colorOptions ->
                        colorOptionContainerListView.removeAllViews()
                        colorOptions.forEachIndexed { index, colorOption ->
                            colorOption.payload?.let { payload ->
                                val item =
                                    LayoutInflater.from(view.context)
                                        .inflate(
                                            R.layout.clock_color_option,
                                            colorOptionContainerListView,
                                            false,
                                        ) as LinearLayout
                                val darkMode =
                                    (view.resources.configuration.uiMode and
                                        Configuration.UI_MODE_NIGHT_MASK ==
                                        Configuration.UI_MODE_NIGHT_YES)
                                ColorOptionIconBinder.bind(
                                    item.requireViewById(R.id.foreground),
                                    payload,
                                    darkMode
                                )
                                OptionItemBinder.bind(
                                    view = item,
                                    viewModel = colorOptions[index],
                                    lifecycleOwner = lifecycleOwner,
                                    foregroundTintSpec = null,
                                )

                                val id = COLOR_PICKER_ITEM_PREFIX_ID + index
                                item.id = id
                                colorOptionContainerListView.addView(item)
                            }
                        }
                    }
                }

                launch {
                    viewModel.selectedColorOptionPosition.collect { selectedPosition ->
                        if (selectedPosition != -1) {
                            val colorOptionContainerListView: LinearLayout =
                                view.requireViewById(R.id.color_options)

                            val selectedView =
                                colorOptionContainerListView.findViewById<View>(
                                    COLOR_PICKER_ITEM_PREFIX_ID + selectedPosition
                                )
                            selectedView?.parent?.requestChildFocus(selectedView, selectedView)
                        }
                    }
                }

                launch {
                    combine(
                            viewModel.selectedClockId.mapNotNull { it },
                            viewModel.selectedClockSize,
                            ::Pair,
                        )
                        .collect { (clockId, size) ->
                            clockHostView.removeAllViews()
                            val clockView =
                                when (size) {
                                    ClockSize.DYNAMIC -> clockViewFactory.getLargeView(clockId)
                                    ClockSize.SMALL -> clockViewFactory.getSmallView(clockId)
                                }
                            // The clock view might still be attached to an existing parent. Detach
                            // before adding to another parent.
                            (clockView.parent as? ViewGroup)?.removeView(clockView)
                            clockHostView.addView(clockView)
                            when (size) {
                                ClockSize.DYNAMIC -> {
                                    // When clock size data flow emits clock size signal, we want
                                    // to update the view without triggering on checked change,
                                    // which is supposed to be triggered by user interaction only.
                                    clockSizeRadioGroup.setOnCheckedChangeListener(null)
                                    clockSizeRadioGroup.check(R.id.radio_dynamic)
                                    clockSizeRadioGroup.setOnCheckedChangeListener(
                                        onCheckedChangeListener
                                    )
                                    clockHostView.doOnPreDraw {
                                        it.pivotX = it.width / 2F
                                        it.pivotY = it.height / 2F
                                    }
                                }
                                ClockSize.SMALL -> {
                                    // When clock size data flow emits clock size signal, we want
                                    // to update the view without triggering on checked change,
                                    // which is supposed to be triggered by user interaction only.
                                    clockSizeRadioGroup.setOnCheckedChangeListener(null)
                                    clockSizeRadioGroup.check(R.id.radio_small)
                                    clockSizeRadioGroup.setOnCheckedChangeListener(
                                        onCheckedChangeListener
                                    )
                                    clockHostView.doOnPreDraw {
                                        it.pivotX = ClockCarouselView.getCenteredHostViewPivotX(it)
                                        it.pivotY = 0F
                                    }
                                }
                            }
                        }
                }

                launch {
                    viewModel.sliderProgress.collect { progress ->
                        slider.setProgress(progress, true)
                    }
                }

                launch {
                    viewModel.isSliderEnabled.collect { isEnabled ->
                        slider.isEnabled = isEnabled
                        slider.alpha =
                            if (isEnabled) SLIDER_ENABLED_ALPHA else SLIDER_DISABLED_ALPHA
                    }
                }
            }
        }

        lifecycleOwner.lifecycle.addObserver(
            LifecycleEventObserver { source, event ->
                when (event) {
                    Lifecycle.Event.ON_RESUME -> {
                        clockViewFactory.registerTimeTicker(source)
                    }
                    Lifecycle.Event.ON_PAUSE -> {
                        clockViewFactory.unregisterTimeTicker(source)
                    }
                    else -> {}
                }
            }
        )
    }

    private fun getRadioText(
        context: Context,
        title: String,
        description: String
    ): SpannableString {
        val text = SpannableString(title + "\n" + description)
        text.setSpan(
            TextAppearanceSpan(context, R.style.SectionTitleTextStyle),
            0,
            title.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        text.setSpan(
            TextAppearanceSpan(context, R.style.SectionSubtitleTextStyle),
            title.length + 1,
            title.length + 1 + description.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return text
    }
}