Skip to content

Commit

Permalink
Merge branch 'feature/reifiedFunctions' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzapico committed Apr 22, 2017
2 parents 7c3b53b + 49c83fa commit 88b0e11
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
19 changes: 18 additions & 1 deletion app/src/main/java/xyz/gonzapico/myfirstkotlinapp/Extensions.kt
@@ -1,9 +1,13 @@
package xyz.gonzapico.myfirstkotlinapp

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
Expand All @@ -22,4 +26,17 @@ fun ViewGroup.inflate(@LayoutRes layoutToInflate: Int,
attachToRoot: Boolean = false) = LayoutInflater.from(context).inflate(
layoutToInflate, this, attachToRoot)

fun ImageView.loadUrl(urlToLoad: String) = Picasso.with(context).load(urlToLoad).into(this)
fun ImageView.loadUrl(urlToLoad: String) = Picasso.with(context).load(urlToLoad).into(this)

/***
* To what activity we want to navigate
*/
inline fun <reified T : Activity> Context.startActivity() {
startActivity(Intent(this, T::class.java))
}

inline fun <reified V : View> View.find(@IdRes resId: Int): V = findViewById(resId) as V

inline fun <reified V : View> Activity.find(@IdRes resId: Int): V = findViewById(resId) as V

inline fun <reified V : View> RecyclerView.ViewHolder.find(@IdRes resId: Int): V = itemView.find<V>(resId)
Expand Up @@ -11,7 +11,7 @@ class HomeActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)

val recyclerView = findViewById(R.id.recycler) as RecyclerView
val recyclerView = find<RecyclerView>(R.id.recycler)
recyclerView.adapter = MediaAdapter(fetchMedia())
}

Expand Down
Expand Up @@ -6,6 +6,7 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import org.w3c.dom.Text

/**
* Created by gfernandez on 22/04/17.
Expand All @@ -25,8 +26,8 @@ class MediaAdapter(
mediaItemList.size

class MediaViewHolder(mediaView: View) : RecyclerView.ViewHolder(mediaView) {
val title = mediaView.findViewById(R.id.media_title) as TextView
val thumb = mediaView.findViewById(R.id.media_thumb) as ImageView
val title = find<TextView>(R.id.media_title)
val thumb = find<ImageView>(R.id.media_thumb)

fun bind(item: MediaItem) {
title.text = item.title
Expand Down

0 comments on commit 88b0e11

Please sign in to comment.