Skip to content

Commit

Permalink
Get empty accounts state working again
Browse files Browse the repository at this point in the history
- Ensure that the user is redirected to the Accounts
Index when there are no accounts
- Adds a landing page for feeds
  • Loading branch information
jocmp committed Dec 18, 2023
1 parent 07cbb53 commit a64d39a
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 60 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.21"
}

android {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/jocmp/basilreader/Async.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jocmp.basilreader

sealed class Async<out T>(private val value: T?) {
open operator fun invoke(): T? = value

data object Uninitialized : Async<Nothing>(value = null)

data object Loading : Async<Nothing>(value = null)

data class Success<out T>(private val value: T) : Async<T>(value = value) {
override operator fun invoke(): T = value
}

data class Failure<out T>(val error: Throwable, private val value: T? = null) : Async<T>(value = value)
}
41 changes: 21 additions & 20 deletions app/src/main/java/com/jocmp/basilreader/ui/App.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
package com.jocmp.basilreader.ui

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.Stable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.neverEqualPolicy
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.datastore.preferences.core.Preferences
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import com.jocmp.basil.Account
import com.jocmp.basil.AccountManager
import com.jocmp.basilreader.selectAccount
import com.jocmp.basilreader.selectedAccount
import com.jocmp.basilreader.settings
import com.jocmp.basilreader.ui.accounts.accountIndex
import com.jocmp.basilreader.ui.accounts.navigateToAddAccount
import com.jocmp.basilreader.ui.articles.articleGraph
import com.jocmp.basilreader.ui.articles.navigateToArticles
import com.jocmp.basilreader.ui.theme.BasilReaderTheme
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.koin.compose.koinInject

@Composable
fun App() {
val composableScope = rememberCoroutineScope()
val navController = rememberNavController()
val accountManager = koinInject<AccountManager>()

val (checkedExistingAccounts, setExistingAccountsChecked) = rememberSaveable {
mutableStateOf(false)
}

BasilReaderTheme {
Surface(
Expand All @@ -47,13 +40,21 @@ fun App() {
startDestination = "articles"
) {
accountIndex(
onNavigate = {}
onSelect = {
navController.navigateToArticles()
}
)
articleGraph(navController = navController)
}
}
}
}
data class AccountState(val id: String)

val LocalAccount = compositionLocalOf<Account?> { null }
LaunchedEffect(checkedExistingAccounts) {
composableScope.launch {
if (!checkedExistingAccounts && accountManager.isEmpty()) {
navController.navigateToAddAccount()
}
setExistingAccountsChecked(true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,48 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import com.jocmp.basil.Account
import androidx.compose.ui.platform.LocalContext
import com.jocmp.basil.AccountManager
import com.jocmp.basil.AccountManager.AccountSummary
import kotlinx.coroutines.coroutineScope
import com.jocmp.basilreader.selectAccount
import com.jocmp.basilreader.settings
import kotlinx.coroutines.launch
import org.koin.androidx.compose.koinViewModel
import org.koin.compose.koinInject

@Composable
fun AccountIndexView(
accountManager: AccountManager = koinInject(),
onNavigate: (accountID: String) -> Unit
onSelect: () -> Unit
) {
val context = LocalContext.current
val composableScope = rememberCoroutineScope()
val accountsState = remember { mutableStateListOf<AccountSummary>() }.also {
val accountsState = remember { mutableStateListOf<AccountSummary>() }

LaunchedEffect(Unit) {
composableScope.launch {
it.addAll(accountManager.latestSummaries())
accountsState.addAll(accountManager.latestSummaries())
}
}

val accounts = accountsState.toList()

Column {
Button(onClick = { accountManager.createAccount() }) {
Button(onClick = { accountsState.add(accountManager.createAccount()) }) {
Text("+")
}
LazyColumn {
items(accounts, key = { it.id }) { account ->
Row(modifier = Modifier.clickable { onNavigate(account.id) }) {
Row(modifier = Modifier.clickable {
composableScope.launch {
context.settings.selectAccount(account.id)
onSelect()
}
}) {
Text(account.displayName)
Button(onClick = { accountManager.removeAccount(account.id) }) {
Text("x")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.jocmp.basilreader.ui.accounts

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import com.jocmp.basil.Account

fun NavGraphBuilder.accountIndex(
onNavigate: (accountID: String) -> Unit
onSelect: () -> Unit
) {
composable("accounts") {
AccountIndexView(onNavigate = onNavigate)
AccountIndexView(onSelect = onSelect)
}
}

fun NavController.navigateToAddAccount() {
navigate("accounts")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.jocmp.basil.AccountManager
import com.jocmp.basil.Feed
import com.jocmp.basil.FeedFormEntry
import com.jocmp.basil.Folder
import com.jocmp.basilreader.Async
import com.jocmp.basilreader.selectAccount
import com.jocmp.basilreader.selectedAccount
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -50,7 +51,7 @@ class AccountViewModel(
val feeds: List<Feed>
get() = account?.feeds?.toList() ?: emptyList()

fun selectAccount(accountID: String) {
private fun selectAccount(accountID: String) {
viewModelScope.launch {
settings.selectAccount(accountID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptions
import androidx.navigation.compose.composable

fun articlesRoute() = "articles"
const val articlesRoute = "articles"

fun feedRoute(feedID: String) = "feeds/${feedID}"

fun NavController.navigateToFeed(
feedID: String,
accountID: String,
navOptions: NavOptions? = null
) = navigate(feedRoute(accountID), navOptions)
fun NavController.navigateToArticles() =
navigate(articlesRoute)

fun NavController.navigateToFeed(feedID: String, navOptions: NavOptions? = null) =
navigate(feedRoute(feedID), navOptions)

fun NavController.navigateToAddFeed() =
navigate("feeds/new")
Expand All @@ -28,6 +28,9 @@ fun NavGraphBuilder.articleGraph(
ArticleScreen(
onFeedAdd = {
navController.navigateToAddFeed()
},
onFeedSelect = {
navController.navigateToFeed(feedID = it)
}
)
}
Expand All @@ -39,8 +42,8 @@ fun NavGraphBuilder.articleGraph(
navController.popBackStack()
},
onSubmit = {
navController.navigate(articlesRoute()) {
popUpTo(articlesRoute()) {
navController.navigate(articlesRoute) {
popUpTo(articlesRoute) {
inclusive = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import org.koin.androidx.compose.koinViewModel
fun ArticleScreen(
viewModel: AccountViewModel = koinViewModel(),
onFeedAdd: () -> Unit,
onFeedSelect: (feedID: String) -> Unit
) {
Column {
FeedList(
folders = viewModel.folders,
feeds = viewModel.feeds,
onFeedAdd = onFeedAdd,
onFeedSelect = {}
onFeedSelect = onFeedSelect
)
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.jocmp.basilreader.ui.articles

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jocmp.basil.Feed
import com.jocmp.basil.Folder
import com.jocmp.basilreader.ui.fixtures.FeedPreviewFixture
Expand All @@ -30,6 +35,9 @@ fun FeedList(
onFeedSelect = onFeedSelect
)
}
Box(modifier = Modifier.padding(horizontal = 8.dp)) {
Text("Feeds", fontWeight = FontWeight.Bold)
}
feeds.forEach {
FeedRow(
feed = it,
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/com/jocmp/basilreader/ui/articles/FeedRow.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package com.jocmp.basilreader.ui.articles

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.jocmp.basil.Feed

@Composable
fun FeedRow(
feed: Feed,
onSelect: (id: String) -> Unit
) {
Text("${feed.name} (${feed.feedURL})")
Box(
modifier = Modifier
.fillMaxWidth()
.clickable {
onSelect(feed.id)
}
) {
Text(
"${feed.name} (${feed.feedURL})",
modifier = Modifier.padding(8.dp)
)
}
}
10 changes: 8 additions & 2 deletions app/src/main/java/com/jocmp/basilreader/ui/articles/FolderRow.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.jocmp.basilreader.ui.articles

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jocmp.basil.Folder

@Composable
fun FolderRow(
folder: Folder,
onFeedSelect: (feedID: String) -> Unit
) {
Text(folder.title)
Box(modifier = Modifier.padding(horizontal = 8.dp)) {
Text(folder.title, fontWeight = FontWeight.Bold)
}
folder.feeds.forEach { feed ->
Row {
Text("- ")
FeedRow(feed = feed, onSelect = onFeedSelect)
}
}
Expand Down
1 change: 0 additions & 1 deletion basil/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
kotlin("plugin.serialization") version "1.9.21"
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("com.google.devtools.ksp") version libs.versions.ksp
Expand Down
16 changes: 11 additions & 5 deletions basil/src/main/java/com/jocmp/basil/AccountManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class AccountManager(
return latestSummaries().map { it.id }
}

fun createAccount(): Account {
fun isEmpty(): Boolean {
return accountSize() == 0
}

fun accountSize(): Int {
return listAccounts().size
}

fun createAccount(): AccountSummary {
val accountID = UUID.randomUUID().toString()

accountFolder().apply {
Expand All @@ -44,11 +52,9 @@ class AccountManager(
}
}

val folder = accountFile(accountID).apply {
mkdir()
}
accountFile(accountID).mkdir()

return buildAccount(id = accountID, path = folder)
return AccountSummary(id = accountID, AccountPreferences(displayName = "Feedbin"))
}

fun removeAccount(accountID: String) {
Expand Down
3 changes: 0 additions & 3 deletions basil/src/main/java/com/jocmp/basil/AccountPreferences.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.jocmp.basil

import kotlinx.serialization.Serializable

@Serializable
data class AccountPreferences(
val displayName: String
)
Loading

0 comments on commit a64d39a

Please sign in to comment.