Start work on collar details.

This commit is contained in:
Danila Fedorin 2020-05-12 00:06:58 -07:00
parent c6584e4eaa
commit 9983753912
7 changed files with 61 additions and 7 deletions

View File

@ -3,7 +3,7 @@
package="com.danilafe.fencelessgrazing">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
@ -13,7 +13,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CollarListActivity"></activity>
<activity android:name=".CollarDetailActivity"></activity>
<activity android:name=".CollarListActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -0,0 +1,7 @@
package com.danilafe.fencelessgrazing
import com.danilafe.fencelessgrazing.model.CollarSummary
interface CollarClickListener {
fun onCollarClick(collar: CollarSummary?)
}

View File

@ -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)
}
}

View File

@ -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<Int, Marker> = 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))

View File

@ -12,7 +12,8 @@ import com.danilafe.fencelessgrazing.model.CollarSummary
import com.danilafe.fencelessgrazing.requests.CollarRequest
class CollarSummaryAdapter(
private val items : List<CollarSummary>
private val items: List<CollarSummary>,
private val collarClickListener: CollarClickListener
) : ListAdapter<CollarSummary, CollarViewHolder>(DiffCallback()) {
class DiffCallback : DiffUtil.ItemCallback<CollarSummary>() {
@ -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

View File

@ -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)
}
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CollarDetailActivity">
</androidx.constraintlayout.widget.ConstraintLayout>