Skip to content

Commit

Permalink
hotfix for toaster
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Jan 31, 2024
1 parent 98bf684 commit 7f74a19
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package pl.marianjureczko.poszukiwacz.activity.searching
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import pl.marianjureczko.poszukiwacz.activity.treasureselector.SelectTreasureInputData
import pl.marianjureczko.poszukiwacz.model.Treasure
import pl.marianjureczko.poszukiwacz.model.TreasureDescription

interface TreasuresStorage {
fun getTreasureSelectorActivityInputData(justFoundTreasureDesc: TreasureDescription?): SelectTreasureInputData
fun getTreasureSelectorInputData(treasureCollected: Boolean, justFoundTreasureDesc: TreasureDescription?): SelectTreasureInputData
}

class ChangeTreasureButtonListener (
Expand All @@ -16,6 +15,6 @@ class ChangeTreasureButtonListener (
) : View.OnClickListener {

override fun onClick(v: View?) {
activityLauncher.launch(treasures.getTreasureSelectorActivityInputData(null))
activityLauncher.launch(treasures.getTreasureSelectorInputData(false, null))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SearchingActivity : ActivityWithAdsAndBackButton() {
override fun onPostResume() {
super.onPostResume()
if (!model.treasureSelectionInitialized()) {
treasureSelectorLauncher.launch(model.getTreasureSelectorActivityInputData(null))
treasureSelectorLauncher.launch(model.getTreasureSelectorInputData(false, null))
}
}

Expand Down Expand Up @@ -143,7 +143,8 @@ class SearchingActivity : ActivityWithAdsAndBackButton() {
model.replaceProgress(progress, storageHelper)
showCollectedTreasures()
if (resultActivityOutput.newTreasureCollected) {
treasureSelectorLauncher.launch(model.getTreasureSelectorActivityInputData(resultActivityOutput.justFoundTreasureDescription))
val input = model.getTreasureSelectorInputData(true, resultActivityOutput.justFoundTreasureDescription);
treasureSelectorLauncher.launch(input)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ class SearchingActivityViewModel(private val state: SavedStateHandle) : ViewMode
return treasuresProgress.selectedTreasure
}

override fun getTreasureSelectorActivityInputData(justFoundTreasureDesc: TreasureDescription?): SelectTreasureInputData {
override fun getTreasureSelectorInputData(
treasureCollected: Boolean,
justFoundTreasureDesc: TreasureDescription?
): SelectTreasureInputData {
treasureSelectionInitialized = true
return SelectTreasureInputData(route, treasuresProgress, currentCoordinates, justFoundTreasureDesc)
return SelectTreasureInputData(treasureCollected, route, treasuresProgress, currentCoordinates, justFoundTreasureDesc)
}

fun tryToFindTreasureDescription(justFoundTreasure: Treasure?) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.Treasure
import pl.marianjureczko.poszukiwacz.model.TreasureDescription
import pl.marianjureczko.poszukiwacz.model.TreasuresProgress
import pl.marianjureczko.poszukiwacz.shared.XmlHelper
import java.io.Serializable

data class SelectTreasureInputData(
val newTreasureCollected: Boolean,
val route: Route,
val progress: TreasuresProgress,
val currentCoordinates: Coordinates?,
Expand All @@ -34,6 +34,7 @@ class SelectTreasureContract : ActivityResultContract<SelectTreasureInputData, S
}
putExtra(TreasureSelectorActivity.ROUTE, xmlHelper.writeToString(input.route))
putExtra(TreasureSelectorActivity.PROGRESS, xmlHelper.writeToString(input.progress))
putExtra(TreasureSelectorActivity.NEW_TREASURE_COLLECTED, input.newTreasureCollected)
input.currentCoordinates?.let {
putExtra(TreasureSelectorActivity.LOCATION, it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ class SelectorViewModel(private val state: SavedStateHandle) : ViewModel() {
var userLocation: Coordinates? = null
private set
var justFoundTreasureDescription: TreasureDescription? = null
var newTreasureCollected: Boolean = false
private var treasureDescriptionSelectedForPhoto: Int? = null
private val locationCalculator = LocationCalculator()

fun initialize(route: Route,
progress: TreasuresProgress,
newTreasureCollected: Boolean,
userLocation: Coordinates?,
justFoundTreasureDescription: TreasureDescription?) {
this.route = route
this.progress = progress
this.newTreasureCollected = newTreasureCollected
this.userLocation = userLocation
this.justFoundTreasureDescription = justFoundTreasureDescription
state.get<Set<Int>>(IDS_OF_COLLECTED)?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class TreasureSelectorActivity : PermissionActivity(), ActivityTerminator {
internal const val ROUTE = "pl.marianjureczko.poszukiwacz.activity.route_to_select_from"
internal const val PROGRESS = "pl.marianjureczko.poszukiwacz.activity.route_progress"
internal const val LOCATION = "pl.marianjureczko.poszukiwacz.activity.user_coordinates"
internal const val NEW_TREASURE_COLLECTED = "pl.marianjureczko.poszukiwacz.activity.new_treasure_collected"
internal const val TREASURE_DESCRIPTION =
"pl.marianjureczko.poszukiwacz.activity.treasure_selector_treasure_description"
private val xmlHelper = XmlHelper()
Expand All @@ -80,6 +81,7 @@ class TreasureSelectorActivity : PermissionActivity(), ActivityTerminator {
model.initialize(
route = xmlHelper.loadFromString<Route>(intent.getStringExtra(ROUTE)!!),
progress = xmlHelper.loadFromString<TreasuresProgress>(intent.getStringExtra(PROGRESS)!!),
newTreasureCollected = intent.getBooleanExtra(NEW_TREASURE_COLLECTED, false),
userLocation = intent.getSerializableExtra(LOCATION) as Coordinates?,
justFoundTreasureDescription = intent.getSerializableExtra(TREASURE_DESCRIPTION) as TreasureDescription?
)
Expand Down Expand Up @@ -107,16 +109,19 @@ class TreasureSelectorActivity : PermissionActivity(), ActivityTerminator {
super.onBackPressed()
}

private fun markTreasureIfFound() =
private fun markTreasureIfFound() {
if (model.treasureDescriptionHasBeenIdentified()) {
val id = model.justFoundTreasureDescription!!.id.toString()
Toast.makeText(this, this.getString(R.string.treasure_marked_as_collected, id), Toast.LENGTH_LONG).show()
model.justFoundTreasureDescription = null
adapter.notifyDataSetChanged()
} else {
//TODO: in case the toast is too quick - https://www.geeksforgeeks.org/display-toast-for-a-specific-time-in-android/
Toast.makeText(this, R.string.treasure_nor_marked, Toast.LENGTH_LONG).show()
if (model.newTreasureCollected) {
//TODO: in case the toast is too quick - https://www.geeksforgeeks.org/display-toast-for-a-specific-time-in-android/
Toast.makeText(this, R.string.treasure_nor_marked, Toast.LENGTH_LONG).show()
}
}
}

private fun intentResultWithProgress(): Intent {
val data = Intent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.BDDMockito.given
import org.mockito.BDDMockito.mockingDetails
import org.mockito.BDDMockito.reset
import org.mockito.BDDMockito.then
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.verification.VerificationMode
import pl.marianjureczko.poszukiwacz.model.HunterPath
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.Treasure
import pl.marianjureczko.poszukiwacz.model.TreasureType
Expand Down Expand Up @@ -91,7 +88,7 @@ class SearchingActivityViewModelTest {
fun `SHOULD say initialized WHEN the flag was set by calling getTreasureSelectorActivityInputData`() {
//given
val model = some<SearchingActivityViewModel>()
model.getTreasureSelectorActivityInputData(null)
model.getTreasureSelectorInputData(false, null)

//when
val actual = model.treasureSelectionInitialized()
Expand Down Expand Up @@ -145,7 +142,7 @@ class SearchingActivityViewModelTest {
assertThat(fixture.model.getSelectedForHuntTreasure()).isNotNull()

//when
val actual = fixture.model.getTreasureSelectorActivityInputData(null)
val actual = fixture.model.getTreasureSelectorInputData(false, null)

//then
then(state).should().set(SearchingActivityViewModel.TREASURE_SELECTION_INITIALIZED, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SelectorViewModelStateTest {
.willReturn(collectedIds)

//when
selectorViewModel.initialize(some<Route>(), some<TreasuresProgress>(), null, null)
selectorViewModel.initialize(some<Route>(), some<TreasuresProgress>(), false, null, null)

//then
assertThat(selectorViewModel.getIdsOfCollectedTreasures()).containsExactlyInAnyOrderElementsOf(collectedIds)
Expand All @@ -50,7 +50,7 @@ class SelectorViewModelStateTest {
val collectedIds = progress.collectedTreasuresDescriptionId.toSet()

//when
selectorViewModel.initialize(some<Route>(), progress, null, null)
selectorViewModel.initialize(some<Route>(), progress, false, null, null)

//then
assertThat(selectorViewModel.getIdsOfCollectedTreasures()).containsExactlyInAnyOrderElementsOf(collectedIds)
Expand Down Expand Up @@ -143,7 +143,7 @@ internal class SelectorViewModelTest {
fun `SHOULD use treasure pretty name WHEN user location is null`() {
//given
val model = some<SelectorViewModel>()
model.initialize(some<Route>(), some<TreasuresProgress>(), null, null)
model.initialize(some<Route>(), some<TreasuresProgress>(), false, null, null)
assertThat(model.userLocation).isNull()
val treasureDescription = some<TreasureDescription>()

Expand Down Expand Up @@ -185,7 +185,7 @@ internal class SelectorViewModelTest {
val selectorViewModel = SelectorViewModel(state)
val treasure = some<TreasureDescription>()
val route = some<Route>().copy(treasures = mutableListOf(treasure))
selectorViewModel.initialize(route, TreasuresProgress(), null, null)
selectorViewModel.initialize(route, TreasuresProgress(), false, null, null)
selectorViewModel.selectForCommemorativePhoto(treasure)
Mockito.reset(state)
val firstPhoto = Files.createTempFile("test", ".test").toAbsolutePath().toString()
Expand Down

0 comments on commit 7f74a19

Please sign in to comment.