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

Bookmarking Stories to Read Later #74

Merged
merged 16 commits into from
Jul 15, 2024
Merged
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
10 changes: 10 additions & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
alias(libs.plugins.kotlin.android)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.kotlin.ksp)
alias(libs.plugins.emerge)
}

Expand All @@ -28,6 +29,11 @@ android {
isDebuggable = true
applicationIdSuffix = ".debug"
}
create("fast") {
isDebuggable = false
applicationIdSuffix = ".fast"
signingConfig = signingConfigs.getByName("debug")
}
release {
isMinifyEnabled = true
isShrinkResources = true
Expand Down Expand Up @@ -88,6 +94,10 @@ dependencies {
implementation(libs.retrofit.kotlinx.serialization)
implementation(libs.kotlinx.serialization.json)

implementation(libs.androidx.room)
implementation(libs.androidx.room.ktx)
ksp(libs.androidx.room.compiler)

implementation(libs.accompanist.webview)

testImplementation(libs.junit)
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name="com.emergetools.HackerNewsApplication"
android:name=".HackerNewsApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down
118 changes: 118 additions & 0 deletions android/app/src/main/java/com/emergetools/hackernews/AppActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.emergetools.hackernews

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideIn
import androidx.compose.animation.slideOut
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.IntOffset
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.emergetools.hackernews.data.ChromeTabsProvider
import com.emergetools.hackernews.data.LocalCustomTabsIntent
import com.emergetools.hackernews.features.bookmarks.BookmarksNavigation
import com.emergetools.hackernews.features.bookmarks.bookmarksRoutes
import com.emergetools.hackernews.features.comments.commentsRoutes
import com.emergetools.hackernews.features.settings.settingsRoutes
import com.emergetools.hackernews.features.stories.Stories
import com.emergetools.hackernews.features.stories.StoriesDestinations.Feed
import com.emergetools.hackernews.features.stories.storiesGraph
import com.emergetools.hackernews.ui.theme.HackerNewsTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
HackerNewsTheme {
ChromeTabsProvider {
App()
}
}
}
}
}

@Composable
fun rememberNavController(
onDestinationChanged: (NavDestination) -> Unit
): NavHostController {
return rememberNavController().apply {
addOnDestinationChangedListener { _, destination, _ ->
onDestinationChanged(destination)
}
}
}

@Composable
fun App() {
val model = viewModel<AppViewModel>()
val state by model.state.collectAsState()
val navController = rememberNavController() { destination ->
model.actions(AppAction.DestinationChanged(destination))
}
Scaffold(
bottomBar = {
NavigationBar {
state.navItems.forEach { navItem ->
NavigationBarItem(
selected = navItem.selected,
onClick = {
model.actions(AppAction.NavItemSelected(navItem))

navController.navigate(navItem.route) {
popUpTo<Feed> {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
icon = {
Icon(
painter = painterResource(navItem.icon),
contentDescription = navItem.label
)
},
)
}
}
}
) { innerPadding ->
NavHost(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
navController = navController,
enterTransition = { slideIn { IntOffset(x = it.width, y = 0) } },
exitTransition = { slideOut { IntOffset(x = -it.width / 3, y = 0) } + fadeOut() },
popEnterTransition = { slideIn { IntOffset(x = -it.width, y = 0) } },
popExitTransition = { slideOut { IntOffset(x = it.width, y = 0) } },
startDestination = Stories
) {
storiesGraph(navController)
commentsRoutes()
bookmarksRoutes(navController)
settingsRoutes()
}
}
}
104 changes: 104 additions & 0 deletions android/app/src/main/java/com/emergetools/hackernews/AppDomain.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.emergetools.hackernews

import androidx.annotation.DrawableRes
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Menu
import androidx.compose.material.icons.rounded.Settings
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.lifecycle.ViewModel
import androidx.navigation.NavDestination
import com.emergetools.hackernews.features.bookmarks.BookmarksDestinations
import com.emergetools.hackernews.features.bookmarks.BookmarksDestinations.Bookmarks
import com.emergetools.hackernews.features.settings.SettingsDestinations.Settings
import com.emergetools.hackernews.features.stories.Stories
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

data class NavItem(
@DrawableRes
val icon: Int,
val label: String,
val route: Any,
val selected: Boolean
)

data class AppState(
val navItems: List<NavItem> = listOf(
NavItem(
icon = R.drawable.ic_feed,
label = "Feed",
route = Stories,
selected = true
),
NavItem(
icon = R.drawable.ic_bookmarks,
label = "Bookmarks",
route = Bookmarks,
selected = false
),
NavItem(
icon = R.drawable.ic_settings,
label = "Settings",
route = Settings,
selected = false
),
)
) {
val selectedItem = navItems.first { it.selected }
val topLevelRoutes = navItems.associateBy { it.route.javaClass.simpleName }
}

sealed interface AppAction {
data class NavItemSelected(val item: NavItem) : AppAction
data class DestinationChanged(val destination: NavDestination) : AppAction
}

class AppViewModel : ViewModel() {
private val internalState = MutableStateFlow(AppState())
val state = internalState.asStateFlow()

fun actions(action: AppAction) {
when (action) {
is AppAction.NavItemSelected -> {
if (action.item != internalState.value.selectedItem) {
internalState.update { current ->
current.copy(
navItems = current.navItems.map { item ->
if (action.item == item) {
item.copy(selected = true)
} else {
item.copy(selected = false)
}
}
)
}
}
}

is AppAction.DestinationChanged -> {
// TODO: figure out a better way to sync the current destination with bottom nav
val currentState = internalState.value
val parent = action.destination.parent?.route
val route = parent ?: action.destination.route
currentState.topLevelRoutes.keys.forEach { key ->
if (route != null && route.contains(key)) {
val item = currentState.topLevelRoutes[key]
if (item != currentState.selectedItem) {
// select bottom nav
internalState.value = currentState.copy(
navItems = currentState.navItems.map { navItem ->
if (item == navItem) {
navItem.copy(selected = true)
} else {
navItem.copy(selected = false)
}
}
)
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.emergetools
package com.emergetools.hackernews

import android.app.Application
import android.content.Context
import androidx.room.Room
import com.emergetools.hackernews.data.BookmarkDao
import com.emergetools.hackernews.data.HackerNewsBaseDataSource
import com.emergetools.hackernews.data.HackerNewsDatabase
import com.emergetools.hackernews.data.HackerNewsSearchClient
import com.emergetools.hackernews.data.ItemRepository
import kotlinx.serialization.json.Json
Expand All @@ -18,6 +21,20 @@ class HackerNewsApplication: Application() {
private val baseClient = HackerNewsBaseDataSource(json, httpClient)
val searchClient = HackerNewsSearchClient(json, httpClient)
val itemRepository = ItemRepository(baseClient)

lateinit var bookmarkDao: BookmarkDao

override fun onCreate() {
super.onCreate()

val db = Room.databaseBuilder(
applicationContext,
HackerNewsDatabase::class.java,
"hackernews",
).build()

bookmarkDao = db.bookmarkDao()
}
}

fun Context.itemRepository(): ItemRepository {
Expand All @@ -27,3 +44,7 @@ fun Context.itemRepository(): ItemRepository {
fun Context.searchClient(): HackerNewsSearchClient {
return (this.applicationContext as HackerNewsApplication).searchClient
}

fun Context.bookmarkDao(): BookmarkDao {
return (this.applicationContext as HackerNewsApplication).bookmarkDao
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.emergetools.hackernews.data

import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.staticCompositionLocalOf

private val sharedIntent = CustomTabsIntent.Builder().build()
val LocalCustomTabsIntent = staticCompositionLocalOf<CustomTabsIntent> {
error("LocalCustomTabsIntent not provided")
}

@Composable
fun ChromeTabsProvider(
content: @Composable () -> Unit
) {
CompositionLocalProvider(
LocalCustomTabsIntent provides sharedIntent
) {
content()
}
}

Loading
Loading