1+ package moe.feng.kotlinyan.common
2+
3+ import android.annotation.TargetApi
4+ import android.app.Activity
5+ import android.app.Fragment
6+ import android.content.Context
7+ import android.content.SharedPreferences
8+ import android.graphics.Typeface
9+ import android.graphics.drawable.Drawable
10+ import android.preference.Preference
11+ import android.preference.PreferenceManager
12+ import android.view.View
13+ import kotlin.properties.ReadOnlyProperty
14+ import kotlin.reflect.KProperty
15+
16+ // View Properties
17+
18+ fun <T : View > Activity.lazyFindView (viewId : Int ): LazyGetView <Activity , T ?> {
19+ return LazyGetView (viewId, { activity, id -> activity.findViewById(id) })
20+ }
21+
22+ fun <T : View > Activity.lazyFindNonNullView (viewId : Int ): LazyGetNonNullView <Activity , T > {
23+ return LazyGetNonNullView (viewId, { activity, id -> activity.findViewById(id) })
24+ }
25+
26+ fun <T : View > Fragment.findView (viewId : Int ): LazyGetView <Fragment , T ?> {
27+ return LazyGetView (viewId, { fragment, id -> fragment.view.findViewById(id) }, dontLazy = true )
28+ }
29+
30+ fun <T : View > Fragment.findNonNullView (viewId : Int ): LazyGetNonNullView <Fragment , T > {
31+ return LazyGetNonNullView (viewId, { fragment, id -> fragment.view.findViewById(id) }, dontLazy = true )
32+ }
33+
34+ fun <T : View > View.lazyFindView (viewId : Int ): LazyGetView <View , T ?> {
35+ return LazyGetView (viewId, { view, id -> view.findViewById(id) })
36+ }
37+
38+ fun <T : View > View.lazyFindNonNullView (viewId : Int ): LazyGetNonNullView <View , T > {
39+ return LazyGetNonNullView (viewId, { view, id -> view.findViewById(id) })
40+ }
41+
42+ class LazyGetView <in R , out T : View ?> (
43+ private val viewId : Int ,
44+ private val getter : (R , Int ) -> T ? ,
45+ private val dontLazy : Boolean = false
46+ ): ReadOnlyProperty<R, T?> {
47+
48+ private var _value : T ? = null
49+
50+ override fun getValue (thisRef : R , property : KProperty <* >): T ? {
51+ if (_value == null || ! dontLazy) _value = getter(thisRef, viewId)
52+ return _value
53+ }
54+
55+ }
56+
57+ class LazyGetNonNullView <in R , out T : View > (
58+ private val viewId : Int ,
59+ private val getter : (R , Int ) -> T ,
60+ private val dontLazy : Boolean = false
61+ ): ReadOnlyProperty<R, T> {
62+
63+ private var _value : T ? = null
64+
65+ override fun getValue (thisRef : R , property : KProperty <* >): T {
66+ if (_value == null || dontLazy) _value = getter(thisRef, viewId)
67+ return _value !!
68+ }
69+
70+ }
71+
72+ // Preference Properties
73+
74+ class GetPreference <out T : Preference >(private val keyName : String )
75+ : ReadOnlyProperty <GetPreference .PreferenceObserver , T > {
76+
77+ override fun getValue (thisRef : PreferenceObserver , property : KProperty <* >): T {
78+ return thisRef.getPreferenceManager().findPreference(keyName) as T
79+ }
80+
81+ interface PreferenceObserver {
82+ fun getPreferenceManager (): PreferenceManager
83+ }
84+
85+ }
86+
87+ // Shared Preferences Properties
88+
89+ class GetSharedPreferences (private val prefName : String ,
90+ private val mode : Int = Context .MODE_PRIVATE ): ReadOnlyProperty<Context, SharedPreferences> {
91+
92+ override fun getValue (thisRef : Context , property : KProperty <* >): SharedPreferences {
93+ return thisRef.getSharedPreferences(prefName, mode)
94+ }
95+
96+ }
97+
98+ // Resources Properties
99+
100+ fun Context.dimenRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Float >
101+ = ResourcesProperty (id, resources::getDimension, loadOnlyOnce)
102+ fun Context.stringRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <String >
103+ = ResourcesProperty <String >(id, resources::getString, loadOnlyOnce)
104+ fun Context.integerRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Int >
105+ = ResourcesProperty (id, resources::getInteger, loadOnlyOnce)
106+ fun Context.boolRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Boolean >
107+ = ResourcesProperty (id, resources::getBoolean, loadOnlyOnce)
108+ fun Context.drawableRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Drawable >
109+ = ResourcesProperty <Drawable >(id, resources::getDrawable, loadOnlyOnce)
110+ fun Context.colorRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Int >
111+ = ResourcesProperty <Int >(id, resources::getColor, loadOnlyOnce)
112+ fun Context.stringArrayRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Array <String >>
113+ = ResourcesProperty (id, resources::getStringArray, loadOnlyOnce)
114+ fun Context.intArrayRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <IntArray >
115+ = ResourcesProperty (id, resources::getIntArray, loadOnlyOnce)
116+ @TargetApi(26 ) fun Context.fontRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Typeface >
117+ = ResourcesProperty (id, resources::getFont, loadOnlyOnce)
118+
119+ fun Fragment.dimenRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Float >
120+ = ResourcesProperty (id, { resources.getDimension(it) }, loadOnlyOnce)
121+ fun Fragment.stringRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <String >
122+ = ResourcesProperty (id, { resources.getString(it) }, loadOnlyOnce)
123+ fun Fragment.integerRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Int >
124+ = ResourcesProperty (id, { resources.getInteger(it) }, loadOnlyOnce)
125+ fun Fragment.boolRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Boolean >
126+ = ResourcesProperty (id, { resources.getBoolean(it) }, loadOnlyOnce)
127+ fun Fragment.drawableRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Drawable >
128+ = ResourcesProperty (id, { resources.getDrawable(it) }, loadOnlyOnce)
129+ fun Fragment.colorRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Int >
130+ = ResourcesProperty (id, { resources.getColor(it) }, loadOnlyOnce)
131+ fun Fragment.stringArrayRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Array <String >>
132+ = ResourcesProperty (id, { resources.getStringArray(it) }, loadOnlyOnce)
133+ fun Fragment.intArrayRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <IntArray >
134+ = ResourcesProperty (id, { resources.getIntArray(it) }, loadOnlyOnce)
135+ @TargetApi(26 ) fun Fragment.fontRes (id : Int , loadOnlyOnce : Boolean = true): ResourcesProperty <Typeface >
136+ = ResourcesProperty (id, { resources.getFont(it) }, loadOnlyOnce)
137+
138+ class ResourcesProperty <out T > (
139+ private val resourceId : Int ,
140+ private val func : (resourceId: Int ) -> T ,
141+ private val loadOnlyOnce : Boolean = true ): ReadOnlyProperty<Any, T> {
142+
143+ private var _value : T ? = null
144+
145+ override fun getValue (thisRef : Any , property : KProperty <* >): T {
146+ if (_value == null || ! loadOnlyOnce) _value = func(resourceId)
147+ return _value !!
148+ }
149+
150+ }
0 commit comments