/
SliderView.kt
165 lines (153 loc) · 5.81 KB
/
SliderView.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
/*
* Copyright © 2020 - present | Handwashing reminder by Javinator9889
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*
* Created by Javinator9889 on 13/04/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.activities.views.fragments.washinghands
import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.core.view.doOnLayout
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenStarted
import coil.api.load
import com.javinator9889.handwashingreminder.R
import com.javinator9889.handwashingreminder.activities.base.BaseFragmentView
import com.javinator9889.handwashingreminder.activities.views.viewmodels.*
import kotlinx.android.synthetic.main.wash_your_hands_demo.*
import kotlinx.coroutines.launch
import timber.log.Timber
import java.io.File
import java.util.concurrent.atomic.AtomicInteger
import kotlin.properties.Delegates
private const val WAITING_ITEMS_COUNT = 4
internal const val ARG_POSITION = "bundle:position"
class SliderView : BaseFragmentView() {
override val layoutId: Int = R.layout.wash_your_hands_demo
private lateinit var videoURI: Uri
private var drawableId by Delegates.notNull<Int>()
private val counter = AtomicInteger(0)
private var position by Delegates.notNull<Int>()
private lateinit var videoModelFactory: VideoModelFactory
private lateinit var handsFactory: WashingHandsModelFactory
private val viewModel: VideoModel by viewModels {
SavedViewModelFactory(videoModelFactory, this)
}
private val washingHandsModel: WashingHandsModel by viewModels {
SavedViewModelFactory(handsFactory, this)
}
init {
lifecycleScope.launch {
whenStarted {
loading.visibility = View.VISIBLE
viewModel.videos.observe(viewLifecycleOwner, Observer {
with(File(requireContext().cacheDir, it)) {
videoURI = Uri.fromFile(this)
}
incrementCounter()
Timber.d("Video finished loading")
})
washingHandsModel.image.observe(viewLifecycleOwner, Observer {
try {
image.load(it)
} catch (e: Exception) {
Timber.e(e, "Error while loading Glide view")
image.setImageResource(it)
}
drawableId = it
Timber.d("Image finished loading")
incrementCounter()
})
washingHandsModel.title.observe(viewLifecycleOwner, Observer {
title.text = it
Timber.d("Title finished loading")
incrementCounter()
})
washingHandsModel.description.observe(viewLifecycleOwner,
Observer {
description.text = it
Timber.d("Description finished loading")
incrementCounter()
})
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val args = arguments ?: savedInstanceState ?: throw
IllegalStateException("Arguments cannot be null")
videoModelFactory = VideoModelFactory(args.getInt(ARG_POSITION))
handsFactory = WashingHandsModelFactory(args.getInt(ARG_POSITION))
position = args.getInt(ARG_POSITION)
}
override fun onPause() {
Timber.d("Slide paused")
video.requestFocus()
video.pause()
super.onPause()
}
override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (hidden)
onPause()
else
onResume()
}
override fun onResume() {
Timber.d("Slide resumed")
video.requestFocus()
video.start()
try {
image.load(drawableId)
} catch (e: Exception) {
Timber.e(e, "Error while loading Glide view")
image.setImageResource(drawableId)
}
super.onResume()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
image.doOnLayout {
washingHandsModel.setImageSize(it.measuredWidth, it.measuredHeight)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(ARG_POSITION, position)
}
private fun incrementCounter() {
Timber.d("Counter incremented")
if (counter.incrementAndGet() < WAITING_ITEMS_COUNT)
return
showContent()
}
private fun showContent() {
loading.visibility = View.GONE
video.setVideoURI(videoURI)
title.visibility = View.VISIBLE
video.visibility = View.VISIBLE
video.requestFocus()
video.start()
video.setOnPreparedListener { mp: MediaPlayer? ->
mp?.isLooping = true
}
description.visibility = View.VISIBLE
image.visibility = View.VISIBLE
}
}