Skip to content

Commit

Permalink
#17 in progress - selector
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Apr 11, 2024
1 parent cdedad2 commit 543d695
Show file tree
Hide file tree
Showing 22 changed files with 623 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.pm.ActivityInfo
import android.content.res.Resources
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
Expand All @@ -21,22 +22,32 @@ 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.ResultScreen
import pl.marianjureczko.poszukiwacz.activity.result.n.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
import pl.marianjureczko.poszukiwacz.activity.treasureselector.n.SelectorScreen
import pl.marianjureczko.poszukiwacz.shared.Settings
import pl.marianjureczko.poszukiwacz.ui.Screen
import pl.marianjureczko.poszukiwacz.ui.theme.AppTheme
import javax.inject.Inject

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}"
val SELECTOR_PATH = "selector"
val SELECTOR_ROUTE = "$SELECTOR_PATH/{$PARAMETER_JUST_FOUND_TREASURE}"

/**
* Routes creation and selection activity
*/
//TODO: check https://developer.android.com/build/build-variants and Product Flavours
@AndroidEntryPoint
class MainActivity : PermissionActivity() {

private val TAG = javaClass.simpleName
@Inject
lateinit var settings: Settings

Expand All @@ -55,6 +66,26 @@ class MainActivity : PermissionActivity() {
}
}

override fun onPause() {
Log.i(TAG, "onPause")
super.onPause()
}

override fun onResume() {
Log.i(TAG, "onResume")
super.onResume()
}

override fun onStop() {
Log.i(TAG, "onStop")
super.onStop()
}

override fun onDestroy() {
Log.i(TAG, "onDestroy")
super.onDestroy()
}

fun onClickOnGuide() {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(resources.getString(R.string.help_path))))
}
Expand All @@ -67,11 +98,11 @@ private fun ComposeRoot(settings: Settings, resources: Resources, onClickGuide:
NavHost(navController, startDestination = "main") {
composable(route = "main") {
MainScreen(navController, settings.isClassicMode(), resources, onClickGuide) { routeName ->
navController.navigate("searching/$routeName")
navController.navigate("$SEARCHING_PATH/$routeName")
}
}
composable(
route = "searching/{$PARAMETER_ROUTE_NAME}",
route = SEARCHING_ROUTE,
arguments = listOf(navArgument(PARAMETER_ROUTE_NAME) { type = NavType.StringType }),
// deepLinks = listOf(navDeepLink { uriPattern = "www.restaurantsapp.details.com/{restaurant_id}" }),
) {
Expand All @@ -81,14 +112,18 @@ private fun ComposeRoot(settings: Settings, resources: Resources, onClickGuide:
resources = resources,
onClickOnGuide = onClickGuide,
goToTipPhoto = { navController.navigate("tipPhoto/$it") },
goToResult = { navController.navigate("result/$it") },
goToMap = { navController.navigate("map/$it") }
goToResult = { resultType, treasureId -> navController.navigate("$RESULTS_PATH/$resultType/$treasureId") },
goToMap = { navController.navigate("map/$it") },
goToTreasureSelector = { navController.navigate("$SELECTOR_PATH/$it") }
)
}
composable(
route = "result/{$PARAMETER_RESULT_TYPE}",
arguments = listOf(navArgument(PARAMETER_RESULT_TYPE) { type = NavType.EnumType(ResultType::class.java) })
) { ResultScreen(navController, resources, onClickGuide) }
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, resources, onClickGuide) }
composable(
route = "tipPhoto/{$PARAMETER_TIP_PHOTO}",
arguments = listOf(navArgument(PARAMETER_TIP_PHOTO) { type = NavType.StringType })
Expand All @@ -101,5 +136,15 @@ private fun ComposeRoot(settings: Settings, resources: Resources, onClickGuide:
) {
MapScreen(navController = navController, onClickOnGuide = onClickGuide, resources = resources)
}
composable(
route = SELECTOR_ROUTE,
arguments = listOf(navArgument(PARAMETER_JUST_FOUND_TREASURE) { type = NavType.IntType }),
) { navBackStackEntry -> SelectorScreen(
navController,
navBackStackEntry,
resources,
onClickGuide,
goToResult = { treasureId -> navController.navigate("$RESULTS_PATH/${ResultType.TREASURE}/$treasureId") }
) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import android.annotation.SuppressLint
import android.content.res.Resources
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import pl.marianjureczko.poszukiwacz.App
import pl.marianjureczko.poszukiwacz.ui.components.TopBar
import pl.marianjureczko.poszukiwacz.ui.theme.AppTheme

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MainScreen(
navController: NavController?,
navController: NavController,
isClassic: Boolean,
resources: Resources,
onClickOnGuide: () -> Unit,
Expand All @@ -31,11 +28,10 @@ fun MainScreen(
)
}

@Preview(showBackground = true, apiLevel = 31)
@Composable
fun DefaultPreview() {
AppTheme {
MainScreen(null, false, App.getResources(), {}, {})
}
}

//@Preview(showBackground = true, apiLevel = 31)
//@Composable
//fun DefaultPreview() {
// AppTheme {
// MainScreen(null, false, App.getResources(), {}, {})
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import pl.marianjureczko.poszukiwacz.ui.components.TopBar
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MapScreen(
navController: NavController?,
navController: NavController,
resources: Resources,
onClickOnGuide: () -> Unit
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import pl.marianjureczko.poszukiwacz.ui.theme.SecondaryBackground
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun TipPhotoScreen(
navController: NavController?,
navController: NavController,
onClickOnGuide: () -> Unit
) {
Scaffold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,62 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import pl.marianjureczko.poszukiwacz.App
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.searching.n.ResultSharedViewModel
import pl.marianjureczko.poszukiwacz.activity.searching.n.SharedViewModel
import pl.marianjureczko.poszukiwacz.ui.components.AdvertBanner
import pl.marianjureczko.poszukiwacz.ui.components.TopBar
import pl.marianjureczko.poszukiwacz.ui.theme.AppTheme
import pl.marianjureczko.poszukiwacz.ui.shareViewModelStoreOwner
import pl.marianjureczko.poszukiwacz.ui.theme.FANCY_FONT
import pl.marianjureczko.poszukiwacz.ui.theme.SecondaryBackground

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun ResultScreen(
navController: NavController?,
navController: NavController,
navBackStackEntry: NavBackStackEntry,
resources: Resources,
onClickOnGuide: () -> Unit
) {
Scaffold(
topBar = { TopBar(navController, onClickOnGuide) },
content = {
ResultScreenBody(resources)
ResultScreenBody(resources, shareViewModelStoreOwner(navBackStackEntry, navController))
}
)
}

@Composable
fun ResultScreenBody(resources: Resources) {
val viewModel: ResultViewModel = hiltViewModel()
val state = viewModel.state.value
fun ResultScreenBody(resources: Resources, viewModelStoreOwner: NavBackStackEntry) {
val localViewModel: ResultViewModel = hiltViewModel()
val localState = localViewModel.state.value
val sharedViewModel: ResultSharedViewModel = getViewModel(viewModelStoreOwner)
sharedViewModel.resultPresented()
val snackbarCoroutineScope = rememberCoroutineScope()
Column(Modifier.background(SecondaryBackground)) {
if (state.resultType == ResultType.TREASURE) {
if (localState.resultType == ResultType.TREASURE) {
//TODO
} else {
Spacer(
modifier = Modifier
.weight(0.01f)
.background(Color.Transparent)
)
val text = if (state.resultType == ResultType.NOT_A_TREASURE) {
val text = if (localState.resultType == ResultType.NOT_A_TREASURE) {
resources.getString(R.string.not_a_treasure_msg)
} else {
resources.getString(R.string.treasure_already_taken_msg)
}
Text(
fontSize = 60.sp,
fontWeight = FontWeight.Bold,
fontFamily = FontFamily(Font(R.font.akaya_telivigala)),
fontFamily = FANCY_FONT,
color = Color.Gray,
textAlign = TextAlign.Center,
text = text
Expand All @@ -78,10 +81,16 @@ fun ResultScreenBody(resources: Resources) {
}
}

@Preview(showBackground = true, apiLevel = 31)
@Composable
fun ResultDefaultPreview() {
AppTheme {
ResultScreenBody(App.getResources())
}
}
private fun getViewModel(viewModelStoreOwner: NavBackStackEntry): ResultSharedViewModel {
val viewModelDoNotInline: SharedViewModel = hiltViewModel(viewModelStoreOwner)
return viewModelDoNotInline
}

//@Preview(showBackground = true, apiLevel = 31)
//@Composable
//fun ResultDefaultPreview() {
// AppTheme {
// ResultScreenBody(App.getResources())
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

const val PARAMETER_RESULT_TYPE = "result_type"
const val PARAMETER_TREASURE_ID = "treasure_id"
const val NOTHING_FOUND_TREASURE_ID = -1

@HiltViewModel
class ResultViewModel @Inject constructor(
private val stateHandle: SavedStateHandle
private val stateHandle: SavedStateHandle,
) : ViewModel() {

private var _state: MutableState<ResultState> = mutableStateOf(createState())
Expand All @@ -24,4 +26,5 @@ class ResultViewModel @Inject constructor(
val resultType = stateHandle.get<ResultType>(PARAMETER_RESULT_TYPE) ?: ResultType.NOT_A_TREASURE
return ResultState(resultType, null, null, null)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ class LocationFetcher(val context: Context) {
}
}

fun startFetching(viewModelScope: CoroutineScope, updateLocationCallback: (Location) -> Unit) {
fun startFetching(interval: Long, viewModelScope: CoroutineScope, updateLocationCallback: (Location) -> Unit) {
this.updateLocationCallback = updateLocationCallback
viewModelScope.launch {
requestLocation()
requestLocation(interval)
}
}

fun stopFetching() {
fusedLocationClient.removeLocationUpdates(locationCallback)
}

suspend fun requestLocation() {
suspend fun requestLocation(interval: Long = 1_000) {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)

val locationRequest = LocationRequest.Builder(1_000).build()
val locationRequest = LocationRequest.Builder(interval).build()
return suspendCoroutine { _ ->
//The permission should be already granted, but Idea reports error when the check is missing
if (ContextCompat.checkSelfPermission(
Expand Down
Loading

0 comments on commit 543d695

Please sign in to comment.