Skip to content

Commit

Permalink
refactor(dataconnect): update schema to match quickstart-js (#1678)
Browse files Browse the repository at this point in the history
* refactor: use the latest schema from quickstart-js

* refactor: connectors -> movie-connector

* docs: update README.md

* refactor: rating is an Int after all :)

* add empty lines at the end of the files
  • Loading branch information
thatfiredev authored Oct 4, 2024
1 parent 68d41be commit 44dccbf
Show file tree
Hide file tree
Showing 28 changed files with 1,212 additions and 1,661 deletions.
4 changes: 2 additions & 2 deletions dataconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ check out the [official documentation](https://firebase.google.com/docs/data-con
4. Click on "Start Emulators" - this should generate the Kotlin SDK for you and start the emulators.
### 4. Populate the database
In VS Code, open the `quickstart-android/dataconnect/dataconnect/data_seed.gql` file and click the
In VS Code, open the `quickstart-android/dataconnect/dataconnect/moviedata_insert.gql` file and click the
`Run (local)` button at the top of the file.
If you’d like to confirm that the data was correctly inserted,
open `quickstart-android/dataconnect/connectors/queries.gql` and run the `ListMovies` query.
open `quickstart-android/dataconnect/movie-connector/queries.gql` and run the `ListMovies` query.
### 5. Running the app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
Expand All @@ -30,10 +29,6 @@ import com.google.firebase.dataconnect.movies.MoviesConnector
import com.google.firebase.dataconnect.movies.instance
import com.google.firebase.example.dataconnect.feature.actordetail.ActorDetailRoute
import com.google.firebase.example.dataconnect.feature.actordetail.ActorDetailScreen
import com.google.firebase.example.dataconnect.feature.genredetail.GenreDetailRoute
import com.google.firebase.example.dataconnect.feature.genredetail.GenreDetailScreen
import com.google.firebase.example.dataconnect.feature.genres.GenresRoute
import com.google.firebase.example.dataconnect.feature.genres.GenresScreen
import com.google.firebase.example.dataconnect.feature.moviedetail.MovieDetailRoute
import com.google.firebase.example.dataconnect.feature.moviedetail.MovieDetailScreen
import com.google.firebase.example.dataconnect.feature.movies.MoviesRoute
Expand All @@ -47,7 +42,6 @@ data class TopLevelRoute<T : Any>(val labelResId: Int, val route: T, val icon: I

val TOP_LEVEL_ROUTES = listOf(
TopLevelRoute(R.string.label_movies, MoviesRoute, Icons.Filled.Home),
TopLevelRoute(R.string.label_genres, GenresRoute, Icons.Filled.Menu),
TopLevelRoute(R.string.label_profile, ProfileRoute, Icons.Filled.Person)
)

Expand Down Expand Up @@ -116,15 +110,6 @@ class MainActivity : ComponentActivity() {
)
}
composable<ActorDetailRoute>() { ActorDetailScreen() }
composable<GenresRoute> {
GenresScreen(onGenreClicked = { genre ->
navController.navigate(
GenreDetailRoute(genre),
{ launchSingleTop = true }
)
})
}
composable<GenreDetailRoute> { GenreDetailScreen() }
searchScreen()
composable<ProfileRoute> { ProfileScreen() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.outlined.FavoriteBorder
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
Expand All @@ -31,7 +28,6 @@ import com.google.firebase.example.dataconnect.ui.components.ErrorCard
import com.google.firebase.example.dataconnect.ui.components.LoadingScreen
import com.google.firebase.example.dataconnect.ui.components.Movie
import com.google.firebase.example.dataconnect.ui.components.MoviesList
import com.google.firebase.example.dataconnect.ui.components.ToggleButton
import kotlinx.serialization.Serializable


Expand All @@ -47,18 +43,14 @@ fun ActorDetailScreen(
uiState = uiState,
onMovieClicked = {
// TODO
},
onFavoriteToggled = {
actorDetailViewModel.toggleFavorite(it)
}
)
}

@Composable
fun ActorDetailScreen(
uiState: ActorDetailUIState,
onMovieClicked: (actorId: String) -> Unit,
onFavoriteToggled: (newValue: Boolean) -> Unit
onMovieClicked: (actorId: String) -> Unit
) {
when (uiState) {
is ActorDetailUIState.Error -> ErrorCard(uiState.errorMessage)
Expand All @@ -75,8 +67,6 @@ fun ActorDetailScreen(
) {
ActorInformation(
actor = uiState.actor,
isActorFavorite = uiState.isFavorite,
onFavoriteToggled = onFavoriteToggled
)
MoviesList(
listTitle = stringResource(R.string.title_main_roles),
Expand All @@ -103,9 +93,7 @@ fun ActorDetailScreen(
@Composable
fun ActorInformation(
modifier: Modifier = Modifier,
actor: GetActorByIdQuery.Data.Actor?,
isActorFavorite: Boolean,
onFavoriteToggled: (newValue: Boolean) -> Unit
actor: GetActorByIdQuery.Data.Actor?
) {
if (actor == null) {
ErrorCard(stringResource(R.string.error_actor_not_found))
Expand All @@ -128,22 +116,14 @@ fun ActorInformation(
.aspectRatio(9f / 16f)
.padding(vertical = 8.dp)
)
// TODO(thatfiredev): Remove biography
Text(
text = actor.biography ?: stringResource(R.string.biography_not_available),
text = stringResource(R.string.biography_not_available),
modifier = Modifier
.padding(horizontal = 16.dp)
.fillMaxWidth()
)
}
Spacer(modifier = Modifier.height(8.dp))
ToggleButton(
iconEnabled = Icons.Filled.Favorite,
iconDisabled = Icons.Outlined.FavoriteBorder,
textEnabled = stringResource(R.string.button_remove_favorite),
textDisabled = stringResource(R.string.button_favorite),
isEnabled = isActorFavorite,
onToggle = onFavoriteToggled
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ sealed class ActorDetailUIState {
// Actor is null if it can't be found on the DB
val actor: GetActorByIdQuery.Data.Actor?,
val isUserSignedIn: Boolean = false,
var isFavorite: Boolean = false
) : ActorDetailUIState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,10 @@ class ActorDetailViewModel(
id = UUID.fromString(actorId)
).data.actor

_uiState.value = if (user == null) {
ActorDetailUIState.Success(actor, isUserSignedIn = false)
} else {
val favoriteActor = moviesConnector.getIfFavoritedActor.execute(
id = user.uid,
actorId = UUID.fromString(actorId)
).data.favoriteActor

val isFavorite = favoriteActor != null

ActorDetailUIState.Success(
actor,
isUserSignedIn = true,
isFavorite = isFavorite
)
}
} catch (e: Exception) {
_uiState.value = ActorDetailUIState.Error(e.message)
}
}
}

fun toggleFavorite(newValue: Boolean) {
// TODO(thatfiredev): hide the button if there's no user logged in
val uid = firebaseAuth.currentUser?.uid ?: return
viewModelScope.launch {
try {
if (newValue) {
moviesConnector.addFavoritedActor.execute(UUID.fromString(actorId))
} else {
moviesConnector.deleteFavoriteActor.execute(
userId = uid,
actorId = UUID.fromString(actorId)
)
}
// Re-run the query to fetch the actor details
fetchActor()
_uiState.value = ActorDetailUIState.Success(
actor = actor,
isUserSignedIn = user != null
)
} catch (e: Exception) {
_uiState.value = ActorDetailUIState.Error(e.message)
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 44dccbf

Please sign in to comment.