-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomExtensions.kt
141 lines (112 loc) · 4.27 KB
/
CustomExtensions.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
package com.rethinkbooks.utils
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.support.v4.content.ContextCompat
import android.view.View
import android.widget.TextView
import com.rethinkbooks.api.App
import com.rethinkbooks.datakit.R
import com.rethinkbooks.main.BaseLifecycleActivity
import com.rethinkbooks.main.BaseViewModel
import com.rethinkbooks.reader.fonts.FontInterface
import com.rethinkbooks.reader.fonts.FontManager
import com.rethinkbooks.reader.fonts.ReaderFont
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Types
object CustomExtensionsObject {}
enum class JSONDefaultEnum(val default: String) {
JSON_OBJECT(JSONDefaults.OBJECT),
JSON_ARRAY(JSONDefaults.ARRAY)
}
val Any.toJsonString
get() = toJson(JSONDefaultEnum.JSON_OBJECT)
val app
get() = App.instance
/**
* Convert Any object to a Json Sring, return null if not possible
*/
fun Any.toJson(default: String? = null): String? = this.let {
App.instance.moshi.adapter(it.getClass()).toJson(it)?.let {
it
} ?: default
} ?: default
/**
* Convert Any object to a Json Sring, return null if not possible
*/
fun Any.toJson(default: JSONDefaultEnum): String = this.let {
App.instance.moshi.adapter(it.getClass()).toJson(it)?.let {
it
} ?: default.default
}
/**
* Attempt Json String to Class, return null if fails
* @param jsonString
*/
fun <T : Any> Class<T>.fromJson(jsonString: String?): T? = jsonString?.let {
App.instance.moshi.adapter(this).fromJson(jsonString)
}
/**
* Attempt Json String to Class, return null if fails
* @param clazzType
fun <T : Any> String?.fromJson(clazzType: Class<T>): T? = this?.let {
App.instance.moshi.adapter(clazzType).fromJson(it)
}
inline fun <reified T> String?.toObject() = this?.let { App.instance.moshi.adapter(T::class.java).fromJson(it) }
*/
/**
* Use Moshi to convert json string to object. Try/Catch catches any failuers and a default value is declared
* @param clazzType - the class type to use
* @param default - default value to use
* @return the object
*/
inline fun <reified T> String?.toSafeObject(clazzType: Class<T> = T::class.java, default: () -> T): T = this?.let {
try {
"toSafeObject attempt for $clazzType = $this".timber.d
return@let App.instance.moshi.adapter(clazzType).fromJson(it) ?: default()
} catch (e: Exception) {
e.timber.d("[toSafeObject failed for $clazzType]")
}
return default()
} ?: default()
inline fun <reified T> String?.toSafeObjectElseNull(clazzType: Class<T> = T::class.java): T? = toSafeObject<T?> { null }
val Boolean.isFalse
get() = !this
/**
* Attempt Json String to Class, return null if fails
* @param jsonString
*/
inline fun <reified T : Any> Class<T>.fromJsonArray(jsonString: String?): List<T>? = jsonString?.let {
val type = Types.newParameterizedType(List::class.java, this)
val adapter: JsonAdapter<List<T>> = App.instance.moshi.adapter(type)
adapter.fromJson(jsonString)
}
/**
* Attempt Json String to Class, return null if fails
* @param clazzType
*/
inline fun <reified T : Any> String?.fromJsonArray(clazzType: Class<T> = T::class.java): List<T>? = this?.let {
val type = Types.newParameterizedType(List::class.java, clazzType)
val adapter: JsonAdapter<List<T>> = App.instance.moshi.adapter(type)
adapter.fromJson(this)
}
fun <T : BaseViewModel> View.getViewModel(clazzType: Class<T>) = ViewModelProviders.of(context as BaseLifecycleActivity).get(clazzType)
val Int.color
get() = ContextCompat.getColor(App.instance, this)
val Int.string
get() = app.getString(this)
inline fun <reified T> Intent.getMoshi(name: String = T::class.java.canonicalName, clazz: Class<T> = T::class.java) = getStringExtra(name).toSafeObjectElseNull(clazz)
inline fun <reified T> Intent.putMoshi(item: T, name: String = T::class.java.canonicalName) = putExtra(name, item?.toJson())
var TextView.typeface_bs: FontInterface
set(value) {
typeface = FontManager.getFontTypeface(value)
}
get() = ReaderFont.BOOKSHOUT_DEFAULT
val bookShoutResourceManager
get() = app.bookShoutResourceManager
var View.backgroundColorResource: Int
set(value) {
setBackgroundColor(value.color)
}
get() = R.color.transparent
val Network.wifi
get() = Network.getWifi(app)