Move stuff into more granular packages.

This commit is contained in:
Danila Fedorin 2020-05-13 19:11:24 -07:00
parent 1cb22bf4a0
commit e13572ae67
19 changed files with 122 additions and 92 deletions

View File

@ -13,10 +13,10 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".StatisticsActivity"/>
<activity android:name=".CollarDetailActivity" />
<activity android:name=".CollarListActivity" />
<activity android:name=".MainActivity">
<activity android:name=".ui.activities.StatisticsActivity"/>
<activity android:name=".ui.activities.CollarDetailActivity" />
<activity android:name=".ui.activities.CollarListActivity" />
<activity android:name=".ui.activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -1,4 +1,4 @@
package com.danilafe.fencelessgrazing.requests
package com.danilafe.fencelessgrazing.requests.authenticated
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest

View File

@ -1,11 +1,8 @@
package com.danilafe.fencelessgrazing.requests
package com.danilafe.fencelessgrazing.requests.authenticated
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.danilafe.fencelessgrazing.model.CollarDetails
import com.danilafe.fencelessgrazing.model.CollarPos
import com.danilafe.fencelessgrazing.model.CollarSummary
import com.google.gson.reflect.TypeToken
import com.danilafe.fencelessgrazing.requests.toGsonListener
/**
* Request to the `/collars/<id>/details` API endpoint. Retrieves detailed information

View File

@ -1,10 +1,8 @@
package com.danilafe.fencelessgrazing.requests
package com.danilafe.fencelessgrazing.requests.authenticated
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.danilafe.fencelessgrazing.model.CollarPos
import com.danilafe.fencelessgrazing.model.CollarSummary
import com.google.gson.reflect.TypeToken
import com.danilafe.fencelessgrazing.requests.toGsonListener
/**
* A request to the `/collars/<id>/history` API endpoint. Retrieves the entire list

View File

@ -1,9 +1,8 @@
package com.danilafe.fencelessgrazing.requests
package com.danilafe.fencelessgrazing.requests.authenticated
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.danilafe.fencelessgrazing.model.CollarSummary
import com.google.gson.reflect.TypeToken
import com.danilafe.fencelessgrazing.requests.toGsonListener
/**
* A request to the `/collars` API endpoint. Retrieves a list of collar summaries, represented

View File

@ -1,9 +1,8 @@
package com.danilafe.fencelessgrazing.requests
package com.danilafe.fencelessgrazing.requests.authenticated
import com.danilafe.fencelessgrazing.model.CollarDistance
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.google.gson.reflect.TypeToken
import com.danilafe.fencelessgrazing.requests.toGsonListener
/**
* A request to the `/collars/stats/distance` API endpoint. Retrieves

View File

@ -1,4 +1,4 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.activities
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
@ -8,9 +8,10 @@ import android.widget.Toast
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.Volley
import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.model.Polygon
import com.danilafe.fencelessgrazing.requests.CollarDetailRequest
import com.danilafe.fencelessgrazing.requests.CollarHistoryRequest
import com.danilafe.fencelessgrazing.requests.authenticated.CollarDetailRequest
import com.danilafe.fencelessgrazing.requests.authenticated.CollarHistoryRequest
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
@ -166,21 +167,30 @@ class CollarDetailActivity : AppCompatActivity() {
* Sends API requests that retrieve updated information about the collar.
*/
private fun triggerRefresh() {
val historyRequest = CollarHistoryRequest(getString(R.string.apiUrl), collarId, token,
val historyRequest =
CollarHistoryRequest(getString(R.string.apiUrl),
collarId,
token,
Response.Listener {
dataPoints = it.map { p -> GeoPoint(p.longitude.toDouble(), p.latitude.toDouble()) }
dataPoints =
it.map { p -> GeoPoint(p.longitude.toDouble(), p.latitude.toDouble()) }
},
Response.ErrorListener {
Toast.makeText(this, "Failed to retrieve history of collar", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Failed to retrieve history of collar", Toast.LENGTH_SHORT)
.show()
}
)
val detailRequest = CollarDetailRequest(getString(R.string.apiUrl), collarId, token,
val detailRequest =
CollarDetailRequest(getString(R.string.apiUrl),
collarId,
token,
Response.Listener {
collarName.text = it.name
collarStimulus.text = getString(R.string.collarSummaryStimulus, it.stimulus)
},
Response.ErrorListener {
Toast.makeText(this, "Failed to retrieve details of collar", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Failed to retrieve details of collar", Toast.LENGTH_SHORT)
.show()
}
)
queue.add(historyRequest)

View File

@ -1,4 +1,4 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.activities
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
@ -12,8 +12,11 @@ import androidx.recyclerview.widget.RecyclerView
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.Volley
import com.danilafe.fencelessgrazing.ui.components.CollarClickListener
import com.danilafe.fencelessgrazing.ui.components.CollarSummaryAdapter
import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.model.CollarSummary
import com.danilafe.fencelessgrazing.requests.CollarRequest
import com.danilafe.fencelessgrazing.requests.authenticated.CollarRequest
import org.osmdroid.config.Configuration
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.util.GeoPoint
@ -116,7 +119,9 @@ class CollarListActivity : AppCompatActivity() {
* Sends the request to the API to retrieve an updated list of active collars.
*/
private fun triggerRefresh() {
val request = CollarRequest(getString(R.string.apiUrl), token,
val request =
CollarRequest(getString(R.string.apiUrl),
token,
Response.Listener {
summaries.clear()
summaries.addAll(it)
@ -124,7 +129,8 @@ class CollarListActivity : AppCompatActivity() {
updateMap()
},
Response.ErrorListener {
Toast.makeText(this, "Failed to retrieve collar list!", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Failed to retrieve collar list!", Toast.LENGTH_SHORT)
.show()
}
)
queue.add(request)
@ -158,7 +164,11 @@ class CollarListActivity : AppCompatActivity() {
*/
private fun setupCollarList() {
val layoutManager = LinearLayoutManager(collarList.context)
summaryAdapter = CollarSummaryAdapter(summaries, object : CollarClickListener {
summaryAdapter =
CollarSummaryAdapter(
summaries,
object :
CollarClickListener {
override fun onCollarClick(collar: CollarSummary?) {
if (collar == null) return
startCollarDetailActivity(collar)

View File

@ -1,4 +1,4 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.activities
import android.os.Bundle
import android.view.LayoutInflater
@ -9,8 +9,9 @@ import androidx.fragment.app.Fragment
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.Volley
import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.model.CollarDistance
import com.danilafe.fencelessgrazing.requests.DistanceTraveledRequest
import com.danilafe.fencelessgrazing.requests.authenticated.DistanceTraveledRequest
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
@ -61,12 +62,18 @@ class DistanceTraveledGraph() : Fragment() {
val activity = requireActivity()
val sharedPrefs = activity.getSharedPreferences("FencelessGrazing", 0)
val token = sharedPrefs.getString("token", null)!!
val request = DistanceTraveledRequest(activity.getString(R.string.apiUrl), token,
val request =
DistanceTraveledRequest(activity.getString(R.string.apiUrl),
token,
Response.Listener {
updateChartData(it)
},
Response.ErrorListener {
Toast.makeText(activity, "Failed to retrieve distance traveled!", Toast.LENGTH_SHORT).show()
Toast.makeText(
activity,
"Failed to retrieve distance traveled!",
Toast.LENGTH_SHORT
).show()
}
)
queue.add(request)

View File

@ -1,4 +1,4 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.activities
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
@ -8,6 +8,7 @@ import android.widget.TextView
import android.widget.Toast
import com.android.volley.Response
import com.android.volley.toolbox.Volley
import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.requests.LoginRequest
/**

View File

@ -1,10 +1,10 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.activities
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.ui.components.StatisticsGraphAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
@ -37,7 +37,10 @@ class StatisticsActivity : AppCompatActivity() {
tabLayout = findViewById(R.id.statisticsTabs)
viewPager = findViewById(R.id.statisticsPager)
viewPager.adapter = StatisticsGraphAdapter(this)
viewPager.adapter =
StatisticsGraphAdapter(
this
)
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
tab.text = tabNames[position]
}.attach()

View File

@ -1,4 +1,4 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.components
import com.danilafe.fencelessgrazing.model.CollarSummary

View File

@ -1,15 +1,12 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.components
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.DiffUtil
import com.android.volley.Response
import com.android.volley.toolbox.Volley
import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.model.CollarSummary
import com.danilafe.fencelessgrazing.requests.CollarRequest
import com.danilafe.fencelessgrazing.requests.authenticated.CollarRequest
/**
* A [ListAdapter] subclass to display and manage a list of [CollarSummary] items
@ -21,7 +18,9 @@ import com.danilafe.fencelessgrazing.requests.CollarRequest
class CollarSummaryAdapter(
private val items: List<CollarSummary>,
private val collarClickListener: CollarClickListener
) : ListAdapter<CollarSummary, CollarViewHolder>(DiffCallback()) {
) : ListAdapter<CollarSummary, CollarViewHolder>(
DiffCallback()
) {
/**
* [DiffUtil.ItemCallback] used for the [ListAdapter]. Compares items using
@ -37,14 +36,17 @@ class CollarSummaryAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CollarViewHolder {
val layout = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
return CollarViewHolder(layout)
return CollarViewHolder(
layout
)
}
override fun onBindViewHolder(holder: CollarViewHolder, position: Int) {
holder.bindData(getItem(position), collarClickListener)
}
override fun getItemViewType(position: Int): Int = R.layout.collar_summary_layout
override fun getItemViewType(position: Int): Int =
R.layout.collar_summary_layout
override fun getItem(position: Int): CollarSummary = items[position]

View File

@ -1,8 +1,9 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.components
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.model.CollarSummary
/**
@ -28,7 +29,8 @@ class CollarViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
*/
fun bindData(summary: CollarSummary, collarClickListener: CollarClickListener) {
nameView.text = summary.name
positionView.text = itemView.resources.getString(R.string.collarSummaryLocation,
positionView.text = itemView.resources.getString(
R.string.collarSummaryLocation,
summary.pos.longitude.toDouble(), summary.pos.latitude.toDouble())
itemView.setOnClickListener {
collarClickListener.onCollarClick(summary)

View File

@ -1,13 +1,15 @@
package com.danilafe.fencelessgrazing
package com.danilafe.fencelessgrazing.ui.components
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.danilafe.fencelessgrazing.ui.activities.DistanceTraveledGraph
/**
* Adapter for the [ViewPager2][androidx.viewpager2.widget.ViewPager2] class.
*/
class StatisticsGraphAdapter(activity : FragmentActivity) : FragmentStateAdapter(activity) {
override fun getItemCount(): Int = 1
override fun createFragment(position: Int): Fragment = DistanceTraveledGraph()
override fun createFragment(position: Int): Fragment =
DistanceTraveledGraph()
}

View File

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CollarDetailActivity">
tools:context=".ui.activities.CollarDetailActivity">
<org.osmdroid.views.MapView
android:id="@+id/detailMap"
android:layout_width="match_parent"

View File

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CollarListActivity">
tools:context=".ui.activities.CollarListActivity">
<org.osmdroid.views.MapView
android:id="@+id/map"

View File

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context=".ui.activities.MainActivity">
<EditText
android:id="@+id/username"

View File

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StatisticsActivity">
tools:context=".ui.activities.StatisticsActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/statisticsTabs"