-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
355 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 12 additions & 11 deletions
23
app/src/main/java/com/jocmp/basilreader/ui/articles/AccountViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
package com.jocmp.basilreader.ui.articles | ||
|
||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.runtime.toMutableStateList | ||
import androidx.compose.runtime.neverEqualPolicy | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.jocmp.basil.Account | ||
import com.jocmp.basil.AccountManager | ||
import com.jocmp.basil.Feed | ||
import com.jocmp.basil.Folder | ||
import com.jocmp.basil.FeedFormEntry | ||
import kotlinx.coroutines.launch | ||
import java.util.UUID | ||
|
||
class AccountViewModel( | ||
savedStateHandle: SavedStateHandle, | ||
accountManager: AccountManager | ||
) : ViewModel() { | ||
private val args = ArticleArgs(savedStateHandle) | ||
|
||
private val accountState = mutableStateOf(accountManager.findByID(args.accountId)) | ||
private val account: Account | ||
private val accountState = mutableStateOf( | ||
accountManager.findByID(args.accountID), | ||
policy = neverEqualPolicy() | ||
) | ||
|
||
val account: Account | ||
get() = accountState.value | ||
|
||
val feeds = account.feeds.toList() | ||
val folders = account.folders.toList() | ||
|
||
fun addFeed(url: String) { | ||
fun addFeed(entry: FeedFormEntry, onSuccess: () -> Unit) { | ||
viewModelScope.launch { | ||
val nextAccount = accountState.value.copy() | ||
nextAccount.addFeed(url = url) | ||
accountState.value = nextAccount | ||
account.addFeed(entry) | ||
accountState.value = account | ||
onSuccess() | ||
} | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
app/src/main/java/com/jocmp/basilreader/ui/articles/AddFeedDialog.kt
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/jocmp/basilreader/ui/articles/AddFeedScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.jocmp.basilreader.ui.articles | ||
|
||
import androidx.compose.runtime.Composable | ||
import org.koin.androidx.compose.koinViewModel | ||
|
||
@Composable | ||
fun AddFeedScreen( | ||
viewModel: AccountViewModel = koinViewModel(), | ||
onSubmit: (accountID: String) -> Unit, | ||
onCancel: () -> Unit | ||
) { | ||
AddFeedView( | ||
folders = viewModel.folders, | ||
onSubmit = { entry -> | ||
viewModel.addFeed(entry) { | ||
onSubmit(viewModel.account.id) | ||
} | ||
}, | ||
onCancel = onCancel | ||
) | ||
} |
135 changes: 85 additions & 50 deletions
135
app/src/main/java/com/jocmp/basilreader/ui/articles/AddFeedView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,127 @@ | ||
package com.jocmp.basilreader.ui.articles | ||
|
||
import androidx.compose.ui.Alignment | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.material3.DropdownMenuItem | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.ExposedDropdownMenuBox | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Switch | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextButton | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.toMutableStateMap | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import com.jocmp.basil.Feed | ||
import androidx.compose.ui.unit.dp | ||
import com.jocmp.basil.FeedFormEntry | ||
import com.jocmp.basil.Folder | ||
import com.jocmp.basilreader.R | ||
import com.jocmp.basilreader.ui.components.TextField | ||
import kotlin.math.exp | ||
|
||
@Composable | ||
fun AddFeedView( | ||
folders: List<Folder>, | ||
onSubmit: (feed: Feed) -> Unit, | ||
onSubmit: (feed: FeedFormEntry) -> Unit, | ||
onCancel: () -> Unit | ||
) { | ||
val (selectedFolder, selectFolder) = remember { | ||
mutableStateOf<Folder?>(null) | ||
} | ||
|
||
val (url, setURL) = remember { mutableStateOf("") } | ||
val (name, setName) = remember { mutableStateOf("") } | ||
val (addedFolder, setAddedFolder) = remember { mutableStateOf("") } | ||
val switchFolders = remember { | ||
folders.map { it.title to false }.toMutableStateMap() | ||
} | ||
|
||
fun submitFeed() { | ||
val existingFolderNames = switchFolders.filter { it.value }.keys | ||
val folderNames = collectFolders(existingFolderNames, addedFolder) | ||
|
||
onSubmit( | ||
FeedFormEntry( | ||
url = url, | ||
name = name, | ||
folderTitles = folderNames | ||
) | ||
) | ||
} | ||
|
||
Column { | ||
TextField( | ||
value = url, | ||
onValueChange = setURL, | ||
label = { | ||
Text(stringResource(id = R.string.add_feed_url_title)) | ||
}, | ||
supportingText = { | ||
Text(stringResource(R.string.required_placeholder)) | ||
} | ||
) | ||
TextField( | ||
value = name, | ||
onValueChange = setName, | ||
label = { | ||
Text(stringResource(id = R.string.add_feed_name_title)) | ||
} | ||
) | ||
// if (folders.isNotEmpty()) { | ||
// FolderMenuBox( | ||
// selected = selectedFolder, | ||
// folders = folders, | ||
// onFolderSelect = { selectFolder(it) } | ||
// ) | ||
// } | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun FolderMenuBox( | ||
selected: Folder?, | ||
folders: List<Folder>, | ||
onFolderSelect: (folder: Folder) -> Unit, | ||
) { | ||
val (expanded, setExpanded) = mutableStateOf(false) | ||
|
||
ExposedDropdownMenuBox( | ||
expanded = expanded, | ||
onExpandedChange = { setExpanded(!expanded) } | ||
) { | ||
TextField( | ||
readOnly = true, | ||
value = selected?.title ?: "", | ||
onValueChange = {}, | ||
modifier = Modifier.menuAnchor() | ||
value = addedFolder, | ||
onValueChange = setAddedFolder, | ||
placeholder = { | ||
Text(stringResource(id = R.string.add_feed_new_folder_title)) | ||
} | ||
) | ||
ExposedDropdownMenu( | ||
expanded = true, | ||
onDismissRequest = {} | ||
Column(modifier = Modifier.padding(horizontal = 16.dp)) { | ||
switchFolders.forEach { (folderTitle, checked) -> | ||
Row( | ||
horizontalArrangement = Arrangement.SpaceBetween, | ||
verticalAlignment = Alignment.CenterVertically, | ||
modifier = Modifier.fillMaxWidth() | ||
) { | ||
Text(folderTitle) | ||
Switch( | ||
checked = checked, | ||
onCheckedChange = { value -> switchFolders[folderTitle] = value } | ||
) | ||
} | ||
} | ||
} | ||
Row( | ||
horizontalArrangement = Arrangement.End, | ||
modifier = Modifier.fillMaxWidth() | ||
) { | ||
folders.forEach { folder -> | ||
DropdownMenuItem( | ||
text = { Text(folder.title) }, | ||
onClick = { | ||
onFolderSelect(folder) | ||
} | ||
) | ||
TextButton(onClick = onCancel) { | ||
Text(stringResource(R.string.add_feed_cancel)) | ||
} | ||
Button(onClick = { submitFeed() }) { | ||
Text(stringResource(R.string.add_feed_submit)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun collectFolders( | ||
existingFolders: Set<String>, | ||
addedFolder: String | ||
): List<String> { | ||
val folderNames = existingFolders.toMutableList() | ||
|
||
if (addedFolder.isNotBlank()) { | ||
folderNames.add(addedFolder) | ||
} | ||
|
||
return folderNames | ||
} | ||
|
||
@Preview | ||
@Composable | ||
fun AddFeedViewPreview() { | ||
AddFeedView( | ||
folders = emptyList(), | ||
onSubmit = {} | ||
folders = listOf(Folder(title = "Tech")), | ||
onSubmit = {}, | ||
onCancel = {} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletions
13
app/src/main/java/com/jocmp/basilreader/ui/articles/ArticleScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
package com.jocmp.basilreader.ui.articles | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.runtime.Composable | ||
import org.koin.androidx.compose.koinViewModel | ||
|
||
@Composable | ||
fun ArticleScreen( | ||
viewModel: AccountViewModel = koinViewModel(), | ||
onNewFeedNavigate: (accountID: String) -> Unit, | ||
) { | ||
FeedList( | ||
folders = viewModel.folders, | ||
feeds = viewModel.feeds | ||
) | ||
Column { | ||
FeedList( | ||
folders = viewModel.folders, | ||
feeds = viewModel.feeds, | ||
onNewFeedNavigate = { onNewFeedNavigate(viewModel.account.id) } | ||
) | ||
} | ||
} |
Oops, something went wrong.