Created
June 23, 2019 20:51
-
-
Save carstenhag/3d4b1afca957f938b1655129abdb921f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.chagemann.timetracking.features.nfcReaction | |
import android.app.Application | |
import android.content.Intent | |
import androidx.lifecycle.AndroidViewModel | |
import androidx.lifecycle.Transformations | |
import de.chagemann.timetracking.db.Project | |
import de.chagemann.timetracking.db.TimeRepository | |
import de.chagemann.timetracking.db.TrackingInterval | |
import de.chagemann.timetracking.features.nfcMapping.NFCUtils | |
import org.jetbrains.anko.doAsync | |
import org.threeten.bp.Instant | |
import java.util.* | |
class NFCReactionViewModel(private val app: Application) : AndroidViewModel(app) { | |
private val nfcUtil: NFCUtils by lazy { | |
NFCUtils() | |
} | |
private val repository: TimeRepository by lazy { | |
TimeRepository(app) | |
} | |
fun retrieveMessageAndTrackTime(intent: Intent) { | |
val tagID = nfcUtil.retrieveNFCMessage(intent) | |
Transformations.switchMap(repository.getNFCTagByID(tagID)) { nfcTag -> | |
println("nfc tag by id") | |
Transformations.switchMap(repository.getProjectByID(nfcTag.projectID)) { project: Project? -> | |
println("project by id") | |
println(project) | |
Transformations.map(repository.getUnfinishedTrackingIntervals()) { list -> | |
println(list) | |
when (list.size) { | |
0 -> { | |
doAsync { | |
repository.insertTrackingIntervals( | |
TrackingInterval( | |
UUID.randomUUID().toString(), | |
project?.id ?: "", | |
startInstant = Instant.now() | |
) | |
) | |
} | |
} | |
else -> { | |
val interval = list.find { interval -> interval.projectID == project?.id } | |
println(interval) | |
if (interval != null) { | |
doAsync { | |
repository.insertTrackingIntervals(interval.copy(endInstant = Instant.now())) | |
} | |
} else { | |
} | |
} | |
} | |
} | |
} | |
} | |
/* | |
repository.getNFCTagByID(tagID).observe(this, Observer Tag@{ tag -> | |
toast(tag.name) | |
repository.getProjectByID(tag.projectID).observe(this, Observer { project: Project? -> | |
if (project == null) { | |
toast("project null") | |
return@Observer | |
} | |
repository.getUnfinishedTrackingIntervals().observe(this, Observer{ list -> | |
when (list.size) { | |
0 -> { | |
toast("inserting new interval") | |
doAsync { | |
repository.insertTrackingIntervals( | |
TrackingInterval( | |
UUID.randomUUID().toString(), | |
project.id, | |
startInstant = Instant.now() | |
) | |
) | |
} | |
} | |
else -> { | |
val interval = list.find { interval -> interval.projectID == project.id } | |
if (interval == null) { | |
toast("wtf") | |
return@Observer | |
} | |
toast("inserting end interval") | |
doAsync { | |
repository.insertTrackingIntervals(interval.copy(endInstant = Instant.now())) | |
} | |
} | |
} | |
}) | |
}) | |
}) | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment