Skip to content

Commit

Permalink
inner app navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
yueeng committed Dec 23, 2020
1 parent 69a129c commit a69614a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/io/github/yueeng/hacg/Article.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package io.github.yueeng.hacg
import android.app.Activity
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.Parcelable
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -120,11 +121,10 @@ object HAcg {
config.version <= defaultConfig()?.version ?: 0 -> if (tip) context.toast(R.string.settings_config_newest)
else -> context.snack(context.getString(R.string.settings_config_updating), Snackbar.LENGTH_LONG)
.setAction(R.string.settings_config_update) {
try {
runCatching {
host = defaultHosts(config).first()
configFile.writeText(html!!.first)
updated()
} catch (_: Exception) {
}
}.show()
}
Expand Down Expand Up @@ -308,12 +308,12 @@ data class Article(val id: Int, val title: String,
companion object {
val ID: Regex = """post-(\d+)""".toRegex()
fun parseID(str: String?) = str?.let { s -> ID.find(s)?.let { it.groups[1]?.value?.toInt() } }
val URL: Regex get() = """${HAcg.wordpress}/(\d+)\.html""".toRegex()
private val URL: Regex = """/wp/(\d+)\.html""".toRegex()
fun getIdFromUrl(str: String?) = str?.let { s -> URL.find(s)?.let { it.groups[1]?.value?.toInt() } }
fun isList(url: String) = HAcg.categories.map { "${HAcg.web}$it" }.contains(url)
|| url.startsWith("${HAcg.wordpress}/tag/")
|| url.startsWith("${HAcg.wordpress}/author/")
|| url.startsWith("${HAcg.wordpress}/?s=")
private val LIST = listOf("/wp/tag/", "/wp/author/", "/wp/?s=")
fun isList(url: String): Boolean = Uri.parse(url).path?.let { path ->
HAcg.categories.any { path == it.first } || LIST.any { path.startsWith(it) }
} ?: false
}

constructor(msg: String) : this(0, msg, null, null, null, null, 0, null, null, listOf())
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/io/github/yueeng/hacg/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,22 @@ fun String.toDate(fmt: SimpleDateFormat? = null): Date? = try {
}

val String.html: Spanned get() = HtmlCompat.fromHtml(this, HtmlCompat.FROM_HTML_MODE_COMPACT)
fun String.isSameHost(base: String?) = try {
if (base != null) Uri.parse(this).host == Uri.parse(base).host else false
} catch (_: Exception) {
false
}

val String.isWordpress
get() = try {
Uri.parse(this).path?.startsWith("/wp/") ?: false
} catch (_: Exception) {
false
}

fun Context.openUri(url: String?, web: Boolean = true): Boolean = when {
url.isNullOrEmpty() -> null
!url.startsWith(HAcg.wordpress) -> Uri.parse(url).let { uri ->
!url.isWordpress -> Uri.parse(url).let { uri ->
startActivity(Intent.createChooser(Intent(Intent.ACTION_VIEW, uri).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), uri.scheme))
}
Article.getIdFromUrl(url) != null -> startActivity(Intent(this, InfoActivity::class.java).putExtra("url", url))
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/io/github/yueeng/hacg/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.withContext
import okhttp3.Request
import org.jsoup.Jsoup
import java.text.SimpleDateFormat
import java.util.*

Expand Down Expand Up @@ -224,11 +222,11 @@ class SearchHistoryProvider : SearchRecentSuggestionsProvider() {
}
}

class ArticlePagingSource : PagingSource<String, Article>() {
class ArticlePagingSource(private val title: (String) -> Unit) : PagingSource<String, Article>() {
override suspend fun load(params: LoadParams<String>): LoadResult<String, Article> = try {
val uri = params.key!!
val html = okhttp.newCall(Request.Builder().url(uri).build()).await { _, response -> response.body?.string() }
val dom = Jsoup.parse(html, uri)
val dom = params.key!!.httpGetAwait()!!.jsoup()
listOf("h1.page-title>span", "h1#site-title", "title").asSequence().map { dom.select(it).text() }
.firstOrNull { it.isNotEmpty() }?.let(title::invoke)
val articles = dom.select("article").map { o -> Article(o) }.toList()
val next = (dom.select("#wp_page_numbers a").lastOrNull()
?.takeIf { ">" == it.text() }?.attr("abs:href")
Expand All @@ -243,7 +241,8 @@ class ArticleViewModel(private val handle: SavedStateHandle, args: Bundle?) : Vi
var retry: Boolean
get() = handle.get("retry") ?: false
set(value) = handle.set("retry", value)
val source = Paging(handle, args?.getString("url")) { ArticlePagingSource() }
val title = handle.getLiveData<String>("title")
val source = Paging(handle, args?.getString("url")) { ArticlePagingSource { title.postValue(it) } }
val data = handle.getLiveData<List<Article>>("data")
val last = handle.getLiveData("last", -1)
}
Expand Down Expand Up @@ -289,6 +288,12 @@ class ArticleFragment : Fragment() {
image1.visibility = if (it is LoadState.Error && adapter.itemCount == 0) View.VISIBLE else View.INVISIBLE
if (it is LoadState.Error && adapter.itemCount == 0) if (viewModel.retry) activity?.openOptionsMenu() else activity?.toast(R.string.app_network_retry)
})
if (requireActivity().title.isNullOrEmpty()) {
requireActivity().title = getString(R.string.app_name)
viewModel.title.observe(viewLifecycleOwner, {
requireActivity().title = it
})
}
image1.setOnClickListener {
viewModel.retry = true
query(true)
Expand Down

0 comments on commit a69614a

Please sign in to comment.