Skip to content

Commit

Permalink
Fix some bugs when rendering tracks on the main map
Browse files Browse the repository at this point in the history
rcgroot committed Mar 8, 2017
1 parent a2f57f2 commit d035b77
Showing 5 changed files with 150 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -49,16 +49,16 @@ class TrackMapFragment : Fragment() {
val binding = DataBindingUtil.inflate<FragmentMapBinding>(inflater, R.layout.fragment_map, container, false)
binding.fragmentMapMapview.onCreate(savedInstanceState)
binding.viewModel = viewModel
binding.fragmentMapMapview.getMapAsync(trackPresenter)
this.binding = binding

return binding.root
}

override fun onStart() {
super.onStart()
permissionRequester.checkPermissions(activity) { trackPresenter.start(activity) }
binding!!.fragmentMapMapview?.onStart()
binding!!.fragmentMapMapview.getMapAsync(trackPresenter)
permissionRequester.checkPermissions(activity, { trackPresenter.start(activity) })
}

override fun onResume() {
Original file line number Diff line number Diff line change
@@ -42,14 +42,13 @@ import nl.sogeti.android.gpstracker.ng.common.controllers.ContentControllerProvi
import nl.sogeti.android.gpstracker.ng.map.rendering.TrackTileProvider
import nl.sogeti.android.gpstracker.ng.model.TrackSelection
import nl.sogeti.android.gpstracker.ng.utils.*
import java.lang.ref.WeakReference
import javax.inject.Inject

class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedServicePresenter(), OnMapReadyCallback, ContentController.ContentListener, TrackSelection.Listener {
private var executingReader: TrackReader? = null

private var contentController: ContentController? = null
private var weakGoogleMap = WeakReference<GoogleMap?>(null)
private var googleMap: GoogleMap? = null

@Inject
lateinit var trackSelection: TrackSelection
@@ -74,6 +73,7 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer
trackSelection.removeListener(this)
contentController?.unregisterObserver()
contentController = null
googleMap = null
}

//region Track selection
@@ -126,12 +126,12 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer
/* Google Map Tiles */

override fun onMapReady(googleMap: GoogleMap) {
weakGoogleMap = WeakReference(googleMap)
this.googleMap = googleMap
addTilesToMap()
}

private fun addTilesToMap() {
val googleMap = weakGoogleMap.get()
val googleMap = googleMap
val context = this.context
if (googleMap != null && context != null) {
val tileProvider = TrackTileProvider(context, viewModel.waypoints)
@@ -159,7 +159,7 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer
}
}

private inner class TrackReader internal constructor(val trackUri: Uri, private val viewModel: TrackMapViewModel)
inner class TrackReader internal constructor(val trackUri: Uri, private val viewModel: TrackMapViewModel)
: AsyncTask<Void, Void, Void>() {

val handler = DefaultResultHandler()
@@ -168,20 +168,21 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer
context?.let {
trackUri.readTrack(it, handler, null)
}
viewModel.name.set(handler.name)
var builder = handler.headBuilder
if (builder != null) {
viewModel.trackHeadBounds.set(builder.build())
}
builder = handler.boundsBuilder
if (builder != null) {
viewModel.completeBounds.set(builder.build())
}
viewModel.waypoints.set(handler.waypoints.map { it.map { it.latLng } })

updateViewModelWithHandler(handler)

return null
}

fun updateViewModelWithHandler(handler: DefaultResultHandler) {
handler.headBuilder?.let { viewModel.trackHeadBounds.set(it.build()) }
handler.boundsBuilder?.let { viewModel.completeBounds.set(it.build()) }
viewModel.name.set(handler.name)
val points = handler.waypoints.map { it.map { it.latLng } }
val filteredPoints = points.filter { it.count() > 1 }
viewModel.waypoints.set(filteredPoints)
}

override fun onPostExecute(result: Void?) {
if (executingReader == this) {
executingReader = null
Original file line number Diff line number Diff line change
@@ -38,12 +38,12 @@ import nl.sogeti.android.gpstracker.v2.R

class TrackViewModel(val uri: Uri) {

val name = ObservableField<String>()
val name = ObservableField<String>("")
val iconType = ObservableInt(R.drawable.ic_track_type_default)
val startDay = ObservableField<String>("--")
val duration = ObservableField<String>("--")
val distance = ObservableField<String>("--")
val completeBounds = ObservableField<LatLngBounds?>()
val waypoints = ObservableField<List<List<LatLng>>>(listOf())
var polylines = ObservableField<List<PolylineOptions>>()
var polylines = ObservableField<List<PolylineOptions>?>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*------------------------------------------------------------------------------
** Ident: Sogeti Smart Mobile Solutions
** Author: rene
** Copyright: (c) 2017 Sogeti Nederland B.V. All Rights Reserved.
**------------------------------------------------------------------------------
** Sogeti Nederland B.V. | No part of this file may be reproduced
** Distributed Software Engineering | or transmitted in any form or by any
** Lange Dreef 17 | means, electronic or mechanical, for the
** 4131 NJ Vianen | purpose, without the express written
** The Netherlands | permission of the copyright holder.
*------------------------------------------------------------------------------
*
* This file is part of OpenGPSTracker.
*
* OpenGPSTracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenGPSTracker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenGPSTracker. If not, see <http://www.gnu.org/licenses/>.
*
*/
package nl.sogeti.android.gpstracker.ng.map

import android.net.Uri
import nl.sogeti.android.gpstracker.ng.rules.MockAppComponentTestRule
import nl.sogeti.android.gpstracker.ng.utils.DefaultResultHandler
import nl.sogeti.android.gpstracker.ng.utils.Waypoint
import org.hamcrest.CoreMatchers.`is`
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.junit.MockitoJUnit

class TrackMapPresenterTest {

@get:Rule
var mockitoRule = MockitoJUnit.rule()
@get:Rule
var appComponentRule = MockAppComponentTestRule()
@Mock
lateinit var handler: DefaultResultHandler
lateinit var sut: TrackMapPresenter
lateinit var viewModel: TrackMapViewModel

@Before
fun setup() {
viewModel = TrackMapViewModel()
sut = TrackMapPresenter(viewModel)
}

@Test
fun testSetName() {
// Arrange
val uri = mock(Uri::class.java)
val reader = sut.TrackReader(uri, viewModel)
`when`(handler.name).thenReturn("Input")
// Act
reader.updateViewModelWithHandler(handler)
// Assert
Assert.assertThat(viewModel.name.get(), `is`("Input"))
}

@Test
fun testSetSingleListOfWaypoints() {
// Arrange
val uri = mock(Uri::class.java)
val reader = sut.TrackReader(uri, viewModel)
val waypoint = Waypoint(latitude = 1.2, longitude = 3.4, time = 1)
`when`(handler.waypoints).thenReturn(mutableListOf(mutableListOf(waypoint, waypoint)))
// Act
reader.updateViewModelWithHandler(handler)
// Assert
Assert.assertThat(viewModel.waypoints.get()[0][0].latitude, `is`(1.2))
Assert.assertThat(viewModel.waypoints.get()[0][0].longitude, `is`(3.4))
}

@Test
fun testSetSingleEmptyList() {
// Arrange
val uri = mock(Uri::class.java)
val reader = sut.TrackReader(uri, viewModel)
`when`(handler.waypoints).thenReturn(mutableListOf<MutableList<Waypoint>>())
// Act
reader.updateViewModelWithHandler(handler)
// Assert
Assert.assertThat(viewModel.waypoints.get().count(), `is`(0))
}

@Test
fun testSetSingleEmptyListOfWaypoints() {
// Arrange
val uri = mock(Uri::class.java)
val reader = sut.TrackReader(uri, viewModel)
`when`(handler.waypoints).thenReturn(mutableListOf(mutableListOf<Waypoint>()))
// Act
reader.updateViewModelWithHandler(handler)
// Assert
Assert.assertThat(viewModel.waypoints.get().count(), `is`(0))
}


@Test
fun testSetSingleEmptyFullCombinatie() {
// Arrange
val uri = mock(Uri::class.java)
val reader = sut.TrackReader(uri, viewModel)
val empty = mutableListOf<Waypoint>()
val waypoint = Waypoint(latitude = 1.2, longitude = 3.4, time = 1)
val items = mutableListOf<Waypoint>(waypoint, waypoint)
`when`(handler.waypoints).thenReturn(mutableListOf(empty, items))
// Act
reader.updateViewModelWithHandler(handler)
// Assert
Assert.assertThat(viewModel.waypoints.get().count(), `is`(1))
Assert.assertThat(viewModel.waypoints.get()[0].count(), `is`(2))
}
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import org.mockito.Mockito
import org.mockito.Mockito.`when`

class MockAppComponentTestRule : TestRule {

@@ -17,6 +18,7 @@ class MockAppComponentTestRule : TestRule {
override fun evaluate() {
mockAppComponent = Mockito.mock(AppComponent::class.java)
GpsTrackerApplication.appComponent = mockAppComponent
`when`(mockAppComponent.providerAuthority()).thenReturn("none")
base.evaluate()
}
}

0 comments on commit d035b77

Please sign in to comment.