-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiseaseInformationViewModel.kt
160 lines (149 loc) · 6.15 KB
/
DiseaseInformationViewModel.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
/*
* 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 19/04/20 - Handwashing reminder.
*/
package com.javinator9889.handwashingreminder.activities.views.viewmodels
import android.os.Parcel
import android.os.Parcelable
import android.text.Spanned
import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.beust.klaxon.Klaxon
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.javinator9889.handwashingreminder.collections.DiseasesInformation
import com.javinator9889.handwashingreminder.collections.DiseasesList
import com.javinator9889.handwashingreminder.collections.DiseasesListWrapper
import com.javinator9889.handwashingreminder.utils.RemoteConfig.Keys.DISEASES_JSON
import kotlinx.coroutines.*
import org.sufficientlysecure.htmltextview.HtmlFormatter
import org.sufficientlysecure.htmltextview.HtmlFormatterBuilder
private const val DATA_KEY = "text:html:text"
private const val PARSED_JSON_KEY = "text:json:parsed"
class DiseaseInformationViewModel(
private val state: SavedStateHandle
) : ViewModel() {
val parsedHTMLText: LiveData<List<ParsedHTMLText>> = liveData {
emitSource(state.getLiveData(DATA_KEY, parseHTML()))
}
private suspend fun parseHTML(): List<ParsedHTMLText> {
val informationList = withContext(Dispatchers.IO) {
if (state.contains(PARSED_JSON_KEY) &&
state.get<List<DiseasesInformation>>(PARSED_JSON_KEY) != null
)
DiseasesList(
state.get<List<DiseasesInformation>>(PARSED_JSON_KEY)!!
)
val diseasesString =
with(Firebase.remoteConfig) {
getString(DISEASES_JSON)
}
Klaxon().parse<DiseasesList>(diseasesString)
} ?: return emptyList()
state.set(
PARSED_JSON_KEY,
DiseasesListWrapper(informationList.diseases)
)
return withContext(Dispatchers.Default) {
val parsedItemsList =
ArrayList<ParsedHTMLText>(informationList.diseases.size)
val deferreds =
ArrayList<List<Deferred<Spanned>>>(informationList.diseases.size)
informationList.diseases.forEach { information ->
val htmlDef = listOf(
async { createHTML(information.name) },
async { createHTML(information.shortDescription) },
async { createHTML(information.longDescription) },
async { createHTML(information.provider) },
async { createHTML(information.website) },
async { createHTML(information.symptoms) },
async { createHTML(information.prevention) }
)
deferreds.add(htmlDef)
}
deferreds.forEachIndexed { i, infoList ->
val data = infoList.awaitAll()
parsedItemsList.add(
i, ParsedHTMLText(
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6]
)
)
}
state.set(DATA_KEY, parsedItemsList)
parsedItemsList
}
}
private fun createHTML(htmlText: String): Spanned =
with(HtmlFormatterBuilder()) {
html = htmlText
isRemoveTrailingWhiteSpace = true
HtmlFormatter.formatHtml(this)
}
}
class DiseaseInformationFactory :
ViewModelAssistedFactory<DiseaseInformationViewModel> {
override fun create(handle: SavedStateHandle) =
DiseaseInformationViewModel(handle)
}
data class ParsedHTMLText(
val name: CharSequence,
val shortDescription: CharSequence,
val longDescription: CharSequence,
val provider: CharSequence,
val website: CharSequence,
val symptoms: CharSequence,
val prevention: CharSequence
) : Parcelable {
constructor(parcel: Parcel) : this(
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel),
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel),
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel),
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel),
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel),
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel),
TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel)
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
TextUtils.writeToParcel(name, parcel, flags)
TextUtils.writeToParcel(shortDescription, parcel, flags)
TextUtils.writeToParcel(longDescription, parcel, flags)
TextUtils.writeToParcel(provider, parcel, flags)
TextUtils.writeToParcel(website, parcel, flags)
TextUtils.writeToParcel(symptoms, parcel, flags)
TextUtils.writeToParcel(prevention, parcel, flags)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<ParsedHTMLText> {
override fun createFromParcel(parcel: Parcel): ParsedHTMLText {
return ParsedHTMLText(parcel)
}
override fun newArray(size: Int): Array<ParsedHTMLText?> {
return arrayOfNulls(size)
}
}
}