Skip to content

Commit

Permalink
#17 in progress
Browse files Browse the repository at this point in the history
- showing collected treasure in classic mode
  • Loading branch information
mjureczko committed Dec 27, 2024
1 parent 2094c20 commit cc7184e
Show file tree
Hide file tree
Showing 18 changed files with 308 additions and 262 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import pl.marianjureczko.poszukiwacz.activity.facebook.n.FacebookHelper
import pl.marianjureczko.poszukiwacz.activity.main.COMMEMORATIVE_PATH
import pl.marianjureczko.poszukiwacz.activity.main.RESULTS_PATH
import pl.marianjureczko.poszukiwacz.activity.main.RESULTS_ROUTE
import pl.marianjureczko.poszukiwacz.activity.main.SEARCHING_PATH
import pl.marianjureczko.poszukiwacz.activity.main.SEARCHING_ROUTE
import pl.marianjureczko.poszukiwacz.activity.main.TREASURE_EDITOR_PATH
import pl.marianjureczko.poszukiwacz.activity.main.TREASURE_EDITOR_ROUTE
import pl.marianjureczko.poszukiwacz.activity.searching.n.PARAMETER_ROUTE_NAME
import pl.marianjureczko.poszukiwacz.activity.searching.n.SearchingScreen
import pl.marianjureczko.poszukiwacz.screen.main.MainScreen
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_RESULT_TYPE
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_TREASURE_AMOUNT
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_TREASURE_ID
import pl.marianjureczko.poszukiwacz.screen.result.ResultScreen
import pl.marianjureczko.poszukiwacz.screen.result.ResultType
import pl.marianjureczko.poszukiwacz.screen.treasureseditor.TreasureEditorScreen
import pl.marianjureczko.poszukiwacz.shared.GoToCommemorative
import pl.marianjureczko.poszukiwacz.shared.GoToFacebook
Expand All @@ -25,27 +32,30 @@ import pl.marianjureczko.poszukiwacz.shared.GoToTreasureEditor
fun ComposeRoot(onClickGuide: GoToGuide) {
val navController = rememberNavController()
val goToFacebook: GoToFacebook = FacebookHelper.createFacebookCallback(navController)
val goToCommemorative: GoToCommemorative = { treasureId -> navController.navigate("$COMMEMORATIVE_PATH/$treasureId") }
val goToEditor: GoToTreasureEditor = {routeName -> navController.navigate("$TREASURE_EDITOR_PATH/$routeName")}
val goToCommemorative: GoToCommemorative =
{ treasureId -> navController.navigate("$COMMEMORATIVE_PATH/$treasureId") }
val goToEditor: GoToTreasureEditor = { routeName -> navController.navigate("$TREASURE_EDITOR_PATH/$routeName") }

NavHost(navController, startDestination = "main") {
composable(route = "main") {
MainScreen(
navController,
onClickGuide,
goToFacebook,
goToEditor) { routeName ->
goToEditor
) { routeName ->
navController.navigate("$SEARCHING_PATH/$routeName")
}
}
composable(
route = TREASURE_EDITOR_ROUTE,
arguments = listOf(navArgument(PARAMETER_ROUTE_NAME) { type = NavType.StringType })
){
) {
TreasureEditorScreen(
navController = navController,
onClickOnGuide = onClickGuide,
onClickOnFacebook = goToFacebook)
onClickOnFacebook = goToFacebook
)
}
composable(
route = SEARCHING_ROUTE,
Expand All @@ -55,20 +65,21 @@ fun ComposeRoot(onClickGuide: GoToGuide) {
navController = navController,
onClickOnGuide = onClickGuide,
goToTipPhoto = { navController.navigate("tipPhoto/$it") },
goToResult = { resultType, treasureId -> /*navController.navigate("$RESULTS_PATH/$resultType/$treasureId") */},
goToResult = { resultType, treasureId, amount -> navController.navigate("$RESULTS_PATH/$resultType/$treasureId/$amount") },
goToMap = { navController.navigate("map/$it") },
goToTreasureSelector = { /*navController.navigate("$SELECTOR_PATH/$it")*/ },
goToFacebook = goToFacebook,
goToCommemorative = goToCommemorative,
)
}
// composable(
// route = RESULTS_ROUTE,
// arguments = listOf(
// navArgument(PARAMETER_RESULT_TYPE) { type = NavType.EnumType(ResultType::class.java) },
// navArgument(PARAMETER_TREASURE_ID) { type = NavType.IntType }
// )
// ) { navBackStackEntry -> ResultScreen(navController, navBackStackEntry, onClickGuide, goToFacebook) }
composable(
route = RESULTS_ROUTE,
arguments = listOf(
navArgument(PARAMETER_RESULT_TYPE) { type = NavType.EnumType(ResultType::class.java) },
navArgument(PARAMETER_TREASURE_ID) { type = NavType.IntType },
navArgument(PARAMETER_TREASURE_AMOUNT) { type = NavType.IntType },
)
) { navBackStackEntry -> ResultScreen(navController, navBackStackEntry, onClickGuide, goToFacebook) }
// composable(
// route = "tipPhoto/{$PARAMETER_TIP_PHOTO}",
// arguments = listOf(navArgument(PARAMETER_TIP_PHOTO) { type = NavType.StringType })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import pl.marianjureczko.poszukiwacz.activity.map.n.MapScreen
import pl.marianjureczko.poszukiwacz.activity.map.n.PARAMETER_ROUTE_NAME_2
import pl.marianjureczko.poszukiwacz.activity.photo.n.PARAMETER_TIP_PHOTO
import pl.marianjureczko.poszukiwacz.activity.photo.n.TipPhotoScreen
import pl.marianjureczko.poszukiwacz.activity.result.n.PARAMETER_RESULT_TYPE
import pl.marianjureczko.poszukiwacz.activity.result.n.PARAMETER_TREASURE_ID
import pl.marianjureczko.poszukiwacz.activity.result.n.PARAMETER_TREASURE_QUANTITY
import pl.marianjureczko.poszukiwacz.activity.result.n.ResultScreen
import pl.marianjureczko.poszukiwacz.activity.result.n.ResultType
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_RESULT_TYPE
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_TREASURE_ID
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_TREASURE_QUANTITY
import pl.marianjureczko.poszukiwacz.screen.result.ResultScreen
import pl.marianjureczko.poszukiwacz.screen.result.ResultType
import pl.marianjureczko.poszukiwacz.activity.searching.n.PARAMETER_ROUTE_NAME
import pl.marianjureczko.poszukiwacz.activity.searching.n.SearchingScreen
import pl.marianjureczko.poszukiwacz.activity.treasureselector.n.PARAMETER_JUST_FOUND_TREASURE
Expand Down Expand Up @@ -57,7 +57,7 @@ fun ComposeRoot(onClickGuide: GoToGuide) {
navController = navController,
onClickOnGuide = onClickGuide,
goToTipPhoto = { navController.navigate("tipPhoto/$it") },
goToResult = { resultType, treasureId, quantity -> navController.navigate("$RESULTS_PATH/$resultType/$treasureId/$quantity") },
goToResult = { resultType, treasureId, amount -> navController.navigate("$RESULTS_PATH/$resultType/$treasureId/$amount") },
goToMap = { navController.navigate("map/$it") },
goToTreasureSelector = { navController.navigate("$SELECTOR_PATH/$it") },
goToFacebook = goToFacebook,
Expand All @@ -69,7 +69,7 @@ fun ComposeRoot(onClickGuide: GoToGuide) {
arguments = listOf(
navArgument(PARAMETER_RESULT_TYPE) { type = NavType.EnumType(ResultType::class.java) },
navArgument(PARAMETER_TREASURE_ID) { type = NavType.IntType },
navArgument(PARAMETER_TREASURE_QUANTITY) { type = NavType.IntType },
navArgument(PARAMETER_TREASURE_AMOUNT) { type = NavType.IntType },
)
) { navBackStackEntry -> ResultScreen(navController, navBackStackEntry, onClickGuide, goToFacebook) }
composable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import androidx.activity.compose.setContent
import dagger.hilt.android.AndroidEntryPoint
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.commemorative.n.PARAMETER_TREASURE_DESCRIPTION_ID
import pl.marianjureczko.poszukiwacz.activity.result.n.PARAMETER_RESULT_TYPE
import pl.marianjureczko.poszukiwacz.activity.result.n.PARAMETER_TREASURE_ID
import pl.marianjureczko.poszukiwacz.activity.result.n.PARAMETER_TREASURE_QUANTITY
import pl.marianjureczko.poszukiwacz.activity.searching.n.PARAMETER_ROUTE_NAME
import pl.marianjureczko.poszukiwacz.activity.treasureselector.n.PARAMETER_JUST_FOUND_TREASURE
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_RESULT_TYPE
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_TREASURE_AMOUNT
import pl.marianjureczko.poszukiwacz.screen.result.PARAMETER_TREASURE_ID
import pl.marianjureczko.poszukiwacz.ui.ComposeRoot
import pl.marianjureczko.poszukiwacz.ui.Screen
import pl.marianjureczko.poszukiwacz.ui.theme.AppTheme
Expand All @@ -25,7 +25,7 @@ val TREASURE_EDITOR_ROUTE = "$TREASURE_EDITOR_PATH/{$PARAMETER_ROUTE_NAME}"
val SEARCHING_PATH = "searching"
val SEARCHING_ROUTE = "$SEARCHING_PATH/{$PARAMETER_ROUTE_NAME}"
val RESULTS_PATH = "result"
val RESULTS_ROUTE = "$RESULTS_PATH/{$PARAMETER_RESULT_TYPE}/{$PARAMETER_TREASURE_ID}/{$PARAMETER_TREASURE_QUANTITY}"
val RESULTS_ROUTE = "$RESULTS_PATH/{$PARAMETER_RESULT_TYPE}/{$PARAMETER_TREASURE_ID}/{$PARAMETER_TREASURE_AMOUNT}"
val SELECTOR_PATH = "selector"
val SELECTOR_ROUTE = "$SELECTOR_PATH/{$PARAMETER_JUST_FOUND_TREASURE}"
val COMMEMORATIVE_PATH = "commemorative"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pl.marianjureczko.poszukiwacz.activity.searching.n

import android.util.Log
import pl.marianjureczko.poszukiwacz.activity.searching.LocationCalculator
import pl.marianjureczko.poszukiwacz.model.Treasure
import pl.marianjureczko.poszukiwacz.model.TreasureDescription
import pl.marianjureczko.poszukiwacz.model.TreasureType
import pl.marianjureczko.poszukiwacz.shared.Coordinates

class JustFoundTreasureDescriptionFinder(
private val treasureDescriptions: List<TreasureDescription>,
private val locationCalculator: LocationCalculator = LocationCalculator(),
) {

private val TAG = javaClass.simpleName

fun findTreasureDescription(
qrCode: String,
justFoundTreasure: Treasure,
selectedTreasureDescription: TreasureDescription? = null,
userLocation: Coordinates? = null,
): TreasureDescription? {
if (justFoundTreasure.type == TreasureType.KNOWLEDGE) {
return treasureDescriptions.find { td ->
justFoundTreasure.id == td.qrCode
}
} else {
return if (selectedTreasureDescription != null && userLocation != null) {
val distance = locationCalculator.distanceInSteps(selectedTreasureDescription, userLocation)
Log.d(TAG, "Distance is $distance")
if (distance < 60) {
selectedTreasureDescription
} else {
null
}
} else {
null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import com.journeyapps.barcodescanner.ScanOptions
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.main.RESULTS_ROUTE
import pl.marianjureczko.poszukiwacz.activity.main.SELECTOR_ROUTE
import pl.marianjureczko.poszukiwacz.activity.result.n.NOTHING_FOUND_TREASURE_ID
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.TreasureDescription
import pl.marianjureczko.poszukiwacz.screen.result.NOTHING_FOUND_TREASURE_ID
import pl.marianjureczko.poszukiwacz.screen.searching.Scores
import pl.marianjureczko.poszukiwacz.shared.GoToCommemorative
import pl.marianjureczko.poszukiwacz.shared.GoToFacebook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.result.n.NOTHING_FOUND_TREASURE_ID
import pl.marianjureczko.poszukiwacz.activity.result.n.ResultType
import pl.marianjureczko.poszukiwacz.activity.searching.ArcCalculator
import pl.marianjureczko.poszukiwacz.activity.searching.LocationCalculator
import pl.marianjureczko.poszukiwacz.model.HunterPath
Expand All @@ -25,6 +23,8 @@ import pl.marianjureczko.poszukiwacz.model.TreasureDescription
import pl.marianjureczko.poszukiwacz.model.TreasureParser
import pl.marianjureczko.poszukiwacz.model.TreasureType
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.screen.result.NOTHING_FOUND_TREASURE_ID
import pl.marianjureczko.poszukiwacz.screen.result.ResultType
import pl.marianjureczko.poszukiwacz.shared.Coordinates
import pl.marianjureczko.poszukiwacz.shared.DoPhoto
import pl.marianjureczko.poszukiwacz.shared.GoToResults
Expand Down Expand Up @@ -92,44 +92,51 @@ class SharedViewModel @Inject constructor(
/*
* newCode will contain code like k01001
* result will be NOT_A_TREASURE or ALREADY_TAKEN or TREASURE
* finally goToResults(result, treasureId) is called
* goToResult = { resultType, treasureId -> navController.navigate("$RESULTS_PATH/$resultType/$treasureId") }
*/
var result: ResultType? = null
var treasureId = NOTHING_FOUND_TREASURE_ID
var foundTreasure: Treasure? = null
var scannedTreasure: Treasure? = null
if (scanResult != null && !scanResult.contents.isNullOrEmpty()) {
val newCode = scanResult.contents
//TODO t: refactor split / extract method
try {
foundTreasure = TreasureParser().parse(newCode)
val tdFinder = TreasureDescriptionFinder(state.value.route.treasures)
val foundTd: TreasureDescription? = tdFinder.findTreasureDescription(newCode, foundTreasure)
val treasuresProgress = state.value.treasuresProgress
if (treasuresProgress.contains(foundTreasure)) {
scannedTreasure = TreasureParser().parse(newCode)
val tdFinder = JustFoundTreasureDescriptionFinder(state.value.route.treasures)
val foundTd: TreasureDescription? = tdFinder.findTreasureDescription(newCode, scannedTreasure)
var treasuresProgress: TreasuresProgress = state.value.treasuresProgress
if (treasuresProgress.contains(scannedTreasure)) {
result = ResultType.ALREADY_TAKEN
} else {
if (foundTreasure.type == TreasureType.KNOWLEDGE && state.value.route.treasures.find { t -> newCode == t.qrCode } == null) {
if (scannedTreasure.type == TreasureType.KNOWLEDGE && state.value.route.treasures.find { t -> newCode == t.qrCode } == null) {
throw IllegalArgumentException("Unknown knowledge treasure")
}
treasuresProgress.collect(foundTreasure, foundTd)
treasuresProgress.resultRequiresPresentation = true
treasuresProgress.justFoundTreasureId = foundTd?.id
treasuresProgress.treasureFoundGoToSelector = true
foundTd?.let { treasureId = it.id }
storageHelper.save(treasuresProgress)
result = ResultType.from(foundTreasure.type)
treasuresProgress = updateTreasureProgress(treasuresProgress, scannedTreasure, foundTd)
_state.value = state.value.copy(treasuresProgress = treasuresProgress)
result = ResultType.from(scannedTreasure.type)
}
} catch (e: IllegalArgumentException) {
result = ResultType.NOT_A_TREASURE
}
}
result?.let {
goToResults(it, treasureId, foundTreasure?.quantity)
goToResults(it, treasureId, scannedTreasure?.quantity)
}
}
}

private fun updateTreasureProgress(
treasuresProgress: TreasuresProgress,
foundTreasure: Treasure,
foundTd: TreasureDescription?
): TreasuresProgress {
treasuresProgress.collect(foundTreasure, foundTd)
var result: TreasuresProgress = treasuresProgress.copy(resultRequiresPresentation = true)
result = result.copy(justFoundTreasureId = foundTd?.id)
result = result.copy(treasureFoundGoToSelector = true)
storageHelper.save(result)
return result
}

override fun resultPresented() {
viewModelScope.launch(ioDispatcher) {
delay(500)
Expand Down Expand Up @@ -212,6 +219,7 @@ class SharedViewModel @Inject constructor(
}
}

//TODO t: how to handle first search, when no treasure is selected
private fun createState(): SharedState {
Log.i(TAG, "Create state")
val route = loadRoute()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import pl.marianjureczko.poszukiwacz.activity.result.n.NOTHING_FOUND_TREASURE_ID
import pl.marianjureczko.poszukiwacz.screen.result.NOTHING_FOUND_TREASURE_ID
import pl.marianjureczko.poszukiwacz.shared.PhotoHelper
import pl.marianjureczko.poszukiwacz.shared.di.IoDispatcher
import javax.inject.Inject
Expand Down
Loading

0 comments on commit cc7184e

Please sign in to comment.