only create guest session when trying to rate

This commit is contained in:
Owen LeJeune
2022-03-06 15:21:33 -05:00
parent 4f664bb1a3
commit 6ce7f3fa5c
5 changed files with 65 additions and 14 deletions

View File

@@ -7,13 +7,15 @@ import retrofit2.Response
import retrofit2.http.Body import retrofit2.http.Body
import retrofit2.http.DELETE import retrofit2.http.DELETE
import retrofit2.http.GET import retrofit2.http.GET
import retrofit2.http.HTTP
interface AuthenticationApi { interface AuthenticationApi {
@GET("authentication/guest_session/new") @GET("authentication/guest_session/new")
suspend fun getNewGuestSession(): Response<GuestSessionResponse> suspend fun getNewGuestSession(): Response<GuestSessionResponse>
@DELETE("authentication/session") // @DELETE("authentication/session")
@HTTP(method = "DELETE", path = "authentication/session", hasBody = true)
suspend fun deleteSession(@Body body: DeleteSessionBody): Response<DeleteSessionResponse> suspend fun deleteSession(@Body body: DeleteSessionBody): Response<DeleteSessionResponse>
} }

View File

@@ -296,7 +296,7 @@ private fun RateButton(
}) })
CreateSessionDialog(showDialog = showSessionDialog, onSessionReturned = { CreateSessionDialog(showDialog = showSessionDialog, onSessionReturned = {
showRatingDialog.value = it
}) })
} }
@@ -306,19 +306,46 @@ private fun CreateSessionDialog(showDialog: MutableState<Boolean>, onSessionRetu
AlertDialog( AlertDialog(
modifier = Modifier.wrapContentHeight(), modifier = Modifier.wrapContentHeight(),
onDismissRequest = { showDialog.value = false }, onDismissRequest = { showDialog.value = false },
title = { Text(text = "Sign In") }, title = { Text(text = stringResource(R.string.sign_in_dialog_title)) },
confirmButton = {}, confirmButton = {},
dismissButton = { dismissButton = {
Button( TextButton(
modifier = Modifier.height(40.dp), modifier = Modifier.height(40.dp),
onClick = { onClick = {
showDialog.value = false showDialog.value = false
} }
) { ) {
Text(stringResource(R.string.action_cancel)) Text(text = stringResource(R.string.action_cancel))
} }
}, },
text = {} text = {
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Button(
modifier = Modifier.fillMaxWidth(),
onClick = {
CoroutineScope(Dispatchers.IO).launch {
SessionManager.requestNewGuestSession()?.let {
withContext(Dispatchers.Main) {
showDialog.value = false
onSessionReturned(true)
}
}
}
}
) {
Text(text = stringResource(R.string.action_continue_as_guest))
}
Button(
modifier = Modifier.fillMaxWidth(),
onClick = {
showDialog.value = false
}
) {
Text(text = stringResource(R.string.action_sign_in))
}
}
}
) )
} }
} }

View File

@@ -1,5 +1,6 @@
package com.owenlejeune.tvtime.ui.screens.tabs.bottom package com.owenlejeune.tvtime.ui.screens.tabs.bottom
import android.widget.Toast
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
@@ -17,6 +18,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@@ -93,6 +95,7 @@ fun SettingsTab(preferences: AppPreferences = get(AppPreferences::class.java)) {
@Composable @Composable
private fun DebugOptions(preferences: AppPreferences = get(AppPreferences::class.java)) { private fun DebugOptions(preferences: AppPreferences = get(AppPreferences::class.java)) {
val context = LocalContext.current
val shouldShowPalette = remember { mutableStateOf(false) } val shouldShowPalette = remember { mutableStateOf(false) }
Text( Text(
text = "Show material palette", text = "Show material palette",
@@ -117,7 +120,9 @@ private fun DebugOptions(preferences: AppPreferences = get(AppPreferences::class
.clickable( .clickable(
onClick = { onClick = {
preferences.guestSessionId = "" preferences.guestSessionId = ""
SessionManager.clearSession() SessionManager.clearSession {
Toast.makeText(context, "Cleared session: $it", Toast.LENGTH_SHORT).show()
}
} }
) )
) )

View File

@@ -1,13 +1,14 @@
package com.owenlejeune.tvtime.utils package com.owenlejeune.tvtime.utils
import com.owenlejeune.tvtime.api.tmdb.GuestSessionApi
import com.owenlejeune.tvtime.api.tmdb.TmdbClient import com.owenlejeune.tvtime.api.tmdb.TmdbClient
import com.owenlejeune.tvtime.api.tmdb.model.DeleteSessionBody
import com.owenlejeune.tvtime.api.tmdb.model.RatedEpisode import com.owenlejeune.tvtime.api.tmdb.model.RatedEpisode
import com.owenlejeune.tvtime.api.tmdb.model.RatedMedia
import com.owenlejeune.tvtime.api.tmdb.model.RatedMovie import com.owenlejeune.tvtime.api.tmdb.model.RatedMovie
import com.owenlejeune.tvtime.api.tmdb.model.RatedTv import com.owenlejeune.tvtime.api.tmdb.model.RatedTv
import com.owenlejeune.tvtime.preferences.AppPreferences import com.owenlejeune.tvtime.preferences.AppPreferences
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject import org.koin.core.component.inject
@@ -22,8 +23,22 @@ object SessionManager: KoinComponent {
private val authenticationService by lazy { TmdbClient().createAuthenticationService() } private val authenticationService by lazy { TmdbClient().createAuthenticationService() }
fun clearSession() { fun clearSession(onResponse: (isSuccessful: Boolean) -> Unit) {
_currentSession = null currentSession?.let { session ->
CoroutineScope(Dispatchers.IO).launch {
val deleteResponse = authenticationService.deleteSession(
DeleteSessionBody(
session.sessionId
)
)
withContext(Dispatchers.Main) {
if (deleteResponse.isSuccessful) {
_currentSession = null
}
onResponse(deleteResponse.isSuccessful)
}
}
}
} }
suspend fun initialize() { suspend fun initialize() {
@@ -34,7 +49,7 @@ object SessionManager: KoinComponent {
} }
} }
private suspend fun requestNewGuestSession(): Session? { suspend fun requestNewGuestSession(): Session? {
val response = authenticationService.getNewGuestSession() val response = authenticationService.getNewGuestSession()
if (response.isSuccessful) { if (response.isSuccessful) {
preferences.guestSessionId = response.body()?.guestSessionId ?: "" preferences.guestSessionId = response.body()?.guestSessionId ?: ""
@@ -78,10 +93,9 @@ object SessionManager: KoinComponent {
override var _ratedTvShows: List<RatedTv> = emptyList() override var _ratedTvShows: List<RatedTv> = emptyList()
override var _ratedTvEpisodes: List<RatedEpisode> = emptyList() override var _ratedTvEpisodes: List<RatedEpisode> = emptyList()
private lateinit var service: GuestSessionApi private val service by lazy { TmdbClient().createGuestSessionService() }
override suspend fun initialize() { override suspend fun initialize() {
service = TmdbClient().createGuestSessionService()
refresh() refresh()
} }

View File

@@ -35,6 +35,9 @@
<string name="updated_at_label">Updated at: %1$s</string> <string name="updated_at_label">Updated at: %1$s</string>
<string name="no_reviews_label">No reviews</string> <string name="no_reviews_label">No reviews</string>
<string name="sign_in_dialog_title">Sign In to Rate</string>
<string name="action_continue_as_guest">Continue as Guest</string>
<string name="action_sign_in">Sign In</string>
<string name="rate_action_label">Rate</string> <string name="rate_action_label">Rate</string>
<string name="delete_rating_action_label">Delete Rating</string> <string name="delete_rating_action_label">Delete Rating</string>
<string name="add_to_list_action_label">Add to List</string> <string name="add_to_list_action_label">Add to List</string>