Skip to content

Commit

Permalink
STEP 4 : display a speaker list in recycler view and implement a fact…
Browse files Browse the repository at this point in the history
…ory in application to create class dependencies
  • Loading branch information
javamind committed Apr 2, 2019
1 parent 5680a25 commit 2856a22
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
package="com.devmind.devoxx">

<application
android:name=".DevoxxApplication"
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:roundIcon="@drawable/logo"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SpeakerListActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/>
</activity>
<activity android:name=".SpeakerActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/devmind/devoxx/DevoxxApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.devmind.devoxx

import android.app.Application
import androidx.appcompat.app.AppCompatActivity
import com.devmind.devoxx.model.SpeakerDao
import com.devmind.devoxx.model.SpeakerService

class DevoxxApplication:Application(){

val speakerService by lazy{
SpeakerService()
}

fun speakerDao() = SpeakerDao(speakerService)
}

val AppCompatActivity.devoxxApplication
get() = this.application as DevoxxApplication
14 changes: 14 additions & 0 deletions app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package com.devmind.devoxx

import android.os.Bundle
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.devmind.devoxx.model.SpeakerAdapter
import kotlinx.android.synthetic.main.activity_speaker_list.*

class SpeakerListActivity : MainActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_speaker_list)

val speakerDao = devoxxApplication.speakerDao()

speakerList.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
adapter = SpeakerAdapter()
(adapter as SpeakerAdapter).updateData(speakerDao.readAll())
}
}
}
53 changes: 53 additions & 0 deletions app/src/main/java/com/devmind/devoxx/model/Speaker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.devmind.devoxx.model

import java.util.*


data class Speaker(
val firstname: String,
val lastname: String,
val country: String = "France",
val uuid: String = UUID.randomUUID().toString()
)


class SpeakerService {
val speakers = mutableListOf(Speaker("Guillaume", "EHRET"))
}

interface DaoCrud<T> {
// Create
fun create(element: T)

// Read
fun readAll(): List<T>

fun readOne(id: String): T

// Update
fun update(element: T)

// Delete
fun delete(element: T)
}

class SpeakerDao(private val speakerService: SpeakerService) : DaoCrud<Speaker> {

override fun create(element: Speaker) {
speakerService.speakers.add(element)
}

override fun readAll() = speakerService.speakers

override fun readOne(id: String): Speaker = speakerService.speakers.first { it.uuid == id }

override fun update(element: Speaker) {
delete(readOne(element.uuid))
create(element)
}

override fun delete(element: Speaker) {
speakerService.speakers.removeAll { it.uuid == element.uuid }
}

}
41 changes: 41 additions & 0 deletions app/src/main/java/com/devmind/devoxx/model/SpeakerAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.devmind.devoxx.model

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.devmind.devoxx.R

class SpeakerAdapter: RecyclerView.Adapter<SpeakerAdapter.ViewHolder>(){

private val speakers = mutableListOf<Speaker>()

class ViewHolder(view: ConstraintLayout): RecyclerView.ViewHolder(view){
val name = view.findViewById(R.id.speakerName) as TextView
val country = view.findViewById(R.id.speakerCountry) as TextView
}

fun updateData(speakers: List<Speaker>){
this.speakers.clear()
this.speakers.addAll(speakers)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.activity_speaker_item, parent, false)
return ViewHolder(view as ConstraintLayout)
}

override fun getItemCount(): Int = speakers.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val speaker = speakers[position]
holder.apply {
name.text = "${speaker.firstname} ${speaker.lastname}"
country.text = speaker.country
}
}

}


25 changes: 25 additions & 0 deletions app/src/main/res/layout/activity_speaker_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".SpeakerActivity" android:padding="8dp">
<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/speakerName" android:textStyle="bold" android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:text="TextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
android:id="@+id/speakerCountry"
app:layout_constraintTop_toBottomOf="@+id/speakerName" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:textAllCaps="true"/>
</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_speaker_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:id="@+id/title3"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/title3" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp"
android:id="@+id/speakerList"/>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 2856a22

Please sign in to comment.