-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STEP 4 : display a speaker list in recycler view and implement a fact…
…ory in application to create class dependencies
- Loading branch information
Showing
7 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
app/src/main/java/com/devmind/devoxx/model/SpeakerAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters