Recently Written · git

sbm-android

sbm bookmarks for Android: fuzzy search, share to add, sync with bm and the browser add-on

git clone https://github.com/equwal/sbm-android

Log | Files | Refs


app/src/main/java/com/equwal/sbm/AddActivity.kt (2635 bytes)

1 package com.equwal.sbm
2 
3 import android.app.Activity
4 import android.content.Intent
5 import android.os.Bundle
6 import android.view.View
7 import android.widget.Button
8 import android.widget.EditText
9 import android.widget.TextView
10 import android.widget.Toast
11 
12 /** Add a bookmark: from "Share" in a browser, or from the menu of the list. */
13 class AddActivity : Activity() {
14     override fun onCreate(state: Bundle?) {
15         super.onCreate(state)
16         if (Store.file(this) == null) {
17             toast(getString(R.string.choose_first))
18             finish()
19             return
20         }
21         setContentView(R.layout.activity_add)
22         val url = findViewById<EditText>(R.id.url)
23         val desc = findViewById<EditText>(R.id.desc)
24         val tags = findViewById<EditText>(R.id.tags)
25 
26         if (state == null && intent.action == Intent.ACTION_SEND) {
27             val text = intent.getStringExtra(Intent.EXTRA_TEXT).orEmpty()
28             url.setText(Tsv.firstUrl(text) ?: text.trim())
29             desc.setText(intent.getStringExtra(Intent.EXTRA_SUBJECT).orEmpty())
30         }
31 
32         val known = try {
33             Tsv.parse(Store.read(this)).flatMap { it.tags }.distinct().sorted()
34         } catch (e: Exception) {
35             emptyList()
36         }
37         val hint = findViewById<TextView>(R.id.known)
38         if (known.isEmpty()) {
39             hint.visibility = View.GONE
40         } else {
41             hint.text = getString(R.string.known_tags, known.joinToString(" "))
42         }
43 
44         findViewById<Button>(R.id.cancel).setOnClickListener { finish() }
45         findViewById<Button>(R.id.save).setOnClickListener {
46             val b = Bookmark(
47                 url.text.toString().filterNot { it.isWhitespace() },
48                 Tsv.clean(desc.text.toString()).trim(),
49                 tags.text.toString().split(' ', '\t', '\n').filter { it.isNotEmpty() })
50             if (b.url.isEmpty()) {
51                 url.error = getString(R.string.need_url)
52                 return@setOnClickListener
53             }
54             try {
55                 val existing = Store.add(this, b)
56                 toast(if (existing == null) getString(R.string.added) else getString(R.string.already, existing.desc.ifEmpty { existing.url }))
57                 // The list syncs when it opens; this sends the new bookmark now.
58                 if (existing == null && Sync.signedIn(this)) Sync.run(this) {}
59                 finish()
60             } catch (e: Exception) {
61                 toast(getString(R.string.cannot_write, e.message))
62             }
63         }
64     }
65 
66     private fun toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_LONG).show()
67 }