Skip to content

Commit

Permalink
Mock to meta-data update / insert
Browse files Browse the repository at this point in the history
  • Loading branch information
rcgroot committed Mar 13, 2017
1 parent 64bba3c commit 03a3cb3
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ public void run() {
synchronized (ConnectedServicePresenter.this) {
Context context = ConnectedServicePresenter.this.getContext();
if (context != null) {
Uri trackUri = TrackUriExtensionKt.trackUri(serviceManager.getTrackId());
String name = ContentProviderExtensionsKt.apply(trackUri, context, new Function1<Cursor, String>() {
@Override
public String invoke(Cursor cursor) {
return ContentProviderExtensionsKt.getString(cursor, NAME);
}
}, null, null);
long trackId = serviceManager.getTrackId();
Uri trackUri = null;
String name = null;
if (trackId > 0) {
trackUri = TrackUriExtensionKt.trackUri(trackId);
name = ContentProviderExtensionsKt.apply(trackUri, context, new Function1<Cursor, String>() {
@Override
public String invoke(Cursor cursor) {
return ContentProviderExtensionsKt.getString(cursor, NAME);
}
}, null, null);
}
didConnectToService(trackUri, name, serviceManager.getLoggingState());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ContentController(val context: Context, val listener: ContentListener) {
fun unregister() {
if (registeredUri != null) {
context.contentResolver.unregisterContentObserver(contentObserver)
registeredUri = null4
registeredUri = null4§
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import nl.sogeti.android.gpstracker.ng.utils.*
import javax.inject.Inject

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

private var executingReader: TrackReader? = null
private var contentController: ContentController? = null
private var googleMap: GoogleMap? = null

Expand Down Expand Up @@ -107,21 +107,13 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer

//endregion

/* Content watching */
//region Content watching

override fun onChangeUriContent(contentUri: Uri, changesUri: Uri) {
startReadingTrack(contentUri)
}

private fun startReadingTrack(trackUri: Uri) {
var executingReader = this.executingReader
if (executingReader == null || executingReader.trackUri != trackUri) {
executingReader?.cancel(true)
executingReader = TrackReader(trackUri, viewModel)
executingReader.execute()
this.executingReader = executingReader
}
}
//endregion

/* Google Map Tiles */

Expand All @@ -141,6 +133,16 @@ class TrackMapPresenter(private val viewModel: TrackMapViewModel) : ConnectedSer

/* Private */

private fun startReadingTrack(trackUri: Uri) {
var executingReader = this.executingReader
if (executingReader == null || executingReader.trackUri != trackUri) {
executingReader?.cancel(true)
executingReader = TrackReader(trackUri, viewModel)
executingReader.execute()
this.executingReader = executingReader
}
}

private fun makeTrackSelection() {
val trackUri = trackSelection.trackUri
if (trackUri != null && trackUri.lastPathSegment != "-1") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@
package nl.sogeti.android.gpstracker.ng.map

import android.net.Uri
import nl.sogeti.android.gpstracker.integration.ContentConstants
import nl.sogeti.android.gpstracker.ng.common.GpsTrackerApplication
import nl.sogeti.android.gpstracker.ng.common.abstractpresenters.ContextedPresenter
import nl.sogeti.android.gpstracker.ng.common.controllers.ContentController
import nl.sogeti.android.gpstracker.ng.common.controllers.ContentControllerProvider
import nl.sogeti.android.gpstracker.ng.model.TrackSelection
import nl.sogeti.android.gpstracker.ng.utils.apply
import nl.sogeti.android.gpstracker.ng.utils.getString
import javax.inject.Inject

class TrackPresenter(private val viewModel: TrackViewModel, private val view: TrackViewModel.View) : ContextedPresenter(), TrackSelection.Listener {
class TrackPresenter(private val viewModel: TrackViewModel, private val view: TrackViewModel.View) : ContextedPresenter(), TrackSelection.Listener, ContentController.ContentListener {

private var contentController: ContentController? = null
@Inject
lateinit var trackSelection: TrackSelection
@Inject
lateinit var contentControllerProvider: ContentControllerProvider

init {
GpsTrackerApplication.appComponent.inject(this)
Expand All @@ -47,14 +55,16 @@ class TrackPresenter(private val viewModel: TrackViewModel, private val view: Tr

override fun didStart() {
trackSelection.addListener(this)
contentController = contentControllerProvider.createContentControllerProvider(context!!, this)
trackSelection.trackUri?.let {
didSelectTrack(it, trackSelection.trackName)
}

}

override fun willStop() {
trackSelection.removeListener(this)
contentController?.unregisterObserver()
contentController = null
}

//endregion
Expand All @@ -79,6 +89,26 @@ class TrackPresenter(private val viewModel: TrackViewModel, private val view: Tr

override fun didSelectTrack(trackUri: Uri, name: String) {
viewModel.trackUri.set(trackUri)
contentController?.registerObserver(trackUri)
showName(name)
}

//endregion

//region Content watching

override fun onChangeUriContent(contentUri: Uri, changesUri: Uri) {
val name = contentUri.apply(context!!, { it.getString(ContentConstants.Tracks.NAME) })
name?.let {
showName(it)
}
}

//endregion

//region Private

private fun showName(name: String) {
viewModel.name.set(name)
view.showTrackName(name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ class MockServiceManager : ServiceManagerInterface {
fun reset() {
started = false
globalState.loggingState = STATE_UNKNOWN
globalState.trackId = 5L
globalState.trackId = -1L
}

companion object globalState {
var started = false
var loggingState = STATE_UNKNOWN
var trackId = 5L
var trackId = -1L
var segmentId = 10L
var waypointId = 100L
}
Expand Down Expand Up @@ -117,7 +117,7 @@ class MockServiceManager : ServiceManagerInterface {
}

private fun recordNewTrack() {
trackId++
if (trackId > 0) trackId++ else trackId = 2
MockTracksProvider.globalState.addTrack(trackId)
recordNewSegment()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,57 @@ class MockTracksProvider : ContentProvider() {
if (content != null) {
cursor = globalState.buildMatrixCursor(content.first, content.second)
} else {
Timber.w("Query on $uri did not match anything in global state $globalState ")
Timber.e("Query on $uri did not match anything in global state $globalState ")
}
return cursor

}

override fun getType(uri: Uri): String? {
return null
override fun insert(uri: Uri, values: ContentValues?): Uri? {
val content = globalState.uriContent[uri]
if (content != null && values != null) {
val row = mutableListOf<Any>()
val columns = content.first
for (index in 0..columns.size - 1) {
val column = columns[index]
val value = values.getAsString(column)
row.add(value)
}
content.second.add(row)
} else {
Timber.e("Insert on $uri did not match anything in global state $globalState ")
}

return uri
}

override fun insert(uri: Uri, values: ContentValues?): Uri? {
return null
override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int {
var changed = 0
val content = globalState.uriContent[uri]
if (content != null && values != null && content.second.size > 0) {
val row = content.second.first()
val columns = content.first
for (index in 0..columns.size - 1) {
val column = columns[index]
val value = values.getAsString(column)
value?.let { row[index] = value; changed++ }
}
globalState.context?.contentResolver?.notifyChange(uri, null)
} else {
Timber.e("Update on $uri did not match anything in global state $globalState ")
}

return changed
}

override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
Timber.e("Delete on $uri did not match anything in global state $globalState ")
return 0
}

override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?): Int {
return 0
override fun getType(uri: Uri): String? {
Timber.e("getType on $uri did not match anything in global state $globalState ")
return null
}

fun reset() {
Expand All @@ -79,7 +110,7 @@ class MockTracksProvider : ContentProvider() {

companion object globalState {
var context: Context? = null
val uriContent = mutableMapOf<Uri, Pair<Array<String>, MutableList<List<Any>>>>()
val uriContent = mutableMapOf<Uri, Pair<Array<String>, MutableList<MutableList<Any>>>>()
get() {
val preload = gpxAmsterdam
if (!preload.isEmpty()) {
Expand All @@ -105,7 +136,7 @@ class MockTracksProvider : ContentProvider() {
}

fun addTrack(trackId: Long) {
// .../track
// .../tracks
val tracksUri = tracksUri()
var content = uriContent[tracksUri]
if (content == null) {
Expand All @@ -114,18 +145,22 @@ class MockTracksProvider : ContentProvider() {
}
addContentToTrackContent(content.second, trackId)
context?.contentResolver?.notifyChange(tracksUri, null)
// .../track/id
// .../tracks/id
content = createEmptyTrackContent()
val trackUri = trackUri(trackId)
uriContent[trackUri] = content
addContentToTrackContent(content.second, trackId)
context?.contentResolver?.notifyChange(trackUri, null)
// tracks/id/metadata
val metaContent = Pair(arrayOf(MetaDataColumns.KEY, MetaDataColumns.VALUE), mutableListOf<MutableList<Any>>())
val metaUri = metaDataTrackUri(trackId)
uriContent[metaUri] = metaContent
}

private fun createEmptyTrackContent() = Pair(arrayOf(Tracks._ID, Tracks.NAME, Tracks.CREATION_TIME), mutableListOf<List<Any>>())
private fun createEmptyTrackContent() = Pair(arrayOf(Tracks._ID, Tracks.NAME, Tracks.CREATION_TIME), mutableListOf<MutableList<Any>>())

private fun addContentToTrackContent(content: MutableList<List<Any>>, trackId: Long) {
content.add(listOf(
private fun addContentToTrackContent(content: MutableList<MutableList<Any>>, trackId: Long) {
content.add(mutableListOf(
trackId,
"track $trackId",
Date().time))
Expand All @@ -149,10 +184,10 @@ class MockTracksProvider : ContentProvider() {
context?.contentResolver?.notifyChange(segmentUri, null)
}

private fun createEmptySegmentContent() = Pair(arrayOf(Segments._ID, Segments.TRACK), mutableListOf<List<Any>>())
private fun createEmptySegmentContent() = Pair(arrayOf(Segments._ID, Segments.TRACK), mutableListOf<MutableList<Any>>())

private fun addContentToSegmentContent(content: MutableList<List<Any>>, trackId: Long, segmentId: Long) {
content.add(listOf(
private fun addContentToSegmentContent(content: MutableList<MutableList<Any>>, trackId: Long, segmentId: Long) {
content.add(mutableListOf(
segmentId,
trackId))
}
Expand All @@ -178,10 +213,10 @@ class MockTracksProvider : ContentProvider() {
context?.contentResolver?.notifyChange(waypointsTrackUri, null)
}

private fun createEmptyWaypointContent() = Pair(arrayOf(Waypoints._ID, Waypoints.SEGMENT, Waypoints.LATITUDE, Waypoints.LONGITUDE, Waypoints.TIME), mutableListOf<List<Any>>())
private fun createEmptyWaypointContent() = Pair(arrayOf(Waypoints._ID, Waypoints.SEGMENT, Waypoints.LATITUDE, Waypoints.LONGITUDE, Waypoints.TIME), mutableListOf<MutableList<Any>>())

private fun addContentToWaypointContent(content: MutableList<List<Any>>, segmentId: Long, waypointId: Long, latitude: Double, longitude: Double, time: Long) {
content.add(listOf(waypointId, segmentId, latitude, longitude, time))
private fun addContentToWaypointContent(content: MutableList<MutableList<Any>>, segmentId: Long, waypointId: Long, latitude: Double, longitude: Double, time: Long) {
content.add(mutableListOf(waypointId, segmentId, latitude, longitude, time))
}

fun buildMatrixCursor(columns: Array<String>, content: List<List<Any>>): Cursor? {
Expand Down

0 comments on commit 03a3cb3

Please sign in to comment.