Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ data class PostItem(
val quantity: Int? = null,
) {
fun getHost(): String {
return url.split("/")[2]
return url.split("/").getOrNull(2) ?: ""
}

fun getSiteConfig(hostsMap: Map<String, SiteConfig>): SiteConfig? {
Expand Down
24 changes: 11 additions & 13 deletions app/src/main/java/com/paulcoding/hviewer/ui/page/AppEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.animation.fadeOut
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
Expand Down Expand Up @@ -48,7 +49,7 @@ fun AppEntry(intent: Intent?) {
val navController = rememberNavController()

val githubState by Github.stateFlow.collectAsState()
val siteConfigs = githubState.siteConfigs ?: SiteConfigs()
val siteConfigs by remember { derivedStateOf { githubState.siteConfigs ?: SiteConfigs() } }
val appViewModel: AppViewModel = viewModel()
val appState by appViewModel.stateFlow.collectAsState()
val context = LocalContext.current
Expand All @@ -59,7 +60,8 @@ fun AppEntry(intent: Intent?) {
navController.navigate(Route.POST)
}

fun navToCustomTag(tag: Tag) {
fun navToCustomTag(post: PostItem, tag: Tag) {
appViewModel.setCurrentPost(post)
appViewModel.setCurrentTag(tag)
navController.navigate(Route.CUSTOM_TAG)
}
Expand Down Expand Up @@ -106,8 +108,8 @@ fun AppEntry(intent: Intent?) {
isDevMode = appState.isDevMode,
siteConfigs = siteConfigs,
refresh = { Github.refreshLocalConfigs() },
navToTopics = { site ->
appViewModel.setSiteConfig(site, siteConfigs.sites[site]!!)
navToTopics = { siteConfig ->
appViewModel.setCurrentPost(PostItem(siteConfig.baseUrl))
navController.navigate(Route.POSTS)
}, navToSettings = {
navController.navigate(Route.SETTINGS)
Expand Down Expand Up @@ -143,15 +145,15 @@ fun AppEntry(intent: Intent?) {
navToImages(post)
},
navToSearch = { navController.navigate(Route.SEARCH) },
navToCustomTag = { navToCustomTag(it) },
navToCustomTag = { postItem, tag -> navToCustomTag(postItem, tag) },
navToTabs = { navController.navigate(Route.TABS) },
goBack = { navController.popBackStack() },
)
}
animatedComposable(Route.CUSTOM_TAG) {
CustomTagPage(
appViewModel,
navToCustomTag = { navToCustomTag(it) },
navToCustomTag = { postItem, tag -> navToCustomTag(postItem, tag) },
goBack = { navController.popBackStack() }
) {
navToImages(it)
Expand All @@ -175,20 +177,18 @@ fun AppEntry(intent: Intent?) {
navToImages = { post: PostItem ->
navToImages(post)
},
navToCustomTag = { navToCustomTag(it) },
navToCustomTag = { postItem, tag -> navToCustomTag(postItem, tag) },
goBack = { navController.popBackStack() },
)
}
animatedComposable(Route.FAVORITE) {
FavoritePage(
appViewModel = appViewModel,
navToImages = { post: PostItem ->
appViewModel.setSiteConfig(post.site, siteConfigs.sites[post.site]!!)
navToImages(post)
},
navToCustomTag = { post, tag ->
appViewModel.setSiteConfig(post.site, siteConfigs.sites[post.site]!!)
navToCustomTag(tag)
navToCustomTag(post, tag)
},
goBack = { navController.popBackStack() }
)
Expand Down Expand Up @@ -228,12 +228,10 @@ fun AppEntry(intent: Intent?) {
HistoryPage(
goBack = { navController.popBackStack() }, appViewModel = appViewModel,
navToImages = { post: PostItem ->
appViewModel.setSiteConfig(post.site, siteConfigs.sites[post.site]!!)
navToImages(post)
},
navToCustomTag = { post, tag ->
appViewModel.setSiteConfig(post.site, siteConfigs.sites[post.site]!!)
navToCustomTag(tag)
navToCustomTag(post, tag)
},
deleteHistory = appViewModel::deleteHistory
)
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/com/paulcoding/hviewer/ui/page/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.paulcoding.hviewer.model.PostHistory
import com.paulcoding.hviewer.model.PostItem
import com.paulcoding.hviewer.model.SiteConfig
import com.paulcoding.hviewer.model.Tag
import com.paulcoding.hviewer.network.Github
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -44,15 +45,9 @@ class AppViewModel : ViewModel() {

fun getCurrentTag() = _stateFlow.value.tag

fun setSiteConfig(site: String, siteConfig: SiteConfig) {
_stateFlow.update { it.copy(site = site to siteConfig) }
}


data class UiState(
val post: PostItem = PostItem(),
val url: String = "",
val site: Pair<String, SiteConfig> = "" to SiteConfig(),
val tag: Tag = Tag(),
val isDevMode: Boolean = BuildConfig.DEBUG,
)
Expand All @@ -70,7 +65,6 @@ class AppViewModel : ViewModel() {
fun addFavorite(postItem: PostItem, reAdded: Boolean = false) {
viewModelScope.launch {
val item = if (reAdded) postItem else postItem.copy(
site = _stateFlow.value.site.first,
createdAt = System.currentTimeMillis()
)
DatabaseProvider.getInstance().favoritePostDao().insert(item)
Expand All @@ -85,7 +79,6 @@ class AppViewModel : ViewModel() {

fun addHistory(postItem: PostItem) {
val item = PostHistory(
site = _stateFlow.value.site.first,
createdAt = System.currentTimeMillis(),
url = postItem.url,
views = postItem.views,
Expand Down Expand Up @@ -129,4 +122,12 @@ class AppViewModel : ViewModel() {
emptyList()
}
}

fun getCurrentSiteConfig(): SiteConfig {
val hostMap = Github.stateFlow.value.siteConfigs?.toHostsMap()
if (hostMap != null) {
return _stateFlow.value.post.getSiteConfig(hostMap) ?: SiteConfig()
}
return SiteConfig()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.paulcoding.hviewer.ui.page.AppViewModel
fun CustomTagPage(
appViewModel: AppViewModel,
goBack: () -> Unit,
navToCustomTag: (Tag) -> Unit,
navToCustomTag: (PostItem, Tag) -> Unit,
navToImages: (PostItem) -> Unit
) {
val appState by appViewModel.stateFlow.collectAsState()
Expand All @@ -49,11 +49,10 @@ fun CustomTagPage(

PageContent(
appViewModel,
siteConfig = appState.site.second,
tag = tag,
navToCustomTag = {
if (it.name != tag.name)
navToCustomTag(it)
navToCustomTag = { post, newTag ->
if (newTag.name != tag.name)
navToCustomTag(post, tag)
},
onPageChange = { currentPage, total ->
pageProgress = currentPage to total
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import com.paulcoding.hviewer.extensions.isScrolledToEnd
import com.paulcoding.hviewer.extensions.toCapital
import com.paulcoding.hviewer.helper.makeToast
import com.paulcoding.hviewer.model.PostItem
import com.paulcoding.hviewer.model.SiteConfig
import com.paulcoding.hviewer.model.Tag
import com.paulcoding.hviewer.ui.component.HBackIcon
import com.paulcoding.hviewer.ui.component.HEmpty
Expand All @@ -56,14 +55,14 @@ fun PostsPage(
appViewModel: AppViewModel,
navToImages: (PostItem) -> Unit,
navToSearch: () -> Unit,
navToCustomTag: (Tag) -> Unit,
navToCustomTag: (PostItem, Tag) -> Unit,
navToTabs: () -> Unit,
goBack: () -> Unit
) {
val appState by appViewModel.stateFlow.collectAsState()
val tabs by appViewModel.tabs.collectAsState(initial = listOf())

val siteConfig = appState.site.second
val siteConfig = appViewModel.getCurrentSiteConfig()

val listTag: List<Tag> = siteConfig.tags.keys.map { key ->
Tag(name = key, url = siteConfig.tags[key]!!)
Expand All @@ -81,7 +80,7 @@ fun PostsPage(
HPageProgress(pageProgress.first, pageProgress.second)
HIcon(imageVector = Icons.Outlined.Search) { navToSearch() }
if (tabs.isNotEmpty()) {
HIcon(imageVector = Icons.Outlined.Tab) { navToTabs() }
HIcon(imageVector = Icons.Outlined.Tab) { navToTabs() }
}
})
}) { paddings ->
Expand Down Expand Up @@ -114,7 +113,6 @@ fun PostsPage(

PageContent(
appViewModel,
siteConfig,
tag = tag,
navToCustomTag = navToCustomTag,
onPageChange = { currentPage, total ->
Expand All @@ -130,15 +128,14 @@ fun PostsPage(
@Composable
fun PageContent(
appViewModel: AppViewModel,
siteConfig: SiteConfig,
tag: Tag,
onPageChange: (Int, Int) -> Unit,
navToCustomTag: (Tag) -> Unit = {},
navToCustomTag: (PostItem, Tag) -> Unit,
onClick: (PostItem) -> Unit
) {
val listFavorite by appViewModel.favoritePosts.collectAsState(initial = emptyList())
val viewModel: PostsViewModel = viewModel(
factory = PostsViewModelFactory(siteConfig, tag),
factory = PostsViewModelFactory(appViewModel.getCurrentSiteConfig(), tag),
key = tag.name
)
val listState = rememberLazyListState()
Expand Down Expand Up @@ -172,8 +169,8 @@ fun PageContent(
FavoriteCard(
post,
isFavorite = listFavorite.find { it.url == post.url } != null,
onTagClick = {
navToCustomTag(it)
onTagClick = { tag ->
navToCustomTag(post, tag)
},
onAddToTabs = {
appViewModel.addTab(post)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,11 @@ import com.paulcoding.hviewer.ui.page.posts.FavoriteCard
fun SearchPage(
appViewModel: AppViewModel,
navToImages: (post: PostItem) -> Unit,
navToCustomTag: (tag: Tag) -> Unit,
navToCustomTag: (PostItem, Tag) -> Unit,
goBack: () -> Unit,
) {
val appState by appViewModel.stateFlow.collectAsState()
val siteConfig = appState.site.second

val viewModel: SearchViewModel = viewModel(
factory = SearchViewModelFactory(siteConfig),
factory = SearchViewModelFactory(appViewModel.getCurrentSiteConfig()),
)
val uiState by viewModel.stateFlow.collectAsState()
var query by remember { mutableStateOf(viewModel.stateFlow.value.query) }
Expand Down Expand Up @@ -128,12 +125,12 @@ fun SearchPage(
}
}


// TODO: duplicated PageContent
@Composable
fun PageContent(
appViewModel: AppViewModel,
viewModel: SearchViewModel,
navToCustomTag: (Tag) -> Unit,
navToCustomTag: (PostItem, Tag) -> Unit,
onClick: (PostItem) -> Unit
) {
val listState = rememberLazyListState()
Expand Down Expand Up @@ -164,7 +161,7 @@ fun PageContent(
appViewModel.deleteFavorite(post)
},
onTagClick = {
navToCustomTag(it)
navToCustomTag(post, it)
}
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.BugReport
import androidx.compose.material.icons.outlined.Edit
import androidx.compose.material.icons.outlined.History
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -44,7 +42,7 @@ import kotlinx.coroutines.launch
@Composable
fun SitesPage(
isDevMode: Boolean,
navToTopics: (site: String) -> Unit,
navToTopics: (siteConfig: SiteConfig) -> Unit,
goBack: () -> Unit,
siteConfigs: SiteConfigs,
navToSettings: () -> Unit,
Expand Down Expand Up @@ -102,7 +100,7 @@ fun SitesPage(
key = site,
site = siteConfig
) {
navToTopics(site)
navToTopics(siteConfig)
}
}
}
Expand Down