diff --git a/app/src/main/java/oppen/ariane/io/gemini/GeminiDatasource.kt b/app/src/main/java/oppen/ariane/io/gemini/GeminiDatasource.kt index e6e7d84..ece0277 100644 --- a/app/src/main/java/oppen/ariane/io/gemini/GeminiDatasource.kt +++ b/app/src/main/java/oppen/ariane/io/gemini/GeminiDatasource.kt @@ -40,6 +40,7 @@ class GeminiDatasource(private val context: Context): Datasource { val cached = RuntimeCache.get(uri) when { cached != null -> { + updateHistory(uri) onUpdate(GemState.ResponseGemtext(uri, cached.first, cached.second)) return } @@ -57,7 +58,10 @@ class GeminiDatasource(private val context: Context): Datasource { if(parsedUri.isGemini()){ val cached = RuntimeCache.get(parsedUri) when { - cached != null -> onUpdate(GemState.ResponseGemtext(parsedUri.toURI(), cached.first, cached.second)) + cached != null -> { + updateHistory(uri) + onUpdate(GemState.ResponseGemtext(parsedUri.toURI(), cached.first, cached.second)) + } else -> request(parsedUri.toURI(), forceDownload, onUpdate) } }else{ @@ -100,14 +104,14 @@ class GeminiDatasource(private val context: Context): Datasource { "TLS_ALL" -> socket.enabledProtocols = socket.supportedProtocols else -> socket.enabledProtocols = arrayOf(protocol) } - + println("Ariane socket handshake with ${uri.host} on port $port") socket.startHandshake() }catch (ce: ConnectException){ - println("socket error: $ce") + println("Ariane socket error: $ce") onUpdate(GemState.ResponseError(GeminiResponse.Header(-1, ce.message ?: ce.toString()))) return }catch (she: SSLHandshakeException){ - println("socket error: $she") + println("Ariane socket error: $she") onUpdate(GemState.ResponseError(GeminiResponse.Header(-2, she.message ?: she.toString()))) return } @@ -118,7 +122,9 @@ class GeminiDatasource(private val context: Context): Datasource { val bufferedWriter = BufferedWriter(outputStreamWriter) val outWriter = PrintWriter(bufferedWriter) - outWriter.print(uri.toString() + "\r\n") + val requestEntity = uri.toString() + "\r\n" + println("Ariane socket requesting $requestEntity") + outWriter.print(requestEntity) outWriter.flush() if (outWriter.checkError()) { @@ -183,11 +189,18 @@ class GeminiDatasource(private val context: Context): Datasource { RuntimeCache.put(uri, header, processed) - if(runtimeHistory.isEmpty() || runtimeHistory.last().toString() != uri.toString()) runtimeHistory.add(uri) + updateHistory(uri) onUpdate(GemState.ResponseGemtext(uri, header, processed)) } + private fun updateHistory(uri: URI) { + if (runtimeHistory.isEmpty() || runtimeHistory.last().toString() != uri.toString()) { + runtimeHistory.add(uri) + println("Ariane added $uri to runtime history (size ${runtimeHistory.size})") + } + } + private fun getString(socket: SSLSocket?, uri: URI, header: GeminiResponse.Header, onUpdate: (state: GemState) -> Unit){ val content = socket?.inputStream?.bufferedReader().use { reader -> reader?.readText() @@ -235,7 +248,7 @@ class GeminiDatasource(private val context: Context): Datasource { } } - override fun canGoBack(): Boolean = runtimeHistory.size > 1 + override fun canGoBack(): Boolean = runtimeHistory.isEmpty() || runtimeHistory.size > 1 override fun goBack(onUpdate: (state: GemState) -> Unit) { runtimeHistory.removeLast() diff --git a/app/src/main/java/oppen/ariane/ui/GemActivity.kt b/app/src/main/java/oppen/ariane/ui/GemActivity.kt index 2c75b7b..9f0e860 100644 --- a/app/src/main/java/oppen/ariane/ui/GemActivity.kt +++ b/app/src/main/java/oppen/ariane/ui/GemActivity.kt @@ -411,6 +411,7 @@ class GemActivity : AppCompatActivity() { if(model.canGoBack()){ model.goBack() }else{ + println("Ariane history is empty - exiting") super.onBackPressed() } }