Skip to content

Commit

Permalink
refactor some code for best pratices
Browse files Browse the repository at this point in the history
  • Loading branch information
isfaaghyth committed Sep 18, 2018
1 parent 1cabfc0 commit 60fedf0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
compile 'com.android.support:appcompat-v7:27.+'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:appcompat-v7:27.0.0'
compile 'com.android.support:design:27.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

compile "org.jetbrains.anko:anko:$anko_version"
compile "org.jetbrains.anko:anko-design:$anko_version"
compile "org.jetbrains.anko:anko-commons:$anko_version"

testCompile 'junit:junit:4.12'
}

repositories {
mavenCentral()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import isfaaghyth.app.fotballclub.data.model.Club
* Created by isfaaghyth on 9/17/18.
* github: @isfaaghyth
*/
class ClubManager {
object ClubManager {

companion object {
fun get(context: Context) : ArrayList<Club> {
val clubs = ArrayList<Club>()
clubs.add(Club(R.drawable.madrid, context.getString(R.string.club_madrid), context.getString(R.string.madrid_desc)))
clubs.add(Club(R.drawable.barcelona, context.getString(R.string.club_barcelona), context.getString(R.string.barcelona_desc)))
clubs.add(Club(R.drawable.juventus, context.getString(R.string.club_juventus), context.getString(R.string.juventus_desc)))
clubs.add(Club(R.drawable.bayern, context.getString(R.string.club_bayern), context.getString(R.string.bayern_desc)))
clubs.add(Club(R.drawable.chelsea, context.getString(R.string.club_chelsea), context.getString(R.string.chelsea_desc)))
return clubs
}
fun get(context: Context) : ArrayList<Club> {
val clubs = ArrayList<Club>()
clubs.add(Club(R.drawable.madrid, context.getString(R.string.club_madrid), context.getString(R.string.madrid_desc)))
clubs.add(Club(R.drawable.barcelona, context.getString(R.string.club_barcelona), context.getString(R.string.barcelona_desc)))
clubs.add(Club(R.drawable.juventus, context.getString(R.string.club_juventus), context.getString(R.string.juventus_desc)))
clubs.add(Club(R.drawable.bayern, context.getString(R.string.club_bayern), context.getString(R.string.bayern_desc)))
clubs.add(Club(R.drawable.chelsea, context.getString(R.string.club_chelsea), context.getString(R.string.chelsea_desc)))
return clubs
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,23 @@ import org.jetbrains.anko.imageResource
class FootballAdapter(private val clubs: ArrayList<Club> = arrayListOf(), private val listener: FootballListener)
: RecyclerView.Adapter<FootballAdapter.Holder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
Holder(FootballComponent(parent.context).createView(AnkoContext.create(parent.context, parent)))
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : Holder {
val viewHolder = Holder(FootballComponent(parent.context).createView(AnkoContext.create(parent.context, parent)))
viewHolder.itemView.setOnClickListener {
val position = viewHolder.adapterPosition
listener.onClicked(clubs[position])
}
return viewHolder
}

override fun onBindViewHolder(holder: Holder, position: Int) {
holder.imgClub.imageResource = clubs[position].icon
holder.txtClubName.text = clubs[position].name
holder.itemClub.setOnClickListener {
listener.onClicked(clubs[position])
}
}

override fun getItemCount(): Int = clubs.size

inner class Holder(view: View): RecyclerView.ViewHolder(view) {
var itemClub: LinearLayout = view.findViewById(FootballComponent.itemId)
var imgClub: ImageView = view.findViewById(FootballComponent.imgClubId)
var txtClubName: TextView = view.findViewById(FootballComponent.txtClubNameId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.jetbrains.anko.*
class FootballComponent(val context: Context) : AnkoComponent<ViewGroup> {

companion object {
val itemId = 1
val imgClubId = 2
val txtClubNameId = 3
}
Expand All @@ -23,7 +22,6 @@ class FootballComponent(val context: Context) : AnkoComponent<ViewGroup> {
linearLayout {
lparams(width = matchParent)

id = itemId
isClickable = true
gravity = Gravity.CENTER_VERTICAL
orientation = LinearLayout.HORIZONTAL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package isfaaghyth.app.fotballclub.ui.main
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.widget.Toast
import isfaaghyth.app.fotballclub.BuildConfig
import isfaaghyth.app.fotballclub.R
import isfaaghyth.app.fotballclub.data.local.ClubManager
import isfaaghyth.app.fotballclub.data.model.Club
import isfaaghyth.app.fotballclub.ui.detail.DetailActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.toast
import org.jetbrains.anko.*

class MainActivity : AppCompatActivity() {

Expand All @@ -26,7 +22,7 @@ class MainActivity : AppCompatActivity() {
val clubsRepository = ClubManager.get(this)
lstFootball.layoutManager = LinearLayoutManager(this)
lstFootball.adapter = FootballAdapter(clubsRepository, object : FootballListener {
override fun onClicked(club: Club) = startActivity(intentFor<DetailActivity>("club" to club))
override fun onClicked(club: Club) = startActivity<DetailActivity>("club" to club)
})
}

Expand Down
Binary file added football_01.zip
Binary file not shown.

0 comments on commit 60fedf0

Please sign in to comment.