From ee1887d6142606159149e1cc452467a0d6a36712 Mon Sep 17 00:00:00 2001 From: Guillaume EHRET Date: Wed, 3 Apr 2019 09:07:27 +0200 Subject: [PATCH] STEP 5 : speaker detail to update a create a new one --- .../com/devmind/devoxx/SpeakerActivity.kt | 50 +++++++++++++++++++ .../com/devmind/devoxx/SpeakerListActivity.kt | 22 +++++++- .../devmind/devoxx/model/SpeakerAdapter.kt | 10 +++- app/src/main/res/layout/activity_speaker.xml | 34 +++++++++++++ .../main/res/layout/activity_speaker_list.xml | 11 +++- 5 files changed, 122 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt b/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt index 8e5b498..ea26abd 100644 --- a/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt +++ b/app/src/main/java/com/devmind/devoxx/SpeakerActivity.kt @@ -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() } } diff --git a/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt b/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt index 5ba5634..0a91aaf 100644 --- a/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt +++ b/app/src/main/java/com/devmind/devoxx/SpeakerListActivity.kt @@ -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) @@ -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() } } diff --git a/app/src/main/java/com/devmind/devoxx/model/SpeakerAdapter.kt b/app/src/main/java/com/devmind/devoxx/model/SpeakerAdapter.kt index f5eb5f6..674d8af 100644 --- a/app/src/main/java/com/devmind/devoxx/model/SpeakerAdapter.kt +++ b/app/src/main/java/com/devmind/devoxx/model/SpeakerAdapter.kt @@ -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(){ +class SpeakerAdapter(val listener: SpeakerSelectionListener): RecyclerView.Adapter(){ private val speakers = mutableListOf() @@ -33,9 +34,16 @@ class SpeakerAdapter: RecyclerView.Adapter(){ 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) + } } diff --git a/app/src/main/res/layout/activity_speaker.xml b/app/src/main/res/layout/activity_speaker.xml index 5129422..32b864a 100644 --- a/app/src/main/res/layout/activity_speaker.xml +++ b/app/src/main/res/layout/activity_speaker.xml @@ -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"/> + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_speaker_list.xml b/app/src/main/res/layout/activity_speaker_list.xml index 189f466..026d51d 100644 --- a/app/src/main/res/layout/activity_speaker_list.xml +++ b/app/src/main/res/layout/activity_speaker_list.xml @@ -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"/> + \ No newline at end of file