From 9983753912e765f417e3d1a43cd5c0692cccc834 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 12 May 2020 00:06:58 -0700 Subject: [PATCH] Start work on collar details. --- app/src/main/AndroidManifest.xml | 5 +++-- .../fencelessgrazing/CollarClickListener.kt | 7 +++++++ .../fencelessgrazing/CollarDetailActivity.kt | 18 ++++++++++++++++++ .../fencelessgrazing/CollarListActivity.kt | 19 +++++++++++++++++-- .../fencelessgrazing/CollarSummaryAdapter.kt | 5 +++-- .../fencelessgrazing/CollarViewHolder.kt | 5 ++++- .../res/layout/activity_collar_detail.xml | 9 +++++++++ 7 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/com/danilafe/fencelessgrazing/CollarClickListener.kt create mode 100644 app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt create mode 100644 app/src/main/res/layout/activity_collar_detail.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0d7fc6e..a983e9c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,7 +3,7 @@ package="com.danilafe.fencelessgrazing"> - + - + + diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/CollarClickListener.kt b/app/src/main/java/com/danilafe/fencelessgrazing/CollarClickListener.kt new file mode 100644 index 0000000..0b1e8b6 --- /dev/null +++ b/app/src/main/java/com/danilafe/fencelessgrazing/CollarClickListener.kt @@ -0,0 +1,7 @@ +package com.danilafe.fencelessgrazing + +import com.danilafe.fencelessgrazing.model.CollarSummary + +interface CollarClickListener { + fun onCollarClick(collar: CollarSummary?) +} \ No newline at end of file diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt b/app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt new file mode 100644 index 0000000..e4d2962 --- /dev/null +++ b/app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt @@ -0,0 +1,18 @@ +package com.danilafe.fencelessgrazing + +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle + +class CollarDetailActivity : AppCompatActivity() { + + private lateinit var token: String + private var collarId: Int = -1 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_collar_detail) + + token = intent.getStringExtra("token")!! + collarId = intent.getIntExtra("identifier", -1) + } +} diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/CollarListActivity.kt b/app/src/main/java/com/danilafe/fencelessgrazing/CollarListActivity.kt index d56e770..e2d6055 100644 --- a/app/src/main/java/com/danilafe/fencelessgrazing/CollarListActivity.kt +++ b/app/src/main/java/com/danilafe/fencelessgrazing/CollarListActivity.kt @@ -1,5 +1,6 @@ package com.danilafe.fencelessgrazing +import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Toast @@ -36,7 +37,7 @@ class CollarListActivity : AppCompatActivity() { private val collarOverlays: MutableMap = mutableMapOf() // Timer used to schedule refresh - private val refreshTimer = Timer() + private var refreshTimer = Timer() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -57,6 +58,7 @@ class CollarListActivity : AppCompatActivity() { override fun onResume() { super.onResume() map.onResume() + refreshTimer = Timer() refreshTimer.schedule(timerTask { triggerRefresh() }, 0L, 5000L) } @@ -86,9 +88,22 @@ class CollarListActivity : AppCompatActivity() { map = findViewById(R.id.map) } + private fun startCollarDetailActivity(collar: CollarSummary) { + val newIntent = Intent(this, CollarDetailActivity::class.java).apply { + putExtra("token", token) + putExtra("identifier", collar.id) + } + startActivity(newIntent) + } + private fun setupCollarList() { val layoutManager = LinearLayoutManager(collarList.context) - summaryAdapter = CollarSummaryAdapter(summaries) + summaryAdapter = CollarSummaryAdapter(summaries, object : CollarClickListener { + override fun onCollarClick(collar: CollarSummary?) { + if (collar == null) return + startCollarDetailActivity(collar) + } + }) collarList.adapter = summaryAdapter collarList.layoutManager = layoutManager collarList.addItemDecoration(DividerItemDecoration(collarList.context, layoutManager.orientation)) diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/CollarSummaryAdapter.kt b/app/src/main/java/com/danilafe/fencelessgrazing/CollarSummaryAdapter.kt index 9611a8f..ccc82a6 100644 --- a/app/src/main/java/com/danilafe/fencelessgrazing/CollarSummaryAdapter.kt +++ b/app/src/main/java/com/danilafe/fencelessgrazing/CollarSummaryAdapter.kt @@ -12,7 +12,8 @@ import com.danilafe.fencelessgrazing.model.CollarSummary import com.danilafe.fencelessgrazing.requests.CollarRequest class CollarSummaryAdapter( - private val items : List + private val items: List, + private val collarClickListener: CollarClickListener ) : ListAdapter(DiffCallback()) { class DiffCallback : DiffUtil.ItemCallback() { @@ -29,7 +30,7 @@ class CollarSummaryAdapter( } override fun onBindViewHolder(holder: CollarViewHolder, position: Int) { - holder.bindData(getItem(position)) + holder.bindData(getItem(position), collarClickListener) } override fun getItemViewType(position: Int): Int = R.layout.collar_summary_layout diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/CollarViewHolder.kt b/app/src/main/java/com/danilafe/fencelessgrazing/CollarViewHolder.kt index fac8a10..8c67032 100644 --- a/app/src/main/java/com/danilafe/fencelessgrazing/CollarViewHolder.kt +++ b/app/src/main/java/com/danilafe/fencelessgrazing/CollarViewHolder.kt @@ -9,9 +9,12 @@ class CollarViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private val nameView: TextView = itemView.findViewById(R.id.collarSummaryName) private val positionView: TextView = itemView.findViewById(R.id.collarSummaryPos) - fun bindData(summary: CollarSummary) { + fun bindData(summary: CollarSummary, collarClickListener: CollarClickListener) { nameView.text = summary.name // TODO figure out how to get getString here. positionView.text = "Currently at ${summary.pos.longitude}, ${summary.pos.latitude}" + itemView.setOnClickListener { + collarClickListener.onCollarClick(summary) + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_collar_detail.xml b/app/src/main/res/layout/activity_collar_detail.xml new file mode 100644 index 0000000..5da2fce --- /dev/null +++ b/app/src/main/res/layout/activity_collar_detail.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file