Skip to content

Commit

Permalink
Improve AddFeedDialog looking and behaviour
Browse files Browse the repository at this point in the history
- Add error management for URL TextField
- Add account dropdown
  • Loading branch information
Shinokuni committed Jan 13, 2024
1 parent 76cff80 commit 672de76
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.readrops.api.localfeed

import android.accounts.NetworkErrorException
import androidx.annotation.WorkerThread
import com.gitlab.mvysny.konsumexml.Konsumer
import com.gitlab.mvysny.konsumexml.konsumeXml
Expand All @@ -22,7 +21,6 @@ import okio.Buffer
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import java.io.IOException
import java.lang.Exception
import java.net.HttpURLConnection

class LocalRSSDataSource(private val httpClient: OkHttpClient) : KoinComponent {
Expand Down Expand Up @@ -75,7 +73,8 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) : KoinComponent {
val rootKonsumer = nextElement(LocalRSSHelper.RSS_ROOT_NAMES)
rootKonsumer?.let { type = LocalRSSHelper.guessRSSType(rootKonsumer) }
} catch (e: Exception) {
throw UnknownFormatException(e.message)
close()
return false
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val composeAppModule = module {

viewModel { TimelineViewModel(get(), get()) }

viewModel { FeedViewModel(get(), get()) }
viewModel { FeedViewModel(get(), get(), get()) }

viewModel { AccountSelectionViewModel(get()) }

Expand Down
167 changes: 139 additions & 28 deletions appcompose/src/main/java/com/readrops/app/compose/feeds/AddFeedDialog.kt
Original file line number Diff line number Diff line change
@@ -1,59 +1,170 @@
package com.readrops.app.compose.feeds

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material3.Card
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.readrops.app.compose.R
import com.readrops.app.compose.util.theme.LargeSpacer
import com.readrops.app.compose.util.theme.MediumSpacer
import com.readrops.app.compose.util.theme.ShortSpacer
import com.readrops.app.compose.util.theme.spacing

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AddFeedDialog(
viewModel: FeedViewModel,
onDismiss: () -> Unit,
onValidate: (String) -> Unit,
) {
var url by remember { mutableStateOf("") }
val state by viewModel.addFeedDialogState.collectAsStateWithLifecycle()

var isExpanded by remember { mutableStateOf(false) }

if (state.closeDialog) {
onDismiss()
}

Dialog(
onDismissRequest = onDismiss
) {
Column(
modifier = Modifier
.background(MaterialTheme.colorScheme.background)
.padding(16.dp)
Card(
shape = RoundedCornerShape(16.dp),
) {
Text(
text = "Add new feed",
style = MaterialTheme.typography.headlineSmall
)

Spacer(modifier = Modifier.size(8.dp))

TextField(
value = url,
onValueChange = { url = it }
)

Spacer(modifier = Modifier.size(8.dp))

Button(
onClick = { onValidate(url) },
modifier = Modifier.align(Alignment.CenterHorizontally)
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.background)
.padding(MaterialTheme.spacing.largeSpacing)
) {
Text(text = "Validate")
Icon(
painter = painterResource(id = R.drawable.ic_rss_feed_grey),
contentDescription = null,
modifier = Modifier.size(MaterialTheme.spacing.largeSpacing)
)

MediumSpacer()

Text(
text = "Add new feed",
style = MaterialTheme.typography.headlineSmall
)

MediumSpacer()

OutlinedTextField(
value = state.url,
label = {
Text(text = "URL")
},
onValueChange = { viewModel.setAddFeedDialogURL(it) },
singleLine = true,
trailingIcon = {
if (state.url.isNotEmpty()) {
IconButton(
onClick = { viewModel.setAddFeedDialogURL("") }
) {
Icon(
imageVector = Icons.Default.Clear,
contentDescription = null
)
}
}
},
isError = state.isError(),
supportingText = { Text(state.errorText) }
)

ShortSpacer()

ExposedDropdownMenuBox(
expanded = isExpanded,
onExpandedChange = { isExpanded = isExpanded.not() }
) {
ExposedDropdownMenu(
expanded = isExpanded,
onDismissRequest = { isExpanded = false }
) {
for (account in state.accounts) {
DropdownMenuItem(
text = { Text(text = account.accountName!!) },
onClick = {
isExpanded = false
viewModel.setAddFeedDialogSelectedAccount(account)
},
leadingIcon = {
Icon(
painter = painterResource(
id = if (state.selectedAccount.isLocal){
R.drawable.ic_rss_feed_grey}
else
state.selectedAccount.accountType!!.iconRes
),
contentDescription = null
)
}
)
}
}

OutlinedTextField(
value = state.selectedAccount.accountName!!,
readOnly = true,
onValueChange = {},
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(expanded = isExpanded)
},
leadingIcon = {
Icon(
painter = painterResource(
id = if (state.selectedAccount.isLocal){
R.drawable.ic_rss_feed_grey}
else
state.selectedAccount.accountType!!.iconRes
),
contentDescription = null
)
},
modifier = Modifier.menuAnchor()
)
}

LargeSpacer()

TextButton(
onClick = { viewModel.addFeedDialogValidate() },
) {
Text(text = "Validate")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ object FeedTab : Tab {

if (showDialog) {
AddFeedDialog(
onDismiss = { showDialog = false },
onValidate = {
viewModel = viewModel,
onDismiss = {
showDialog = false
viewModel.insertFeed(it)
}
viewModel.resetAddFeedDialogState()
},
)
}

Expand Down
Loading

0 comments on commit 672de76

Please sign in to comment.