-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathFontOptions.kt
More file actions
59 lines (51 loc) · 2.13 KB
/
FontOptions.kt
File metadata and controls
59 lines (51 loc) · 2.13 KB
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
package com.reactnativenavigation.options
import android.graphics.Typeface
import com.reactnativenavigation.options.params.NullText
import com.reactnativenavigation.options.params.Text
import com.reactnativenavigation.options.parsers.TypefaceLoader
class FontOptions {
private var isDirty = false
var fontFamily: Text = NullText()
set(value) {
field = value
isDirty = true
}
var fontStyle: Text = NullText()
set(value) {
field = value
isDirty = true
}
var fontWeight: Text = NullText()
set(value) {
field = value
isDirty = true
}
private var _typeface: Typeface? = null
@JvmOverloads fun getTypeface(typefaceLoader: TypefaceLoader, defaultTypeface: Typeface? = null): Typeface? {
if (isDirty) {
_typeface = typefaceLoader.getTypeFace(fontFamily.get(null), fontStyle.get(""), fontWeight.get(""), defaultTypeface)
isDirty = false
}
return _typeface
?: defaultTypeface?.let { typefaceLoader.getTypeFace(fontFamily.get(null), fontStyle.get(""), fontWeight.get(""), it) }
}
fun mergeWith(other: FontOptions) {
if (other.fontFamily.hasValue()) fontFamily = other.fontFamily
if (other.fontStyle.hasValue()) fontStyle = other.fontStyle
if (other.fontWeight.hasValue()) fontWeight = other.fontWeight
}
fun mergeWithDefault(defaultOptions: FontOptions) {
if (!fontFamily.hasValue()) fontFamily = defaultOptions.fontFamily
if (!fontStyle.hasValue()) fontStyle = defaultOptions.fontStyle
if (!fontWeight.hasValue()) fontWeight = defaultOptions.fontWeight
}
fun hasValue() = fontFamily.hasValue() || fontStyle.hasValue() || fontWeight.hasValue()
@JvmOverloads fun get(defaultValue: FontOptions? = null): FontOptions? {
return if (hasValue()) this else defaultValue
}
override fun equals(other: Any?) = (other as? FontOptions)?.let {
fontFamily.equals(other.fontFamily) &&
fontStyle.equals(other.fontStyle) &&
fontWeight.equals(other.fontWeight)
} ?: false
}