-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
382 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
163 changes: 163 additions & 0 deletions
163
...c/main/java/pl/marianjureczko/poszukiwacz/activity/commemorative/n/CommemorativeScreen.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package pl.marianjureczko.poszukiwacz.activity.commemorative.n | ||
|
||
import android.annotation.SuppressLint | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.widget.Toast | ||
import androidx.activity.compose.rememberLauncherForActivityResult | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.aspectRatio | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.offset | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.material.ScaffoldState | ||
import androidx.compose.material.rememberScaffoldState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import androidx.compose.ui.graphics.painter.BitmapPainter | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
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.CommemorativeSharedState | ||
import pl.marianjureczko.poszukiwacz.activity.searching.n.CommemorativeSharedViewModel | ||
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.shareViewModelStoreOwner | ||
import pl.marianjureczko.poszukiwacz.ui.theme.SecondaryBackground | ||
|
||
@SuppressLint("UnusedMaterialScaffoldPaddingParameter") | ||
@Composable | ||
fun CommemorativeScreen( | ||
navController: NavController, | ||
navBackStackEntry: NavBackStackEntry, | ||
onClickOnGuide: () -> Unit, | ||
) { | ||
val scaffoldState: ScaffoldState = rememberScaffoldState() | ||
Scaffold( | ||
scaffoldState = scaffoldState, | ||
topBar = { TopBar(navController, onClickOnGuide) }, | ||
content = { | ||
CommemorativeScreenBody( | ||
navController, | ||
shareViewModelStoreOwner(navBackStackEntry, navController), | ||
scaffoldState | ||
) | ||
} | ||
) | ||
} | ||
|
||
@Composable | ||
fun CommemorativeScreenBody( | ||
navController: NavController, | ||
viewModelStoreOwner: NavBackStackEntry, | ||
scaffoldState: ScaffoldState | ||
) { | ||
val sharedViewModel: CommemorativeSharedViewModel = getViewModel(viewModelStoreOwner) | ||
val sharedState = sharedViewModel.state.value as CommemorativeSharedState | ||
val localViewModel: CommemorativeViewModel = hiltViewModel() | ||
val localState: CommemorativeState = localViewModel.state.value | ||
localViewModel.setPhotoPath( | ||
sharedState.treasuresProgress.commemorativePhotosByTreasuresDescriptionIds[localState.treasureDesId]!! | ||
) | ||
Column(Modifier.background(SecondaryBackground)) { | ||
Spacer( | ||
modifier = Modifier | ||
.weight(0.01f) | ||
.background(Color.Transparent) | ||
) | ||
if (localState.photoPath != null) { | ||
val photo: Bitmap = BitmapFactory.decodeFile(localState.photoPath) | ||
val aspectRatio = photo.width.toFloat() / photo.height.toFloat() | ||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.weight(0.9f), | ||
contentAlignment = Alignment.BottomEnd | ||
) { | ||
Image( | ||
painter = BitmapPainter(photo.asImageBitmap()), | ||
contentDescription = "Photo from the hunt", | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(10.dp) | ||
.align(Alignment.Center) | ||
.aspectRatio(aspectRatio), | ||
contentScale = ContentScale.FillBounds, | ||
) | ||
DoPhotoButton(sharedViewModel, sharedState, localState) | ||
Image( | ||
painter = painterResource(id = R.drawable.rotate_arc), | ||
contentDescription = "Rotate commemorative photo", | ||
modifier = Modifier | ||
.height(50.dp) | ||
.offset(x = (-10).dp, y = (-70).dp) | ||
.clickable { localViewModel.rotatePhoto() }, | ||
contentScale = ContentScale.Inside | ||
) | ||
} | ||
} | ||
Spacer( | ||
modifier = Modifier | ||
.weight(0.01f) | ||
.background(Color.Transparent) | ||
) | ||
AdvertBanner() | ||
} | ||
} | ||
|
||
@Composable | ||
private fun DoPhotoButton( | ||
sharedViewModel: CommemorativeSharedViewModel, | ||
sharedState: CommemorativeSharedState, | ||
localState: CommemorativeState | ||
) { | ||
val successMsg = stringResource(R.string.photo_replaced) | ||
val failureMsg = stringResource(R.string.photo_not_replaced) | ||
val cameraLauncher = rememberLauncherForActivityResult( | ||
contract = ActivityResultContracts.TakePicture(), | ||
onResult = { success -> | ||
if (success) { | ||
Toast.makeText(App.getAppContext(), successMsg, Toast.LENGTH_SHORT).show() | ||
sharedViewModel.handleDoCommemorativePhotoResult( | ||
sharedState.route.treasures.find { it.id == localState.treasureDesId }!! | ||
)() | ||
} else { | ||
Toast.makeText(App.getAppContext(), failureMsg, Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
) | ||
Image( | ||
painter = painterResource(id = R.drawable.camera_do_photo), | ||
contentDescription = "Do a new commemorative photo", | ||
modifier = Modifier | ||
.height(50.dp) | ||
.offset(x = (-10).dp, y = (-10).dp) | ||
.clickable { cameraLauncher.launch(localState.tempPhotoFileLocation) }, | ||
contentScale = ContentScale.Inside | ||
) | ||
} | ||
|
||
@Composable | ||
private fun getViewModel(viewModelStoreOwner: NavBackStackEntry): CommemorativeSharedViewModel { | ||
val viewModelDoNotInline: SharedViewModel = hiltViewModel(viewModelStoreOwner) | ||
return viewModelDoNotInline | ||
} |
9 changes: 9 additions & 0 deletions
9
...rc/main/java/pl/marianjureczko/poszukiwacz/activity/commemorative/n/CommemorativeState.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package pl.marianjureczko.poszukiwacz.activity.commemorative.n | ||
|
||
import android.net.Uri | ||
|
||
data class CommemorativeState( | ||
val treasureDesId: Int, | ||
val tempPhotoFileLocation: Uri, | ||
val photoPath: String? | ||
) |
52 changes: 52 additions & 0 deletions
52
...ain/java/pl/marianjureczko/poszukiwacz/activity/commemorative/n/CommemorativeViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pl.marianjureczko.poszukiwacz.activity.commemorative.n | ||
|
||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import pl.marianjureczko.poszukiwacz.shared.PhotoHelper | ||
import javax.inject.Inject | ||
|
||
const val PARAMETER_TREASURE_DESCRIPTION_ID = "treasure_description_id" | ||
|
||
@HiltViewModel | ||
class CommemorativeViewModel @Inject constructor( | ||
private val stateHandle: SavedStateHandle, | ||
private val photoHelper: PhotoHelper | ||
) : ViewModel() { | ||
private val TAG = javaClass.simpleName | ||
private var _state: MutableState<CommemorativeState> = mutableStateOf(createState()) | ||
|
||
val state: State<CommemorativeState> | ||
get() = _state | ||
|
||
fun setPhotoPath(photoPath: String) { | ||
if (photoPath != state.value.photoPath) { | ||
_state.value = _state.value.copy( | ||
photoPath = photoPath | ||
) | ||
} | ||
} | ||
|
||
fun rotatePhoto() { | ||
if(state.value.photoPath != null) { | ||
viewModelScope.launch { | ||
PhotoHelper.rotateGraphicClockwise(state.value.photoPath!!) { | ||
// refresh view | ||
_state.value = _state.value.copy() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun createState(): CommemorativeState { | ||
return CommemorativeState(stateHandle.get<Int>( | ||
PARAMETER_TREASURE_DESCRIPTION_ID)!!, | ||
photoHelper.getCommemorativePhotoTempUri(), | ||
null) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.