From c42d92fcc83bbb213273e722d75905369cc4827e Mon Sep 17 00:00:00 2001 From: Ash Date: Sat, 2 Apr 2022 22:28:16 +0800 Subject: [PATCH] Optimize SubscribeDialog --- .../page/home/drawer/feed/FeedOptionDrawer.kt | 4 +- .../reader/ui/page/home/feeds/FeedsPage.kt | 6 +-- .../ui/page/home/feeds/FeedsViewModel.kt | 17 --------- .../home/feeds/subscribe/ResultViewPage.kt | 8 ++-- .../home/feeds/subscribe/SubscribeDialog.kt | 26 ++++--------- .../feeds/subscribe/SubscribeViewModel.kt | 20 ++++++++++ .../feeds/subscribe/SubscribeViewPager.kt | 38 +++++++------------ 7 files changed, 47 insertions(+), 72 deletions(-) diff --git a/app/src/main/java/me/ash/reader/ui/page/home/drawer/feed/FeedOptionDrawer.kt b/app/src/main/java/me/ash/reader/ui/page/home/drawer/feed/FeedOptionDrawer.kt index e922894..214bf61 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/drawer/feed/FeedOptionDrawer.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/drawer/feed/FeedOptionDrawer.kt @@ -1,6 +1,5 @@ package me.ash.reader.ui.page.home.drawer.feed -import android.util.Log import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.layout.* import androidx.compose.material.ExperimentalMaterialApi @@ -9,7 +8,6 @@ import androidx.compose.material.icons.rounded.DeleteOutline import androidx.compose.material.icons.rounded.RssFeed import androidx.compose.material3.* import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -82,7 +80,7 @@ fun FeedOptionDrawer( parseFullContentPresetOnClick = { viewModel.dispatch(FeedOptionViewAction.ChangeParseFullContentPreset) }, - groupOnClick = { + onGroupClick = { viewModel.dispatch(FeedOptionViewAction.SelectedGroup(it)) }, onKeyboardAction = { }, diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt index 05abbab..b6c6e79 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt @@ -133,11 +133,7 @@ fun FeedsPage( ) }, content = { - SubscribeDialog( - openInputStreamCallback = { - feedsViewModel.dispatch(FeedsViewAction.ImportFromInputStream(it)) - }, - ) + SubscribeDialog() LazyColumn { item { Text( diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsViewModel.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsViewModel.kt index 04183e3..c44f641 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsViewModel.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsViewModel.kt @@ -15,7 +15,6 @@ import me.ash.reader.data.repository.AccountRepository import me.ash.reader.data.repository.OpmlRepository import me.ash.reader.data.repository.RssRepository import me.ash.reader.ui.page.home.FilterState -import java.io.InputStream import javax.inject.Inject @HiltViewModel @@ -31,7 +30,6 @@ class FeedsViewModel @Inject constructor( when (action) { is FeedsViewAction.FetchAccount -> fetchAccount() is FeedsViewAction.FetchData -> fetchData(action.filterState) - is FeedsViewAction.ImportFromInputStream -> importFromInputStream(action.inputStream) is FeedsViewAction.ExportAsString -> exportAsOpml(action.callback) is FeedsViewAction.ScrollToItem -> scrollToItem(action.index) } @@ -47,17 +45,6 @@ class FeedsViewModel @Inject constructor( } } - private fun importFromInputStream(inputStream: InputStream) { - viewModelScope.launch(Dispatchers.IO) { - try { - opmlRepository.saveToDatabase(inputStream) - rssRepository.get().doSync() - } catch (e: Exception) { - Log.e("FeedsViewModel", "importFromInputStream: ", e) - } - } - } - private fun exportAsOpml(callback: (String) -> Unit = {}) { viewModelScope.launch(Dispatchers.Default) { try { @@ -157,10 +144,6 @@ sealed class FeedsViewAction { object FetchAccount : FeedsViewAction() - data class ImportFromInputStream( - val inputStream: InputStream - ) : FeedsViewAction() - data class ExportAsString( val callback: (String) -> Unit = {} ) : FeedsViewAction() diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/ResultViewPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/ResultViewPage.kt index c612d12..8b7367d 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/ResultViewPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/ResultViewPage.kt @@ -42,7 +42,7 @@ fun ResultViewPage( changeNewGroupSelected: (Boolean) -> Unit = {}, allowNotificationPresetOnClick: () -> Unit = {}, parseFullContentPresetOnClick: () -> Unit = {}, - groupOnClick: (groupId: String) -> Unit = {}, + onGroupClick: (groupId: String) -> Unit = {}, onKeyboardAction: () -> Unit = {}, ) { Column( @@ -68,7 +68,7 @@ fun ResultViewPage( newGroupSelected = newGroupSelected, onNewGroupValueChange = onNewGroupValueChange, changeNewGroupSelected = changeNewGroupSelected, - groupOnClick = groupOnClick, + onGroupClick = onGroupClick, onKeyboardAction = onKeyboardAction, ) Spacer(modifier = Modifier.height(6.dp)) @@ -158,7 +158,7 @@ private fun AddToGroup( newGroupSelected: Boolean, onNewGroupValueChange: (String) -> Unit = {}, changeNewGroupSelected: (Boolean) -> Unit = {}, - groupOnClick: (groupId: String) -> Unit = {}, + onGroupClick: (groupId: String) -> Unit = {}, onKeyboardAction: () -> Unit = {}, ) { Subtitle(text = stringResource(R.string.add_to_group)) @@ -175,7 +175,7 @@ private fun AddToGroup( selected = !newGroupSelected && it.id == selectedGroupId, ) { changeNewGroupSelected(false) - groupOnClick(it.id) + onGroupClick(it.id) } } diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt index c1c7eae..ea057be 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt @@ -21,31 +21,27 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.window.DialogProperties import androidx.hilt.navigation.compose.hiltViewModel import com.google.accompanist.pager.ExperimentalPagerApi -import kotlinx.coroutines.Dispatchers import me.ash.reader.* import me.ash.reader.R import me.ash.reader.ui.extension.collectAsStateValue import me.ash.reader.ui.widget.Dialog -import java.io.InputStream @OptIn(ExperimentalPagerApi::class, androidx.compose.ui.ExperimentalComposeUiApi::class) @Composable fun SubscribeDialog( modifier: Modifier = Modifier, subscribeViewModel: SubscribeViewModel = hiltViewModel(), - openInputStreamCallback: (InputStream) -> Unit, ) { val context = LocalContext.current val focusManager = LocalFocusManager.current val scope = rememberCoroutineScope() val viewState = subscribeViewModel.viewState.collectAsStateValue() - val groupsState = - viewState.groups.collectAsState(initial = emptyList(), context = Dispatchers.IO) + val groupsState = viewState.groups.collectAsState(initial = emptyList()) var dialogHeight by remember { mutableStateOf(300.dp) } val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { it?.let { uri -> context.contentResolver.openInputStream(uri)?.let { inputStream -> - openInputStreamCallback(inputStream) + subscribeViewModel.dispatch(SubscribeViewAction.ImportFromInputStream(inputStream)) } } } @@ -99,48 +95,40 @@ fun SubscribeDialog( }, text = { SubscribeViewPager( - readOnly = viewState.lockLinkInput, - inputLink = viewState.linkContent, - errorMessage = viewState.errorMessage, + viewState = viewState, onLinkValueChange = { subscribeViewModel.dispatch(SubscribeViewAction.InputLink(it)) }, onSearchKeyboardAction = { subscribeViewModel.dispatch(SubscribeViewAction.Search(scope)) }, - link = viewState.linkContent, groups = groupsState.value, - selectedAllowNotificationPreset = viewState.allowNotificationPreset, - selectedParseFullContentPreset = viewState.parseFullContentPreset, - selectedGroupId = viewState.selectedGroupId, - newGroupContent = viewState.newGroupContent, onNewGroupValueChange = { subscribeViewModel.dispatch(SubscribeViewAction.InputNewGroup(it)) }, - newGroupSelected = viewState.newGroupSelected, changeNewGroupSelected = { subscribeViewModel.dispatch(SubscribeViewAction.SelectedNewGroup(it)) }, - pagerState = viewState.pagerState, allowNotificationPresetOnClick = { subscribeViewModel.dispatch(SubscribeViewAction.ChangeAllowNotificationPreset) }, parseFullContentPresetOnClick = { subscribeViewModel.dispatch(SubscribeViewAction.ChangeParseFullContentPreset) }, - groupOnClick = { + onGroupClick = { subscribeViewModel.dispatch(SubscribeViewAction.SelectedGroup(it)) }, onResultKeyboardAction = { subscribeViewModel.dispatch(SubscribeViewAction.Subscribe) - } + }, ) }, confirmButton = { when (viewState.pagerState.currentPage) { 0 -> { TextButton( - enabled = viewState.linkContent.isNotEmpty(), + enabled = viewState.linkContent.isNotEmpty() + && viewState.title != stringResource(R.string.searching), onClick = { focusManager.clearFocus() subscribeViewModel.dispatch(SubscribeViewAction.Search(scope)) diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewModel.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewModel.kt index e8c79aa..6eb33de 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewModel.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewModel.kt @@ -1,5 +1,6 @@ package me.ash.reader.ui.page.home.feeds.subscribe +import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.accompanist.pager.ExperimentalPagerApi @@ -11,16 +12,19 @@ import me.ash.reader.R import me.ash.reader.data.article.Article import me.ash.reader.data.feed.Feed import me.ash.reader.data.group.Group +import me.ash.reader.data.repository.OpmlRepository import me.ash.reader.data.repository.RssHelper import me.ash.reader.data.repository.RssRepository import me.ash.reader.data.repository.StringsRepository import me.ash.reader.formatUrl import me.ash.reader.ui.extension.animateScrollToPage +import java.io.InputStream import javax.inject.Inject @OptIn(ExperimentalPagerApi::class) @HiltViewModel class SubscribeViewModel @Inject constructor( + private val opmlRepository: OpmlRepository, private val rssRepository: RssRepository, private val rssHelper: RssHelper, private val stringsRepository: StringsRepository, @@ -35,6 +39,7 @@ class SubscribeViewModel @Inject constructor( is SubscribeViewAction.Reset -> reset() is SubscribeViewAction.Show -> changeVisible(true) is SubscribeViewAction.Hide -> changeVisible(false) + is SubscribeViewAction.ImportFromInputStream -> importFromInputStream(action.inputStream) is SubscribeViewAction.InputLink -> inputLink(action.content) is SubscribeViewAction.Search -> search(action.scope) is SubscribeViewAction.ChangeAllowNotificationPreset -> @@ -67,6 +72,17 @@ class SubscribeViewModel @Inject constructor( } } + private fun importFromInputStream(inputStream: InputStream) { + viewModelScope.launch(Dispatchers.IO) { + try { + opmlRepository.saveToDatabase(inputStream) + rssRepository.get().doSync() + } catch (e: Exception) { + Log.e("FeedsViewModel", "importFromInputStream: ", e) + } + } + } + private fun subscribe() { val feed = _viewState.value.feed ?: return val articles = _viewState.value.articles @@ -232,6 +248,10 @@ sealed class SubscribeViewAction { object Show : SubscribeViewAction() object Hide : SubscribeViewAction() + data class ImportFromInputStream( + val inputStream: InputStream + ) : SubscribeViewAction() + data class InputLink( val content: String ) : SubscribeViewAction() diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewPager.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewPager.kt index 99195de..d4562c6 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewPager.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeViewPager.kt @@ -6,32 +6,22 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalFocusManager import com.google.accompanist.pager.ExperimentalPagerApi -import com.google.accompanist.pager.PagerState import me.ash.reader.data.group.Group import me.ash.reader.ui.widget.ViewPager @OptIn(ExperimentalPagerApi::class) @Composable fun SubscribeViewPager( + viewState: SubscribeViewState, modifier: Modifier = Modifier, - readOnly: Boolean = false, - inputLink: String = "", - errorMessage: String = "", onLinkValueChange: (String) -> Unit = {}, onSearchKeyboardAction: () -> Unit = {}, - link: String = "", groups: List = emptyList(), - selectedAllowNotificationPreset: Boolean = false, - selectedParseFullContentPreset: Boolean = false, - selectedGroupId: String = "", - newGroupContent: String = "", onNewGroupValueChange: (String) -> Unit = {}, - newGroupSelected: Boolean, changeNewGroupSelected: (Boolean) -> Unit = {}, - pagerState: PagerState = com.google.accompanist.pager.rememberPagerState(), allowNotificationPresetOnClick: () -> Unit = {}, parseFullContentPresetOnClick: () -> Unit = {}, - groupOnClick: (groupId: String) -> Unit = {}, + onGroupClick: (groupId: String) -> Unit = {}, onResultKeyboardAction: () -> Unit = {}, ) { val focusManager = LocalFocusManager.current @@ -44,33 +34,33 @@ fun SubscribeViewPager( } ) }, - state = pagerState, + state = viewState.pagerState, userScrollEnabled = false, composableList = listOf( { SearchViewPage( - pagerState = pagerState, - readOnly = readOnly, - inputLink = inputLink, - errorMessage = errorMessage, + pagerState = viewState.pagerState, + readOnly = viewState.lockLinkInput, + inputLink = viewState.linkContent, + errorMessage = viewState.errorMessage, onLinkValueChange = onLinkValueChange, onKeyboardAction = onSearchKeyboardAction, ) }, { ResultViewPage( - link = link, + link = viewState.linkContent, groups = groups, - selectedAllowNotificationPreset = selectedAllowNotificationPreset, - selectedParseFullContentPreset = selectedParseFullContentPreset, - selectedGroupId = selectedGroupId, - newGroupContent = newGroupContent, + selectedAllowNotificationPreset = viewState.allowNotificationPreset, + selectedParseFullContentPreset = viewState.parseFullContentPreset, + selectedGroupId = viewState.selectedGroupId, + newGroupContent = viewState.newGroupContent, onNewGroupValueChange = onNewGroupValueChange, - newGroupSelected = newGroupSelected, + newGroupSelected = viewState.newGroupSelected, changeNewGroupSelected = changeNewGroupSelected, allowNotificationPresetOnClick = allowNotificationPresetOnClick, parseFullContentPresetOnClick = parseFullContentPresetOnClick, - groupOnClick = groupOnClick, + onGroupClick = onGroupClick, onKeyboardAction = onResultKeyboardAction, ) }