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

Feature/my shiny feature n 7 #8

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)

Choose a reason for hiding this comment

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

Changing the dispatcher from IO to Default might not be ideal for UI-related work. It's generally recommended to use IO for background tasks that involve disk or network operations.

// 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

Choose a reason for hiding this comment

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

Adding logging is a good practice, but it would be beneficial to have more context about the logs, such as timestamps and log levels (INFO, DEBUG, ERROR). Additionally, consider using a logging library that provides more flexibility.

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,
initialValue = Loading,
started = SharingStarted.WhileSubscribed(5_000),
)

fun saveActivity(context: Context) {
_context = context

Choose a reason for hiding this comment

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

The usage of lateinit for _context can lead to potential null pointer exceptions if the context is not set before use. It's safer to either pass the context through constructor injection or handle it in a more controlled manner.

}
}

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

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 saveActivity method simply assigns the context to a private variable without performing any action. It's unclear what the intended functionality is. Consider adding comments or documentation explaining why this method exists and how it should be used.

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 ->
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
}