Skip to content

Commit

Permalink
feat(freshrss): support fresh rss api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashinch committed Jan 18, 2024
1 parent 1d4883f commit 814aeac
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.ash.reader.domain.model.account.security

class FreshRSSSecurityKey private constructor() : SecurityKey() {

var serverUrl: String? = null
var username: String? = null
var password: String? = null

constructor(serverUrl: String?, username: String?, password: String?) : this() {
this.serverUrl = serverUrl
this.username = username
this.password = password
}

constructor(value: String? = DESUtils.empty) : this() {
decode(value, FreshRSSSecurityKey::class.java).let {
serverUrl = it.serverUrl
username = it.username
password = it.password
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class GoogleReaderRssService @Inject constructor(
accountDao.update(account.copy(name = it))
}
} catch (ignore: Exception) {
Log.e("RLog", "get user info is failed: ", ignore)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RssService @Inject constructor(
AccountType.Local.id -> localRssService
AccountType.Fever.id -> feverRssService
AccountType.GoogleReader.id -> googleReaderRssService
AccountType.FreshRSS.id -> googleReaderRssService
AccountType.Inoreader.id -> localRssService
AccountType.Feedly.id -> localRssService
else -> localRssService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import me.ash.reader.ui.component.base.FeedbackIconButton
import me.ash.reader.ui.component.base.RYScaffold
import me.ash.reader.ui.component.base.Subtitle
import me.ash.reader.ui.page.settings.SettingItem
import me.ash.reader.ui.page.settings.accounts.addition.AddFeverAccountDialog
import me.ash.reader.ui.page.settings.accounts.addition.AddGoogleReaderAccountDialog
import me.ash.reader.ui.page.settings.accounts.addition.AddLocalAccountDialog
import me.ash.reader.ui.page.settings.accounts.addition.AdditionViewModel
import me.ash.reader.ui.page.settings.accounts.addition.*
import me.ash.reader.ui.theme.palette.onLight

@OptIn(ExperimentalAnimationApi::class)
Expand Down Expand Up @@ -101,7 +98,7 @@ fun AddAccountsPage(
desc = stringResource(R.string.fresh_rss_desc),
iconPainter = painterResource(id = R.drawable.ic_freshrss),
onClick = {
additionViewModel.showAddGoogleReaderAccountDialog()
additionViewModel.showAddFreshRSSAccountDialog()
},
) {}
SettingItem(
Expand Down Expand Up @@ -133,6 +130,7 @@ fun AddAccountsPage(
AddLocalAccountDialog(navController)
AddFeverAccountDialog(navController)
AddGoogleReaderAccountDialog(navController)
AddFreshRSSAccountDialog(navController)
}

@Preview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package me.ash.reader.ui.page.settings.accounts.addition

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
Expand Down Expand Up @@ -62,6 +59,7 @@ fun AddFeverAccountDialog(
},
icon = {
Icon(
modifier = Modifier.size(24.dp),
painter = painterResource(id = R.drawable.ic_fever),
contentDescription = stringResource(R.string.fever),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package me.ash.reader.ui.page.settings.accounts.addition

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import me.ash.reader.R
import me.ash.reader.domain.model.account.Account
import me.ash.reader.domain.model.account.AccountType
import me.ash.reader.domain.model.account.security.FreshRSSSecurityKey
import me.ash.reader.ui.component.base.RYDialog
import me.ash.reader.ui.component.base.RYOutlineTextField
import me.ash.reader.ui.ext.collectAsStateValue
import me.ash.reader.ui.ext.showToast
import me.ash.reader.ui.page.common.RouteName
import me.ash.reader.ui.page.settings.accounts.AccountViewModel

@OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class)
@Composable
fun AddFreshRSSAccountDialog(
navController: NavHostController,
viewModel: AdditionViewModel = hiltViewModel(),
accountViewModel: AccountViewModel = hiltViewModel(),
) {
val context = LocalContext.current
val focusManager = LocalFocusManager.current
val uiState = viewModel.additionUiState.collectAsStateValue()

var freshRSSServerUrl by rememberSaveable { mutableStateOf("") }
var freshRSSUsername by rememberSaveable { mutableStateOf("") }
var freshRSSPassword by rememberSaveable { mutableStateOf("") }

RYDialog(
modifier = Modifier.padding(horizontal = 44.dp),
visible = uiState.addFreshRSSAccountDialogVisible,
properties = DialogProperties(usePlatformDefaultWidth = false),
onDismissRequest = {
focusManager.clearFocus()
viewModel.hideAddFreshRSSAccountDialog()
},
icon = {
Icon(
modifier = Modifier.size(24.dp),
painter = painterResource(id = R.drawable.ic_freshrss),
contentDescription = stringResource(R.string.fresh_rss),
)
},
title = {
Text(
text = stringResource(R.string.fresh_rss),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
},
text = {
Column(
modifier = Modifier.verticalScroll(rememberScrollState())
) {
Spacer(modifier = Modifier.height(10.dp))
RYOutlineTextField(
value = freshRSSServerUrl,
onValueChange = { freshRSSServerUrl = it },
label = stringResource(R.string.server_url),
placeholder = "https://demo.freshrss.org/api/greader.php",
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
)
Spacer(modifier = Modifier.height(10.dp))
RYOutlineTextField(
value = freshRSSUsername,
onValueChange = { freshRSSUsername = it },
label = stringResource(R.string.username),
placeholder = "demo",
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
)
Spacer(modifier = Modifier.height(10.dp))
RYOutlineTextField(
value = freshRSSPassword,
onValueChange = { freshRSSPassword = it },
isPassword = true,
label = stringResource(R.string.password),
placeholder = "demodemo",
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
)
Spacer(modifier = Modifier.height(10.dp))
}
},
confirmButton = {
TextButton(
enabled = freshRSSServerUrl.isNotBlank() && freshRSSUsername.isNotEmpty() && freshRSSPassword.isNotEmpty(),
onClick = {
focusManager.clearFocus()
if (!freshRSSServerUrl.endsWith("/")) {
freshRSSServerUrl += "/"
}
accountViewModel.addAccount(Account(
type = AccountType.FreshRSS,
name = context.getString(R.string.fresh_rss),
securityKey = FreshRSSSecurityKey(
serverUrl = freshRSSServerUrl,
username = freshRSSUsername,
password = freshRSSPassword,
).toString(),
)) { account, exception ->
if (account == null) {
context.showToast(exception?.message ?: "Not valid credentials")
} else {
viewModel.hideAddFreshRSSAccountDialog()
navController.popBackStack()
navController.navigate("${RouteName.ACCOUNT_DETAILS}/${account.id}") {
launchSingleTop = true
}
}
}
}
) {
Text(stringResource(R.string.add))
}
},
dismissButton = {
TextButton(
onClick = {
focusManager.clearFocus()
viewModel.hideAddFreshRSSAccountDialog()
}
) {
Text(stringResource(R.string.cancel))
}
},
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package me.ash.reader.ui.page.settings.accounts.addition

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
Expand Down Expand Up @@ -63,6 +60,7 @@ fun AddGoogleReaderAccountDialog(
},
icon = {
Icon(
modifier = Modifier.size(24.dp),
imageVector = Icons.Rounded.RssFeed,
contentDescription = stringResource(R.string.google_reader),
)
Expand Down Expand Up @@ -111,6 +109,9 @@ fun AddGoogleReaderAccountDialog(
enabled = googleReaderServerUrl.isNotBlank() && googleReaderUsername.isNotEmpty() && googleReaderPassword.isNotEmpty(),
onClick = {
focusManager.clearFocus()
if (!googleReaderServerUrl.endsWith("/")) {
googleReaderServerUrl += "/"
}
accountViewModel.addAccount(Account(
type = AccountType.GoogleReader,
name = context.getString(R.string.google_reader),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package me.ash.reader.ui.page.settings.accounts.addition

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
Expand Down Expand Up @@ -56,6 +53,7 @@ fun AddLocalAccountDialog(
},
icon = {
Icon(
modifier = Modifier.size(24.dp),
imageVector = Icons.Rounded.RssFeed,
contentDescription = stringResource(R.string.local),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,27 @@ class AdditionViewModel @Inject constructor(
)
}
}

fun showAddFreshRSSAccountDialog() {
_additionUiState.update {
it.copy(
addFreshRSSAccountDialogVisible = true,
)
}
}

fun hideAddFreshRSSAccountDialog() {
_additionUiState.update {
it.copy(
addFreshRSSAccountDialogVisible = false,
)
}
}
}

data class AdditionUiState(
val addLocalAccountDialogVisible: Boolean = false,
val addFeverAccountDialogVisible: Boolean = false,
val addGoogleReaderAccountDialogVisible: Boolean = false,
val addFreshRSSAccountDialogVisible: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fun LazyItemScope.AccountConnection(
}
when (account.type.id) {
AccountType.Fever.id -> FeverConnection(account)
AccountType.GoogleReader.id -> {}
AccountType.FreshRSS.id -> {}
AccountType.GoogleReader.id -> GoogleReaderConnection(account)
AccountType.FreshRSS.id -> FreshRSSConnection(account)
AccountType.Feedly.id -> {}
AccountType.Inoreader.id -> {}
}
Expand Down
Loading

0 comments on commit 814aeac

Please sign in to comment.