Pass viewModel into the SaveContent view

This commit is contained in:
Jackson Harper
2022-08-18 19:20:26 +08:00
parent 7c2fa1554f
commit d9d50883c3
3 changed files with 34 additions and 12 deletions

View File

@ -14,23 +14,19 @@ import kotlinx.coroutines.launch
@Composable
@OptIn(ExperimentalMaterialApi::class)
fun SaveContent(extractedText: String?, modalBottomSheetState: ModalBottomSheetState, modifier: Modifier) {
fun SaveContent(viewModel: SaveViewModel, modalBottomSheetState: ModalBottomSheetState, modifier: Modifier) {
val coroutineScope = rememberCoroutineScope()
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
Column(
verticalArrangement = Arrangement.Center,
verticalArrangement = Arrangement.SpaceBetween,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.background(MaterialTheme.colors.background)
.fillMaxSize()
.padding(top = 48.dp, bottom = 32.dp)
) {
val authToken = "authToken"
Text(text = extractedText ?: "no text extracted")
Spacer(modifier = Modifier.height(16.dp))
Text(text = authToken ?: "no auth token")
Text(text = viewModel.message ?: "Saving")
Button(onClick = {
coroutineScope.launch {
modalBottomSheetState.hide()

View File

@ -65,7 +65,7 @@ abstract class SaveSheetActivity: AppCompatActivity() {
sheetState = modalBottomSheetState,
sheetContent = {
BottomSheetUI(coroutineScope, modalBottomSheetState) {
ScreenContent(extractedText, coroutineScope, modalBottomSheetState) {
ScreenContent(viewModel, coroutineScope, modalBottomSheetState) {
onFinish(coroutineScope, modalBottomSheetState)
}
}
@ -157,13 +157,13 @@ abstract class SaveSheetActivity: AppCompatActivity() {
@Composable
fun ScreenContent(
extractedText: String?,
viewModel: SaveViewModel,
coroutineScope: CoroutineScope,
modalBottomSheetState: ModalBottomSheetState,
onExit: () -> Unit?
) {
Box(modifier = Modifier.height(300.dp).background(Color.White)) {
SaveContent(extractedText, modalBottomSheetState, modifier = Modifier.fillMaxSize())
SaveContent(viewModel, modalBottomSheetState, modifier = Modifier.fillMaxSize())
}
}

View File

@ -2,6 +2,9 @@ package app.omnivore.omnivore
import android.content.ContentValues
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.omnivore.omnivore.graphql.generated.SaveUrlMutation
@ -17,13 +20,28 @@ import javax.inject.Inject
class SaveViewModel @Inject constructor(
private val datastoreRepo: DatastoreRepository
): ViewModel() {
var isLoading by mutableStateOf(false)
private set
var message by mutableStateOf<String?>(null)
private set
fun getAuthToken(): String? = runBlocking {
datastoreRepo.getString(DatastoreKeys.omnivoreAuthToken)
}
fun saveURL(url: String) {
viewModelScope.launch {
val apiKey = getAuthToken() ?: ""
isLoading = true
message = "Saving to Omnivore..."
val apiKey = getAuthToken()
if (apiKey == null) {
message = "You are not logged in. Please login before saving."
isLoading = false
return@launch
}
val apolloClient = ApolloClient.Builder()
.serverUrl("${Constants.demoProdURL}/api/graphql")
@ -40,7 +58,15 @@ class SaveViewModel @Inject constructor(
)
).execute()
isLoading = false
val success = (response.data?.saveUrl?.onSaveSuccess?.url != null)
message = if (success) {
"Page Saved"
} else {
"There was an error saving your page"
}
Log.d(ContentValues.TAG, "Saved URL?: ${success.toString()}")
}
}