Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My shiny feature #4 #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class MainActivity : ComponentActivity() {
}
}

viewModel.saveActivity(this)

// Keep the splash screen on-screen until the UI state is loaded. This condition is
// evaluated each time the app needs to be redrawn so it should be fast to avoid blocking
// the UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.samples.apps.nowinandroid

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.samples.apps.nowinandroid.MainActivityUiState.Loading
Expand All @@ -33,13 +34,20 @@ import javax.inject.Inject
class MainActivityViewModel @Inject constructor(
userDataRepository: UserDataRepository,
) : ViewModel() {

private lateinit var _context: Context

val uiState: StateFlow<MainActivityUiState> = userDataRepository.userData.map {
Success(it)
}.stateIn(
scope = viewModelScope,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using lateinit for _context is not ideal because it doesn't enforce non-nullability at compile time. Consider using a nullable property or providing a default value.

initialValue = Loading,
started = SharingStarted.WhileSubscribed(5_000),
)

fun saveActivity(context: Context) {
_context = context
}
}

sealed interface MainActivityUiState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block contains the main structure of the UI components. It looks good and follows best practices.

package com.google.samples.apps.nowinandroid.ui

import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
Expand Down Expand Up @@ -147,6 +148,10 @@ internal fun NiaApp(
)
}

LaunchedEffect(Unit) {
Log.e("#####","info message - App UI")
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NiaNavHost component seems to handle navigation and displaying snackbar messages. Ensure that the onShowSnackbar callback is properly handled to avoid any potential issues with snackbar durations or actions.


NiaNavigationSuiteScaffold(
navigationSuiteItems = {
appState.topLevelDestinations.forEach { destination ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import android.os.Build.VERSION_CODES
import androidx.core.content.getSystemService
import androidx.tracing.trace
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.Default
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.channels.awaitClose
Expand All @@ -40,7 +40,7 @@ import javax.inject.Inject

internal class ConnectivityManagerNetworkMonitor @Inject constructor(
@ApplicationContext private val context: Context,
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
@Dispatcher(Default) private val ioDispatcher: CoroutineDispatcher,
) : NetworkMonitor {
override val isOnline: Flow<Boolean> = callbackFlow {
trace("NetworkMonitor.callbackFlow") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.google.samples.apps.nowinandroid.core.data.repository.SearchContentsR
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
import com.google.samples.apps.nowinandroid.core.model.data.SearchResult
import com.google.samples.apps.nowinandroid.core.model.data.Topic
import com.google.samples.apps.nowinandroid.core.model.data.UserData
import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.UserSearchResult
Expand All @@ -45,11 +46,14 @@ class GetSearchContentsUseCase @Inject constructor(
private fun Flow<SearchResult>.mapToUserSearchResult(userDataStream: Flow<UserData>): Flow<UserSearchResult> =
combine(userDataStream) { searchResult, userData ->
UserSearchResult(
topics = searchResult.topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = topic.id in userData.followedTopics,
)
topics = when (searchResult.topics.size) {
0 -> emptyList()
else -> searchResult.topics.map { topic ->
FollowableTopic(
topic = topic,
isFollowed = topic.id in userData.followedTopics,
)
}
},
newsResources = searchResult.newsResources.map { news ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function mapToUserSearchResult is concise, but it could be more readable if each step of the transformation was broken down into a separate line. This would make it easier to follow and maintain.

UserNewsResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ sealed interface RecentSearchQueriesUiState {
data class Success(
val recentQueries: List<RecentSearchQuery> = emptyList(),
) : RecentSearchQueriesUiState

data class Error(
val message: Strng
) : RecentSearchQueriesUiState
}