Skip to content

Commit

Permalink
In-feed save/unsave.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Mar 20, 2024
1 parent c201b02 commit 75b726b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public fun ContentViewerScreen(
KSIcons.BookmarkAdd
},
contentDescription = null,
onClick = { viewModel.toggleSavedForLater() },
onClick = viewModel::toggleSavedForLater,
)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public fun HomeScreen(
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
HomeScreen(
onViewItem = onViewItem,
onSaveButtonClick = viewModel::toggleSavedForLater,
onRefresh = viewModel::refresh,
uiState = uiState,
modifier = modifier,
Expand All @@ -69,6 +70,7 @@ public fun HomeScreen(
@Composable
internal fun HomeScreen(
onViewItem: (FeedItem) -> Unit,
onSaveButtonClick: (FeedItem) -> Unit,
onRefresh: () -> Unit,
uiState: HomeUiState,
modifier: Modifier = Modifier,
Expand Down Expand Up @@ -124,6 +126,7 @@ internal fun HomeScreen(
ContentUi(
items = state.feedItems,
onItemClick = onViewItem,
onSaveButtonClick = onSaveButtonClick,
)
}
}
Expand All @@ -136,6 +139,7 @@ internal fun HomeScreen(
private fun ContentUi(
items: List<HomeFeedItem>,
onItemClick: (FeedItem) -> Unit,
onSaveButtonClick: (FeedItem) -> Unit,
modifier: Modifier = Modifier,
) {
// TODO add popup / tooltip for transient error
Expand Down Expand Up @@ -175,31 +179,31 @@ private fun ContentUi(
KotlinBlogCard(
item = item.toDisplayable(displayablePublishTime),
onItemClick = onItemClick,
onSaveButtonClick = {},
onSaveButtonClick = onSaveButtonClick,
)
}

is FeedItem.KotlinWeekly -> {
KotlinWeeklyCard(
item = item.toDisplayable(displayablePublishTime),
onItemClick = onItemClick,
onSaveButtonClick = {},
onSaveButtonClick = onSaveButtonClick,
)
}

is FeedItem.KotlinYouTube -> {
KotlinYouTubeCard(
item = item.toDisplayable(displayablePublishTime),
onItemClick = onItemClick,
onSaveButtonClick = {},
onSaveButtonClick = onSaveButtonClick,
)
}

is FeedItem.TalkingKotlin -> {
TalkingKotlinCard(
item = item.toDisplayable(displayablePublishTime),
onItemClick = onItemClick,
onSaveButtonClick = {},
onSaveButtonClick = onSaveButtonClick,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.reactivecircus.kstreamlined.kmp.feed.datasource.FeedDataSource
import io.github.reactivecircus.kstreamlined.kmp.feed.sync.FeedSyncEngine
import io.github.reactivecircus.kstreamlined.kmp.model.feed.FeedItem
import io.github.reactivecircus.kstreamlined.kmp.presentation.home.HomePresenter
import io.github.reactivecircus.kstreamlined.kmp.presentation.home.HomeUiState
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -22,4 +23,8 @@ internal class HomeViewModel @Inject constructor(
fun refresh() = viewModelScope.launch {
presenter.refresh()
}

fun toggleSavedForLater(item: FeedItem) = viewModelScope.launch {
presenter.toggleSavedForLater(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public fun KotlinWeeklyIssueScreen(
onShareButtonClick = { url ->
context.openShareSheet(title, url)
},
onSaveButtonClick = { viewModel.toggleSavedForLater() },
onSaveButtonClick = viewModel::toggleSavedForLater,
onOpenLink = {
context.openCustomTab(it)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public fun TalkingKotlinEpisodeScreen(
onShareButtonClick = { title, url ->
context.openShareSheet(title, url)
},
onSaveButtonClick = { viewModel.toggleSavedForLater() },
onSaveButtonClick = viewModel::toggleSavedForLater,
onPlayPauseButtonClick = viewModel::togglePlayPause,
onOpenLink = context::openCustomTab,
onSaveStartPosition = viewModel::saveStartPosition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.reactivecircus.kstreamlined.kmp.presentation.home
import io.github.reactivecircus.kstreamlined.kmp.feed.datasource.FeedDataSource
import io.github.reactivecircus.kstreamlined.kmp.feed.sync.FeedSyncEngine
import io.github.reactivecircus.kstreamlined.kmp.feed.sync.SyncState
import io.github.reactivecircus.kstreamlined.kmp.model.feed.FeedItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -13,7 +14,7 @@ import kotlinx.coroutines.flow.onEach

public class HomePresenter(
private val feedSyncEngine: FeedSyncEngine,
feedDataSource: FeedDataSource,
private val feedDataSource: FeedDataSource,
scope: CoroutineScope,
) {
private val _uiState = MutableStateFlow<HomeUiState>(HomeUiState.Loading)
Expand Down Expand Up @@ -72,4 +73,12 @@ public class HomePresenter(
public suspend fun refresh() {
feedSyncEngine.sync(forceRefresh = true)
}

public suspend fun toggleSavedForLater(feedItem: FeedItem) {
if (!feedItem.savedForLater) {
feedDataSource.addSavedFeedItem(feedItem.id)
} else {
feedDataSource.removeSavedFeedItem(feedItem.id)
}
}
}

0 comments on commit 75b726b

Please sign in to comment.