Skip to content

Commit

Permalink
STEP 5 : speaker detail to update a create a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
javamind committed Apr 3, 2019
1 parent 2856a22 commit ee1887d
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 5 deletions.
50 changes: 50 additions & 0 deletions app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
package com.devmind.devoxx

import android.content.Intent
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import com.devmind.devoxx.model.Speaker
import kotlinx.android.synthetic.main.activity_speaker.*
import java.util.*

class SpeakerActivity : MainActivity() {

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

val speakerDao = devoxxApplication.speakerDao()
val speakerUiid = intent.getStringExtra("ID")

speakerCountry.apply {
val countries = Locale.getISOCountries().map { Locale("", it) }.map { it.getDisplayCountry() }
setAdapter(ArrayAdapter(baseContext, android.R.layout.simple_dropdown_item_1line, countries))
}

if(!speakerUiid.isNullOrBlank()){
val speaker = speakerDao.readOne(speakerUiid)
speaker.apply {
speakerFirstname.setText(speaker.firstname)
speakerLastname.setText(speaker.lastname)
speakerCountry.setText(speaker.country)
}
}

buttonSpeakerSave.setOnClickListener {
if (speakerFirstname.text.isNullOrBlank() || speakerLastname.text.isNullOrBlank()) {
Toast.makeText(applicationContext, R.string.speaker_error_required, Toast.LENGTH_LONG).show()
} else {
if (speakerUiid.isNullOrBlank()) {
speakerDao.create(Speaker(
speakerFirstname.text.toString(),
speakerLastname.text.toString(),
speakerCountry.text.toString()))

} else {
speakerDao.update(Speaker(
speakerFirstname.text.toString(),
speakerLastname.text.toString(),
speakerCountry.text.toString(),
speakerUiid))
}
startActivity(Intent(applicationContext, SpeakerListActivity::class.java))
}
}
}


override fun onStop() {
buttonSpeakerSave.setOnClickListener(null)
super.onStop()
}
}
22 changes: 20 additions & 2 deletions app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.devmind.devoxx

import android.content.Intent
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() {
interface SpeakerSelectionListener{
fun onSpeakerSelect(id: String)
}

class SpeakerListActivity : MainActivity(), SpeakerSelectionListener {

override fun onSpeakerSelect(id: String) {
startActivity(Intent(applicationContext, SpeakerActivity::class.java).putExtra("ID", id))
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -18,8 +27,17 @@ class SpeakerListActivity : MainActivity() {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
adapter = SpeakerAdapter()
adapter = SpeakerAdapter(this@SpeakerListActivity)
(adapter as SpeakerAdapter).updateData(speakerDao.readAll())
}

buttonAddSpeaker.setOnClickListener {
onSpeakerSelect("")
}
}

override fun onStop() {
buttonAddSpeaker.setOnClickListener(null)
super.onStop()
}
}
10 changes: 9 additions & 1 deletion app/src/main/java/com/devmind/devoxx/model/SpeakerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.devmind.devoxx.R
import com.devmind.devoxx.SpeakerSelectionListener

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

private val speakers = mutableListOf<Speaker>()

Expand All @@ -33,9 +34,16 @@ class SpeakerAdapter: RecyclerView.Adapter<SpeakerAdapter.ViewHolder>(){
holder.apply {
name.text = "${speaker.firstname} ${speaker.lastname}"
country.text = speaker.country
itemView.setOnClickListener {
listener.onSpeakerSelect(speaker.uuid)
}
}
}

override fun onViewRecycled(holder: ViewHolder) {
holder.itemView.setOnClickListener(null)
super.onViewRecycled(holder)
}
}


34 changes: 34 additions & 0 deletions app/src/main/res/layout/activity_speaker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,38 @@
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/title2"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/speakerFirstname" android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/title2" android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:hint="@string/speaker_firstname"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/speakerLastname"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent" android:hint="@string/speaker_lastname"
app:layout_constraintHorizontal_bias="0.0" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/speakerFirstname"/>
<AutoCompleteTextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/speakerCountry" app:layout_constraintHorizontal_bias="0.185"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/speakerLastname"
android:hint="@string/speaker_country" android:layout_marginEnd="8dp" android:layout_marginStart="8dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" app:srcCompat="@drawable/ic_save_white_48dp"
android:id="@+id/buttonSpeakerSave" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_speaker_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
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"/>
android:id="@+id/speakerList" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/buttonAddSpeaker"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" app:srcCompat="@drawable/ic_add_white_48dp"
android:id="@+id/buttonAddSpeaker" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="16dp" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit ee1887d

Please sign in to comment.