Skip to content

Commit

Permalink
Remove the Context dependency on 'SettingsViewModel' and move to Toas…
Browse files Browse the repository at this point in the history
…tMessage Composable
  • Loading branch information
yongsuk44 committed Mar 21, 2024
1 parent f9257c9 commit 17bdad1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.android.samples.socialite.ui.home

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -27,6 +28,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
Expand All @@ -38,13 +40,19 @@ fun Settings(
modifier: Modifier = Modifier,
viewModel: SettingsViewModel = hiltViewModel(),
) {
val context = LocalContext.current
val clearHistoryMessage = stringResource(R.string.clear_message_history_success)

Column(
modifier = modifier
.padding(contentPadding)
.padding(32.dp),
) {
TextButton(
onClick = { viewModel.clearMessages() },
onClick = {
viewModel.clearMessages()
Toast.makeText(context, clearHistoryMessage, Toast.LENGTH_SHORT).show()
},
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 56.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@

package com.google.android.samples.socialite.ui.home

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.samples.socialite.data.DatabaseManager
import com.google.android.samples.socialite.repository.ChatRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@HiltViewModel
class SettingsViewModel @Inject constructor(
@ApplicationContext private val application: Context,
private val repository: ChatRepository,
private val databaseManager: DatabaseManager,
) : ViewModel() {
Expand All @@ -42,11 +38,6 @@ class SettingsViewModel @Inject constructor(
withContext(Dispatchers.IO) {
databaseManager.wipeAndReinitializeDatabase()
}
Toast.makeText(
application.applicationContext,
"Messages have been reset",
Toast.LENGTH_SHORT,
).show()
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<!-- Settings -->
<eat-comment />
<string name="clear_message_history">Clear message history</string>
<string name="clear_message_history_success">✨ Messages have been reset</string>

<!-- Video Edit Screen -->
<string name="remove_audio">Remove audio</string>
Expand Down

0 comments on commit 17bdad1

Please sign in to comment.