mirror of https://git.sr.ht/~oppen/ariane
improve history - a lot
This commit is contained in:
parent
5bae07bef3
commit
2c0e1018ac
|
@ -3,7 +3,7 @@ package oppen.ariane.io.gemini
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
|
|
||||||
class AddressBuilder{
|
class AddressBuilder {
|
||||||
|
|
||||||
private var uri: Uri = Uri.EMPTY
|
private var uri: Uri = Uri.EMPTY
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@ import java.net.URI
|
||||||
interface Datasource {
|
interface Datasource {
|
||||||
fun request(uri: URI, forceDownload: Boolean, onUpdate: (state: GemState) -> Unit)
|
fun request(uri: URI, forceDownload: Boolean, onUpdate: (state: GemState) -> Unit)
|
||||||
fun request(uri: URI, onUpdate: (state: GemState) -> Unit)
|
fun request(uri: URI, onUpdate: (state: GemState) -> Unit)
|
||||||
|
fun canGoBack(): Boolean
|
||||||
|
fun goBack(onUpdate: (state: GemState) -> Unit)
|
||||||
|
|
||||||
companion object{
|
companion object{
|
||||||
fun factory(context: Context): Datasource {
|
fun factory(context: Context): Datasource {
|
||||||
|
|
|
@ -10,20 +10,18 @@ import oppen.isGemini
|
||||||
import oppen.toURI
|
import oppen.toURI
|
||||||
import oppen.toUri
|
import oppen.toUri
|
||||||
import java.io.*
|
import java.io.*
|
||||||
|
import java.lang.IllegalStateException
|
||||||
import java.net.ConnectException
|
import java.net.ConnectException
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import javax.net.ssl.*
|
import javax.net.ssl.*
|
||||||
|
|
||||||
const val GEMINI_SCHEME = "gemini"
|
const val GEMINI_SCHEME = "gemini"
|
||||||
|
|
||||||
class GeminiDatasource(
|
class GeminiDatasource(private val context: Context): Datasource {
|
||||||
private val context: Context): Datasource {
|
|
||||||
|
|
||||||
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
private var last: URI? = null
|
|
||||||
|
|
||||||
private val addressBuilder = AddressBuilder()
|
private val addressBuilder = AddressBuilder()
|
||||||
|
private val runtimeHistory = mutableListOf<URI>()
|
||||||
private var forceDownload = false
|
private var forceDownload = false
|
||||||
|
|
||||||
override fun request(uri: URI, onUpdate: (state: GemState) -> Unit) = request(uri, false, onUpdate)
|
override fun request(uri: URI, onUpdate: (state: GemState) -> Unit) = request(uri, false, onUpdate)
|
||||||
|
@ -42,7 +40,6 @@ class GeminiDatasource(
|
||||||
val cached = RuntimeCache.get(uri)
|
val cached = RuntimeCache.get(uri)
|
||||||
when {
|
when {
|
||||||
cached != null -> {
|
cached != null -> {
|
||||||
last = uri
|
|
||||||
onUpdate(GemState.ResponseGemtext(uri, cached.first, cached.second))
|
onUpdate(GemState.ResponseGemtext(uri, cached.first, cached.second))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -59,11 +56,9 @@ class GeminiDatasource(
|
||||||
val parsedUri = addressBuilder.request(uri).uri()
|
val parsedUri = addressBuilder.request(uri).uri()
|
||||||
if(parsedUri.isGemini()){
|
if(parsedUri.isGemini()){
|
||||||
val cached = RuntimeCache.get(parsedUri)
|
val cached = RuntimeCache.get(parsedUri)
|
||||||
if(cached != null){
|
when {
|
||||||
last = parsedUri.toURI()
|
cached != null -> onUpdate(GemState.ResponseGemtext(parsedUri.toURI(), cached.first, cached.second))
|
||||||
onUpdate(GemState.ResponseGemtext(parsedUri.toURI(), cached.first, cached.second))
|
else -> request(parsedUri.toURI(), forceDownload, onUpdate)
|
||||||
}else{
|
|
||||||
request(parsedUri.toURI(), forceDownload, onUpdate)
|
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
onUpdate(GemState.NotGeminiRequest(uri))
|
onUpdate(GemState.NotGeminiRequest(uri))
|
||||||
|
@ -81,7 +76,6 @@ class GeminiDatasource(
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private fun geminiRequest(uri: URI, onUpdate: (state: GemState) -> Unit){
|
private fun geminiRequest(uri: URI, onUpdate: (state: GemState) -> Unit){
|
||||||
last = uri
|
|
||||||
val port = if(uri.port == -1) 1965 else uri.port
|
val port = if(uri.port == -1) 1965 else uri.port
|
||||||
|
|
||||||
val protocol = prefs.getString("tls_protocol", "TLS")
|
val protocol = prefs.getString("tls_protocol", "TLS")
|
||||||
|
@ -184,9 +178,13 @@ class GeminiDatasource(
|
||||||
val processed = GemtextHelper.findCodeBlocks(lines)
|
val processed = GemtextHelper.findCodeBlocks(lines)
|
||||||
|
|
||||||
when {
|
when {
|
||||||
uri.toString().startsWith("gemini://") -> RuntimeCache.put(uri, header, processed)
|
!uri.toString().startsWith("gemini://") -> throw IllegalStateException("Not a Gemini Uri")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RuntimeCache.put(uri, header, processed)
|
||||||
|
|
||||||
|
if(runtimeHistory.isEmpty() || runtimeHistory.last().toString() != uri.toString()) runtimeHistory.add(uri)
|
||||||
|
|
||||||
onUpdate(GemState.ResponseGemtext(uri, header, processed))
|
onUpdate(GemState.ResponseGemtext(uri, header, processed))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,4 +234,11 @@ class GeminiDatasource(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun canGoBack(): Boolean = runtimeHistory.size > 1
|
||||||
|
|
||||||
|
override fun goBack(onUpdate: (state: GemState) -> Unit) {
|
||||||
|
runtimeHistory.removeLast()
|
||||||
|
request(runtimeHistory.last(), onUpdate)
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -14,8 +14,6 @@ class GemViewModel: ViewModel() {
|
||||||
private lateinit var bookmarks: BookmarksDatasource
|
private lateinit var bookmarks: BookmarksDatasource
|
||||||
private var onState: (state: GemState) -> Unit = {}
|
private var onState: (state: GemState) -> Unit = {}
|
||||||
|
|
||||||
private val history = mutableListOf<URI>()
|
|
||||||
|
|
||||||
fun initialise(home: String, gemini: Datasource, bookmarks: BookmarksDatasource, onState: (state: GemState) -> Unit){
|
fun initialise(home: String, gemini: Datasource, bookmarks: BookmarksDatasource, onState: (state: GemState) -> Unit){
|
||||||
this.gemini = gemini
|
this.gemini = gemini
|
||||||
this.bookmarks = bookmarks
|
this.bookmarks = bookmarks
|
||||||
|
@ -30,15 +28,6 @@ class GemViewModel: ViewModel() {
|
||||||
|
|
||||||
fun request(uri: URI){
|
fun request(uri: URI){
|
||||||
gemini.request(uri){ state ->
|
gemini.request(uri){ state ->
|
||||||
if(state is GemState.ResponseGemtext) {
|
|
||||||
when {
|
|
||||||
history.isEmpty() -> history.add(uri)
|
|
||||||
!history.takeLast(5).contains(uri) -> {
|
|
||||||
println("Adding $uri to runtime history")
|
|
||||||
history.add(uri)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onState(state)
|
onState(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,26 +48,6 @@ class GemViewModel: ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun canGoBack(): Boolean = history.size > 1
|
fun canGoBack(): Boolean = gemini.canGoBack()
|
||||||
|
fun goBack() = gemini.goBack(onState)
|
||||||
@ExperimentalStdlibApi
|
|
||||||
fun goBack(){
|
|
||||||
when {
|
|
||||||
history.size >= 2 -> {
|
|
||||||
logHistory()
|
|
||||||
val previous = history[history.size - 2]
|
|
||||||
history.removeLast()
|
|
||||||
|
|
||||||
println("Requesting history item: $previous")
|
|
||||||
request(previous)
|
|
||||||
logHistory()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun logHistory(){
|
|
||||||
history.forEach { uri ->
|
|
||||||
println("History: $uri")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue