Skip to content

Commit

Permalink
Implementato route-service - Fine iterazione 3
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarne committed Feb 1, 2022
1 parent 6d68977 commit fb454ca
Show file tree
Hide file tree
Showing 186 changed files with 8,147 additions and 139 deletions.
9 changes: 6 additions & 3 deletions App/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions App/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'androidx.navigation.safeargs.kotlin'
id 'com.google.gms.google-services'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10'
id 'kotlin-parcelize'
}

apply plugin: 'kotlin-kapt'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CacheControlInterceptor : Interceptor {
val response = chain.proceed(chain.request())

val cacheControl: CacheControl = CacheControl.Builder()
.maxAge(1, TimeUnit.MINUTES) // 1 minutes cache
.maxAge(1, TimeUnit.DAYS)
.build()

return response.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ class ItineraryRepositoryImpl(
) : ItineraryRepository {
override suspend fun get(id: String): Itinerary? {
val response = itineraryAPI.get(id)
return if(response.isSuccessful)
return if (response.isSuccessful)
response.body()
else null
}

override suspend fun getAllInfo(id: String): Itinerary? {
return get(id)?.let {
val set = it.poiSet.map { poi -> getInfo(poi) }.toHashSet()
val set = it.poiSet.map { poi -> getInfo(poi).copy(inRoute = poi.inRoute) }.toHashSet()
it.copy(poiSet = set)
}
}

override suspend fun create(itinerary: Itinerary): Itinerary? {
val response = itineraryAPI.create(itinerary)
return if(response.isSuccessful)
return if (response.isSuccessful)
response.body()
else null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ data class POI(
var name: String? = null,
val lat: Double,
val lon: Double,
var inRoute: Boolean = false,
@Transient var address: Address? = null
) {
override fun hashCode() = Objects.hash(lat, lon)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ class SearchPOIDeserializer : JsonDeserializer<POI> {
val name = properties["name"]?.asString
val coordinates = response.getAsJsonObject("geometry").getAsJsonArray("coordinates")

return POI(name, coordinates[1].asDouble, coordinates[0].asDouble, address)
return POI(name, coordinates[1].asDouble, coordinates[0].asDouble, address = address)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.unibg.magellanus.app.itinerary.model.network

import com.unibg.magellanus.app.common.network.AuthInterceptor
import com.unibg.magellanus.app.common.network.CacheControlInterceptor
import com.unibg.magellanus.app.itinerary.model.Itinerary
import com.unibg.magellanus.app.user.auth.AuthenticationProvider
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.*
import java.io.File

interface ItineraryAPI {
@GET("{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ class ItineraryListFragment : Fragment() {

private val viewModel by viewModels<ItineraryListViewModel> {
val provider = FirebaseAuthenticationProvider
val cacheDir = requireContext().cacheDir
val api = ItineraryAPI.create(provider)
val geoApi = GeocodingAPI.create(cacheDir)
val geoApi = GeocodingAPI.create(requireContext().cacheDir)
val repository = ItineraryRepositoryImpl(api, geoApi)

ItineraryListViewModel.Factory(repository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.unibg.magellanus.app.R
import com.unibg.magellanus.app.common.observeOnce
import com.unibg.magellanus.app.databinding.FragmentPoiListBinding
import com.unibg.magellanus.app.itinerary.model.ItineraryRepositoryImpl
import com.unibg.magellanus.app.itinerary.model.network.GeocodingAPI
import com.unibg.magellanus.app.itinerary.model.network.ItineraryAPI
import com.unibg.magellanus.app.itinerary.viewmodel.POIListViewModel
import com.unibg.magellanus.app.user.auth.impl.FirebaseAuthenticationProvider
import kotlinx.coroutines.launch

class POIListFragment : Fragment() {

Expand All @@ -44,40 +47,74 @@ class POIListFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_poi_list, container, false)
): View {
val binding = FragmentPoiListBinding.inflate(inflater, container, false)

// Set the adapter
if (view is RecyclerView) {
with(view) {
layoutManager = when {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}
with(binding.list) {

layoutManager = when {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}


viewModel.poiList.observeOnce(viewLifecycleOwner) {
val locateClickListener =
POIRecyclerViewAdapter.OnPOIItemClickListener { poi ->
navController.navigate(
POIListFragmentDirections.actionPOIListFragmentToMapFragment(
itineraryId,
floatArrayOf(poi.lat.toFloat(), poi.lon.toFloat())
)
viewModel.poiList.observeOnce(viewLifecycleOwner) {
val locateClickListener =
POIRecyclerViewAdapter.OnPOIItemClickListener { poi ->
navController.navigate(
POIListFragmentDirections.actionPOIListFragmentToMapFragment(
itineraryId,
floatArrayOf(poi.lat.toFloat(), poi.lon.toFloat())
)
}
)
}

adapter = POIRecyclerViewAdapter(locateClickListener).apply {
setPOIList(it)
val checkboxCheckedChangeListener =
POIRecyclerViewAdapter.OnCheckboxCheckedChangeListener { poi, isChecked ->
viewModel.selectPOI(poi, isChecked)
}

adapter = POIRecyclerViewAdapter(
locateClickListener,
checkboxCheckedChangeListener
).apply {
setPOIList(it)
}
}
}
return view

binding.createBtn.setOnClickListener {
val (coordinates, names) = viewModel.getSelectedPOI()

coordinates.forEach { println(it) }

if (coordinates.size < 3) {
Toast.makeText(requireContext(), R.string.no_coordinates_message, Toast.LENGTH_LONG)
.show()
} else {
navController.navigate(
POIListFragmentDirections.actionPOIListFragmentToRoutedPOIListFragment(
itineraryId,
coordinates.toTypedArray(),
names.toTypedArray()
)
)
}
}

return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = findNavController()
}

override fun onStop() {
super.onStop()
requireActivity().lifecycleScope.launch {
viewModel.saveChanges()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package com.unibg.magellanus.app.itinerary.view

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.unibg.magellanus.app.databinding.FragmentPoiItemBinding
import com.unibg.magellanus.app.itinerary.model.POI

class POIRecyclerViewAdapter(
private val locateClickListener: OnPOIItemClickListener
private val locateClickListener: OnPOIItemClickListener,
private val checkboxCheckedChangeListener: OnCheckboxCheckedChangeListener,
) : RecyclerView.Adapter<POIRecyclerViewAdapter.ViewHolder>() {

private var values = mutableListOf<POI>()
Expand All @@ -33,32 +32,45 @@ class POIRecyclerViewAdapter(

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val poi = values[position]
holder.bind(poi, locateClickListener)
holder.bind(poi, position, locateClickListener, checkboxCheckedChangeListener)
}

override fun getItemCount(): Int = values.size

inner class ViewHolder(binding: FragmentPoiItemBinding) :
RecyclerView.ViewHolder(binding.root) {
val name: TextView = binding.name
private val address: TextView = binding.address
private val locateButton: ImageButton = binding.locateBtn
val name = binding.name
private val address = binding.address
private val locateButton = binding.locateBtn
private val selectedCheckBox = binding.selectCheckbox

fun bind(
poi: POI,
position: Int,
itemClickListener: OnPOIItemClickListener,
checkboxCheckedChangeListener: OnCheckboxCheckedChangeListener,
) {
name.text = poi.name
address.text = poi.address?.formatted
selectedCheckBox.isChecked = poi.inRoute

locateButton.setOnClickListener {
itemClickListener.onClick(poi)
}

selectedCheckBox.setOnCheckedChangeListener { _, isChecked ->
checkboxCheckedChangeListener.onCheckedChange(poi, isChecked)
this.bindingAdapter?.notifyItemChanged(position)
}
}
}

fun interface OnPOIItemClickListener {
fun onClick(poi: POI)
}

fun interface OnCheckboxCheckedChangeListener {
fun onCheckedChange(poi: POI, isChecked: Boolean)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.*
import com.unibg.magellanus.app.itinerary.model.Itinerary
import com.unibg.magellanus.app.itinerary.model.ItineraryRepository
import com.unibg.magellanus.app.itinerary.model.POI
import com.unibg.magellanus.app.route.model.Coordinates
import kotlinx.coroutines.launch

class POIListViewModel(itineraryId: String, private val repository: ItineraryRepository) :
Expand All @@ -22,6 +23,24 @@ class POIListViewModel(itineraryId: String, private val repository: ItineraryRep
}
}

fun selectPOI(poi: POI, isChecked: Boolean) {
_currentItinerary.value?.poiSet?.find { it == poi }?.inRoute = isChecked
}

fun getSelectedPOI(): Pair<List<Coordinates>, List<String>> {
_currentItinerary.value!!.poiSet.filter { it.inRoute }.apply {
val coordinates = this.map { Coordinates(it.lat, it.lon) }
val names = this.map { it.name!! }

return Pair(coordinates, names)
}
}

suspend fun saveChanges() {
val itinerary = _currentItinerary.value!!
repository.update(itinerary)
}

class Factory(private val itineraryId: String, private val repository: ItineraryRepository) :
ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T =
Expand Down

This file was deleted.

Loading

0 comments on commit fb454ca

Please sign in to comment.