Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Commit

Permalink
[ResourceDelegate] Use the lazy delegate from Kotlin's standard library.
Browse files Browse the repository at this point in the history
  • Loading branch information
ragunathjawahar committed Apr 3, 2015
1 parent 89152cc commit 34fcba8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import android.widget.TextView
import com.mobsandgeeks.kaffeine.adapter.ViewHolder
import com.mobsandgeeks.kaffeine.layoutInflater
import com.mobsandgeeks.kaffeine.view.find
import kotlinx.android.synthetic.activity_cricket_teams_recycler_view.*

data class CricketTeam(val name: String, val captain: String)

Expand All @@ -35,6 +34,8 @@ public class CricketTeamsActivity : Activity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cricket_teams_recycler_view)

val cricketTeamsRecyclerView = find<RecyclerView>(R.id.cricketTeamsRecyclerView)

cricketTeamsRecyclerView.setLayoutManager(LinearLayoutManager(this))
cricketTeamsRecyclerView.setAdapter(CricRecylerAdapter(this, teams))

Expand Down Expand Up @@ -70,7 +71,7 @@ class CricketTeamViewHolder(view: View) : ViewHolder(view) {
}

class CricRecylerAdapter(val context: Context, val teams: List<CricketTeam>)
: RecyclerView.Adapter<CricRecyclerHolder>() {
: RecyclerView.Adapter<CricRecyclerHolder>() {
val layoutInflater = context.layoutInflater()

override fun onCreateViewHolder(parent: ViewGroup?, position: Int): CricRecyclerHolder {
Expand Down
32 changes: 0 additions & 32 deletions kaffeine/src/main/kotlin/com/mobsandgeeks/kaffeine/Base.kt

This file was deleted.

17 changes: 4 additions & 13 deletions kaffeine/src/main/kotlin/com/mobsandgeeks/kaffeine/Fragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@ package com.mobsandgeeks.kaffeine

import android.app.Activity
import android.app.Fragment
import kotlin.properties.Delegates
import kotlin.properties.ReadOnlyProperty


val Fragment.context: ReadOnlyProperty<Any, Activity?>
get() = ActivityDelegate(this)

/*
* -----------------------------------------------------------------------------
* Private functions
* -----------------------------------------------------------------------------
*/
private class ActivityDelegate<Activity>(private val fragment: Fragment)
: ReadOnlyLazyDelegate<Any, Activity> {

[suppress("UNCHECKED_CAST")]
override fun lazy(): Activity = fragment.getActivity() as Activity
}
get() = Delegates.lazy {
getActivity()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.mobsandgeeks.kaffeine
import android.os.Build


public fun Any.platformVersion(): Int = Build.VERSION.SDK_INT
public fun Any.platformApi(): Int = Build.VERSION.SDK_INT

public fun Any.platformIsOlderThan(version: Int): Boolean = Build.VERSION.SDK_INT < version

Expand Down
169 changes: 35 additions & 134 deletions kaffeine/src/main/kotlin/com/mobsandgeeks/kaffeine/ResourceDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,167 +19,68 @@ import android.content.res.ColorStateList
import android.content.res.XmlResourceParser
import android.graphics.Movie
import android.graphics.Typeface
import kotlin.properties.Delegates
import kotlin.properties.ReadOnlyProperty


public fun Context.animation(resId: Int): ReadOnlyProperty<Any, XmlResourceParser> =
AnimationResDelegate(this, resId)
Delegates.lazy { getAnimation(resId) }

public fun Context.boolean(resId: Int): ReadOnlyProperty<Any, Boolean> =
BooleanResDelegate(this, resId)

public fun Context.color(resId: Int): ReadOnlyProperty<Any, Int> =
ColorResDelegate(this, resId)

public fun Context.colorStateList(resId: Int): ReadOnlyProperty<Any, ColorStateList> =
ColorStateListResDelegate(this, resId)

public fun Context.dimension(resId: Int): ReadOnlyProperty<Any, Float> =
DimensionResDelegate(this, resId)

public fun Context.intArray(resId: Int): ReadOnlyProperty<Any, IntArray> =
IntArrayResDelegate(this, resId)

public fun Context.integer(resId: Int): ReadOnlyProperty<Any, Int> =
IntegerResDelegate(this, resId)

public fun Context.layout(resId: Int): ReadOnlyProperty<Any, XmlResourceParser> =
LayoutResDelegate(this, resId)

public fun Context.movie(resId: Int): ReadOnlyProperty<Any, Movie> =
MovieResDelegate(this, resId)

// TODO Test
public fun Context.quantityString(resId: Int, quantity: Int): ReadOnlyProperty<Any, String> =
QuantityStringResDelegate(this, resId, quantity)

// TODO Test
public fun Context.quantityString(
resId: Int, quantity: Int, vararg formatArgs: Any): ReadOnlyProperty<Any, String> =
QuantityStringResDelegate(this, resId, quantity, formatArgs)

public fun Context.quantityText(resId: Int, quantity: Int): ReadOnlyProperty<Any, CharSequence> =
QuantityTextResDelegate(this, resId, quantity)

public fun Context.string(resId: Int): ReadOnlyProperty<Any, String> =
StringResDelegate(this, resId)

public fun Context.stringArray(resId: Int): ReadOnlyProperty<Any, Array<String>> =
StringArrayResDelegate(this, resId)

public fun Context.typeface(assetPathId: Int): ReadOnlyProperty<Any, Typeface> =
TypefaceDelegate(this, assetPathId)

public fun Context.typeface(assetPath: String): ReadOnlyProperty<Any, Typeface> =
TypefaceDelegate(this, assetPath)

/*
* -----------------------------------------------------------------------------
* Delegates
* -----------------------------------------------------------------------------
*/
private class AnimationResDelegate(context: Context, resId: Int)
: ResourceDelegate<XmlResourceParser>(context, resId) {

override fun lazy(): XmlResourceParser = context.getAnimation(resId)
public fun Context.boolean(resId: Int): ReadOnlyProperty<Any, Boolean> = Delegates.lazy {
getBoolean(resId)
}

private class BooleanResDelegate(context: Context, resId: Int)
: ResourceDelegate<Boolean>(context, resId) {

override fun lazy(): Boolean = context.getBoolean(resId)
}

private class ColorResDelegate(context: Context, resId: Int)
: ResourceDelegate<Int>(context, resId) {

override fun lazy(): Int = context.getColor(resId)
}

private class ColorStateListResDelegate(context: Context, resId: Int)
: ResourceDelegate<ColorStateList>(context, resId) {

override fun lazy(): ColorStateList = context.getColorStateList(resId)
public fun Context.color(resId: Int): ReadOnlyProperty<Any, Int> = Delegates.lazy {
getColor(resId)
}

private class DimensionResDelegate(context: Context, resId: Int)
: ResourceDelegate<Float>(context, resId) {
public fun Context.colorStateList(resId: Int): ReadOnlyProperty<Any, ColorStateList> =
Delegates.lazy { getColorStateList(resId) }

override fun lazy(): Float = context.getDimension(resId)
public fun Context.dimension(resId: Int): ReadOnlyProperty<Any, Float> = Delegates.lazy {
getDimension(resId)
}

private class IntArrayResDelegate(context: Context, resId: Int)
: ResourceDelegate<IntArray>(context, resId) {

override fun lazy(): IntArray = context.getIntArray(resId)
public fun Context.intArray(resId: Int): ReadOnlyProperty<Any, IntArray> = Delegates.lazy {
getIntArray(resId)
}

private class IntegerResDelegate(context: Context, resId: Int)
: ResourceDelegate<Int>(context, resId) {

override fun lazy(): Int = context.getInteger(resId)
public fun Context.integer(resId: Int): ReadOnlyProperty<Any, Int> = Delegates.lazy {
getInteger(resId)
}

private class LayoutResDelegate(context: Context, resId: Int)
: ResourceDelegate<XmlResourceParser>(context, resId) {

override fun lazy(): XmlResourceParser = context.getLayout(resId)
public fun Context.layout(resId: Int): ReadOnlyProperty<Any, XmlResourceParser> = Delegates.lazy {
getLayout(resId)
}

private class MovieResDelegate(context: Context, resId: Int)
: ResourceDelegate<Movie>(context, resId) {

override fun lazy(): Movie = context.getMovie(resId)
public fun Context.movie(resId: Int): ReadOnlyProperty<Any, Movie> = Delegates.lazy {
getMovie(resId)
}

private class QuantityStringResDelegate(context: Context, resId: Int, private val quantity: Int)
: ResourceDelegate<String>(context, resId) {
private var formatArgs: Any? = null

constructor(context: Context, resId: Int, quantity: Int, formatArgs: Any)
: this(context, resId, quantity) {
this.formatArgs = formatArgs
}
// TODO Test
public fun Context.quantityString(resId: Int, quantity: Int): ReadOnlyProperty<Any, String> =
Delegates.lazy { getQuantityString(resId, quantity) }

override fun lazy(): String = if (formatArgs != null) {
context.getQuantityString(resId, quantity, formatArgs!!)
} else {
context.getQuantityString(resId, quantity)
}
}
// TODO Test
public fun Context.quantityString(
resId: Int, quantity: Int, vararg formatArgs: Any): ReadOnlyProperty<Any, String> =
Delegates.lazy { getQuantityString(resId, quantity, formatArgs) }

private class QuantityTextResDelegate(context: Context, resId: Int, private val quantity: Int)
: ResourceDelegate<CharSequence>(context, resId) {
public fun Context.quantityText(resId: Int, quantity: Int): ReadOnlyProperty<Any, CharSequence> =
Delegates.lazy { getQuantityText(resId, quantity) }

override fun lazy(): CharSequence = context.getQuantityText(resId, quantity)
public fun Context.string(resId: Int): ReadOnlyProperty<Any, String> = Delegates.lazy {
getString(resId)
}

private class StringResDelegate(context: Context, resId: Int)
: ResourceDelegate<String>(context, resId) {

override fun lazy(): String = context.getString(resId)
public fun Context.stringArray(resId: Int): ReadOnlyProperty<Any, Array<String>> = Delegates.lazy {
getStringArray(resId)
}

private class StringArrayResDelegate(context: Context, resId: Int)
: ResourceDelegate<Array<String>>(context, resId) {

override fun lazy(): Array<String> = context.getStringArray(resId)
public fun Context.typeface(assetPathId: Int): ReadOnlyProperty<Any, Typeface> = Delegates.lazy {
typefaceFromAssets(assetPathId)
}

private class TypefaceDelegate(context: Context, private val assetPathId: Int)
: ResourceDelegate<Typeface>(context, assetPathId) {
private var assetPath: String? = null

constructor(context: Context, assetPath: String) : this(context, -1) {
this.assetPath = assetPath
}

override fun lazy(): Typeface = if (assetPath != null) {
context.typefaceFromAssets(assetPath!!)
} else {
context.typefaceFromAssets(assetPathId)
}
public fun Context.typeface(assetPath: String): ReadOnlyProperty<Any, Typeface> = Delegates.lazy {
typefaceFromAssets(assetPath)
}

private abstract class ResourceDelegate<T>(val context: Context, val resId: Int)
: ReadOnlyLazyDelegate<Any, T>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package com.mobsandgeeks.kaffeine.view

import android.app.Activity
import android.view.View


inline fun <reified T : View> View.find(id: Int): T = this.findViewById(id) as T

inline fun <reified T : View> Activity.find(id: Int): T = this.findViewById(id) as T

0 comments on commit 34fcba8

Please sign in to comment.