2020-08-15 14:52:27 +00:00
|
|
|
package oppen.tva.ui
|
|
|
|
|
2020-08-18 10:40:10 +00:00
|
|
|
import android.content.ClipData
|
|
|
|
import android.content.ClipboardManager
|
2020-08-18 16:17:04 +00:00
|
|
|
import android.content.Context
|
2020-08-15 20:33:44 +00:00
|
|
|
import android.content.Intent
|
|
|
|
import android.net.Uri
|
2020-08-15 14:52:27 +00:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.inputmethod.EditorInfo
|
|
|
|
import androidx.activity.viewModels
|
2020-08-18 19:02:16 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2020-08-15 20:33:44 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2020-08-15 14:52:27 +00:00
|
|
|
import androidx.databinding.DataBindingUtil
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2020-08-18 10:40:10 +00:00
|
|
|
import com.google.android.material.snackbar.Snackbar
|
2020-08-15 14:52:27 +00:00
|
|
|
import oppen.tva.R
|
2020-08-20 13:55:59 +00:00
|
|
|
import oppen.tva.Tva
|
2020-08-15 14:52:27 +00:00
|
|
|
import oppen.tva.databinding.ActivityTvaBinding
|
2020-08-18 19:02:16 +00:00
|
|
|
import oppen.tva.io.GeminiResponse
|
2020-08-19 11:33:16 +00:00
|
|
|
import oppen.tva.io.RuntimeCache
|
2020-08-15 14:52:27 +00:00
|
|
|
import oppen.tva.io.TvaState
|
2020-08-20 13:52:24 +00:00
|
|
|
import oppen.tva.io.history.tabs.TabHistoryInterface
|
|
|
|
import oppen.tva.io.history.uris.HistoryInterface
|
2020-08-20 14:16:22 +00:00
|
|
|
import oppen.tva.ui.modals_menus.about.AboutDialog
|
|
|
|
import oppen.tva.ui.modals_menus.history.HistoryDialog
|
|
|
|
import oppen.tva.ui.modals_menus.input.InputDialog
|
|
|
|
import oppen.tva.ui.modals_menus.overflow.OverflowPopup
|
|
|
|
import oppen.tva.ui.modals_menus.set_home.SetHomeDialog
|
|
|
|
import oppen.tva.ui.modals_menus.tabs.NewTabPopup
|
|
|
|
import oppen.tva.ui.modals_menus.tabs.TabsDialog
|
2020-08-16 21:30:24 +00:00
|
|
|
import oppen.visibleRetainingSpace
|
2020-08-15 14:52:27 +00:00
|
|
|
|
2020-08-15 20:33:44 +00:00
|
|
|
|
2020-08-15 14:52:27 +00:00
|
|
|
class TvaActivity : AppCompatActivity() {
|
|
|
|
|
2020-08-20 14:57:38 +00:00
|
|
|
private var inSearch = false
|
|
|
|
|
2020-08-15 14:52:27 +00:00
|
|
|
private val model by viewModels<TvaViewModel>()
|
|
|
|
private lateinit var binding: ActivityTvaBinding
|
2020-08-20 13:52:24 +00:00
|
|
|
private lateinit var history: HistoryInterface
|
2020-08-16 22:21:25 +00:00
|
|
|
private val adapter = GemtextAdapter { uri, longTap, view ->
|
|
|
|
if(longTap){
|
2020-08-17 20:25:39 +00:00
|
|
|
NewTabPopup.show(view){ menuId ->
|
|
|
|
when (menuId) {
|
|
|
|
R.id.link_menu_open_in_new_tab -> {
|
|
|
|
model.newTab(uri)
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 22:21:25 +00:00
|
|
|
}
|
|
|
|
}else{
|
2020-08-20 14:57:38 +00:00
|
|
|
//Reset input text hint after user has been searching
|
|
|
|
if(inSearch) {
|
|
|
|
binding.addressEdit.hint = getString(R.string.main_input_hint)
|
|
|
|
inSearch = false
|
|
|
|
}
|
2020-08-16 22:21:25 +00:00
|
|
|
model.request(uri)
|
|
|
|
}
|
2020-08-15 14:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_tva)
|
|
|
|
binding.viewmodel = model
|
|
|
|
binding.lifecycleOwner = this
|
|
|
|
|
|
|
|
binding.gemtextRecycler.layoutManager = LinearLayoutManager(this)
|
|
|
|
binding.gemtextRecycler.adapter = adapter
|
|
|
|
|
2020-08-20 13:52:24 +00:00
|
|
|
history = HistoryInterface.default(this)
|
|
|
|
|
|
|
|
model.initialise(TabHistoryInterface.default(this)){ state ->
|
2020-08-15 14:52:27 +00:00
|
|
|
when(state){
|
2020-08-18 16:17:04 +00:00
|
|
|
is TvaState.AppQuery -> runOnUiThread{ showAlert("App backdoor/query not implemented yet") }
|
2020-08-20 14:57:38 +00:00
|
|
|
is TvaState.ResponseInput -> runOnUiThread {
|
|
|
|
loadingView(false)
|
|
|
|
InputDialog.show(this, state){ queryAddress ->
|
2020-08-20 14:16:22 +00:00
|
|
|
model.request(queryAddress)
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 21:30:24 +00:00
|
|
|
is TvaState.Requesting -> loadingView(true)
|
2020-08-15 20:33:44 +00:00
|
|
|
is TvaState.NotGeminiRequest -> externalProtocol(state)
|
2020-08-18 19:02:16 +00:00
|
|
|
is TvaState.ResponseError -> showAlert("${GeminiResponse.getCodeString(state.header.code)}: ${state.header.meta}")
|
2020-08-18 16:17:04 +00:00
|
|
|
is TvaState.ResponseGemtext -> renderGemtext(state)
|
|
|
|
is TvaState.ResponseText -> runOnUiThread{ showAlert("Plain text display not implemented") }
|
2020-08-17 20:25:39 +00:00
|
|
|
is TvaState.TabChange -> binding.tabCount.text = "${state.count}"
|
|
|
|
is TvaState.Blank -> {
|
|
|
|
binding.addressEdit.setText("")
|
|
|
|
adapter.render(arrayListOf())
|
|
|
|
}
|
2020-08-15 14:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
binding.addressEdit.setOnEditorActionListener { _, actionId, _ ->
|
|
|
|
when (actionId) {
|
|
|
|
EditorInfo.IME_ACTION_GO -> {
|
2020-08-20 14:57:38 +00:00
|
|
|
val input = binding.addressEdit.text.toString()
|
|
|
|
|
|
|
|
if(input.startsWith("gemini://")){
|
|
|
|
model.request(input)
|
|
|
|
}else{
|
|
|
|
model.request("${Tva.GEMINI_USER_SEARCH_BASE}$input")
|
|
|
|
}
|
|
|
|
|
2020-08-15 14:52:27 +00:00
|
|
|
return@setOnEditorActionListener true
|
|
|
|
}
|
|
|
|
else -> return@setOnEditorActionListener false
|
|
|
|
}
|
|
|
|
}
|
2020-08-15 21:12:17 +00:00
|
|
|
|
2020-08-18 16:17:04 +00:00
|
|
|
binding.more.setOnClickListener {
|
|
|
|
OverflowPopup.show(binding.more){menuId ->
|
|
|
|
when (menuId) {
|
2020-08-20 14:57:38 +00:00
|
|
|
R.id.overflow_menu_search -> {
|
|
|
|
binding.addressEdit.hint = getString(R.string.main_input_search_hint)
|
|
|
|
binding.addressEdit.text?.clear()
|
|
|
|
binding.addressEdit.requestFocus()
|
|
|
|
inSearch = true
|
|
|
|
}
|
2020-08-18 16:17:04 +00:00
|
|
|
R.id.overflow_menu_share -> {
|
|
|
|
Intent().apply {
|
|
|
|
action = Intent.ACTION_SEND
|
|
|
|
putExtra(Intent.EXTRA_TEXT, binding.addressEdit.text.toString())
|
|
|
|
type = "text/plain"
|
|
|
|
startActivity(Intent.createChooser(this, null))
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 13:52:24 +00:00
|
|
|
R.id.overflow_menu_reload -> {
|
|
|
|
val address = binding.addressEdit.text.toString()
|
|
|
|
RuntimeCache.remove(address)
|
|
|
|
model.request(address)
|
|
|
|
}
|
|
|
|
R.id.overflow_menu_history -> HistoryDialog.show(this){ historyAddress ->
|
|
|
|
model.request(historyAddress)
|
2020-08-19 11:33:16 +00:00
|
|
|
}
|
2020-08-18 20:21:43 +00:00
|
|
|
R.id.overflow_menu_about -> AboutDialog.show(this)
|
2020-08-18 16:17:04 +00:00
|
|
|
R.id.overflow_menu_set_home -> {
|
2020-08-18 20:21:43 +00:00
|
|
|
SetHomeDialog.show(this, binding.addressEdit.text.toString()){
|
2020-08-18 16:17:04 +00:00
|
|
|
showAlert("Home capsule updated")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
R.id.overflow_menu_copy -> {
|
|
|
|
val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
|
|
|
|
val clip = ClipData.newPlainText(getString(R.string.gemini_address), binding.addressEdit.text.toString())
|
|
|
|
clipboard.setPrimaryClip(clip)
|
|
|
|
Snackbar.make(binding.root, getString(R.string.address_copied_to_clipboard), Snackbar.LENGTH_SHORT).setAction(R.string.share) {
|
|
|
|
Intent().apply {
|
|
|
|
action = Intent.ACTION_SEND
|
|
|
|
putExtra(Intent.EXTRA_TEXT, binding.addressEdit.text.toString())
|
|
|
|
type = "text/plain"
|
|
|
|
startActivity(Intent.createChooser(this, null))
|
|
|
|
}
|
|
|
|
}.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
binding.home.setOnClickListener {
|
2020-08-20 14:16:22 +00:00
|
|
|
val prefs = getSharedPreferences("oppen.tva.ui.dialogs.set_home", Context.MODE_PRIVATE)
|
2020-08-20 13:55:59 +00:00
|
|
|
val home = prefs.getString("home", Tva.DEFAULT_HOME_CAPSULE)
|
2020-08-18 16:17:04 +00:00
|
|
|
model.request(home!!)
|
|
|
|
}
|
2020-08-17 20:25:39 +00:00
|
|
|
binding.tabs.setOnClickListener {
|
|
|
|
TabsDialog().show(this, model)
|
|
|
|
}
|
2020-08-15 14:52:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 13:52:24 +00:00
|
|
|
override fun onNewIntent(intent: Intent?) {
|
|
|
|
super.onNewIntent(intent)
|
|
|
|
intent?.data.toString().let{model.request(it)}
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:17:04 +00:00
|
|
|
private fun showAlert(message: String) = runOnUiThread{
|
|
|
|
loadingView(false)
|
2020-08-18 19:02:16 +00:00
|
|
|
|
|
|
|
if(message.length > 40){
|
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.setMessage(message)
|
|
|
|
.show()
|
|
|
|
}else {
|
|
|
|
Snackbar.make(binding.root, message, Snackbar.LENGTH_SHORT).show()
|
|
|
|
}
|
2020-08-18 16:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun externalProtocol(state: TvaState.NotGeminiRequest) = runOnUiThread {
|
2020-08-16 21:30:24 +00:00
|
|
|
loadingView(false)
|
2020-08-15 20:33:44 +00:00
|
|
|
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(state.uri.toString()))
|
|
|
|
startActivity(browserIntent)
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:17:04 +00:00
|
|
|
private fun renderGemtext(state: TvaState.ResponseGemtext) = runOnUiThread {
|
2020-08-16 21:30:24 +00:00
|
|
|
loadingView(false)
|
2020-08-18 16:17:04 +00:00
|
|
|
binding.addressEdit.setText(state.uri.toString())
|
|
|
|
adapter.render(state.lines)
|
2020-08-20 13:52:24 +00:00
|
|
|
|
|
|
|
history.add(state.uri.toString())
|
2020-08-15 14:52:27 +00:00
|
|
|
}
|
2020-08-15 20:20:15 +00:00
|
|
|
|
2020-08-16 21:30:24 +00:00
|
|
|
private fun loadingView(visible: Boolean) = runOnUiThread {
|
|
|
|
binding.progressBar.visibleRetainingSpace(visible)
|
|
|
|
if(visible) binding.appBar.setExpanded(true)
|
|
|
|
}
|
|
|
|
|
2020-08-15 20:20:15 +00:00
|
|
|
@ExperimentalStdlibApi
|
|
|
|
override fun onBackPressed() {
|
|
|
|
if(model.canGoBack()){
|
|
|
|
model.goBack()
|
|
|
|
}else{
|
|
|
|
super.onBackPressed()
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 20:06:11 +00:00
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
model.persistTabState()
|
|
|
|
}
|
2020-08-15 14:52:27 +00:00
|
|
|
}
|