浏览代码

Users can now specify a note label to apply to their sent notes.
* Some UI shuffling and tweaks to support the new capabiltiy.
* Bump gradle version.

Casey Doran 4 年之前
父节点
当前提交
24391ed354

+ 1 - 1
app/src/main/AndroidManifest.xml

@@ -16,7 +16,7 @@
         android:theme="@style/AppTheme">
         <activity
             android:name=".LoginActivity"
-            android:label="Trilium connection setup" />
+            android:label="@string/trilium_sender_settings" />
         <activity
             android:name=".MainActivity"
             android:label="@string/app_name"

+ 37 - 1
app/src/main/java/io/github/zadam/triliumsender/LoginActivity.kt

@@ -3,6 +3,7 @@ package io.github.zadam.triliumsender
 import android.os.Bundle
 import android.text.TextUtils
 import android.util.Log
+import android.view.View
 import android.view.inputmethod.EditorInfo
 import android.widget.TextView
 import android.widget.Toast
@@ -37,6 +38,17 @@ class LoginActivity : AppCompatActivity() {
         })
 
         loginButton.setOnClickListener { attemptLogin() }
+
+        // Check if we're already set-up.
+        setSetupStatus()
+    }
+
+    override fun onStop() {
+        // Save edited label or address. Use apitoken from existing settings.
+        val settings = TriliumSettings(this)
+        TriliumSettings(this@LoginActivity).save(triliumAddressEditText.text.toString(), settings.apiToken, labelEditText.text.toString())
+        super.onStop()
+
     }
 
     /**
@@ -59,6 +71,7 @@ class LoginActivity : AppCompatActivity() {
         val triliumAddress = triliumAddressEditText.text.toString()
         val username = usernameEditText.text.toString()
         val password = passwordEditText.text.toString()
+        val noteLabel = labelEditText.text.toString()
 
         // Check for an empty URL. Flag and abort if so.
         if (TextUtils.isEmpty(triliumAddress)) {
@@ -100,7 +113,7 @@ class LoginActivity : AppCompatActivity() {
 
             if (loginResult.success) {
                 // Store the address and api token.
-                TriliumSettings(this@LoginActivity).save(triliumAddress, loginResult.token!!)
+                TriliumSettings(this@LoginActivity).save(triliumAddress, loginResult.token!!, noteLabel)
                 // Announce our success.
                 Toast.makeText(this@LoginActivity, getString(R.string.connection_configured_correctly), Toast.LENGTH_LONG).show()
                 // End the activity.
@@ -187,4 +200,27 @@ class LoginActivity : AppCompatActivity() {
             return@withContext LoginResult(true, null, token)
         }
     }
+
+    /**
+     * Checks the settings object and updates the UI to match the current state.
+     */
+    private fun setSetupStatus() {
+        val settings = TriliumSettings(this)
+
+        // Always attempt to restore the note label.
+        labelEditText.setText(settings.noteLabel)
+
+        if (!settings.isConfigured()) {
+            // Hide the logged-in indicator.
+            loggedInIndicator.visibility = View.INVISIBLE
+
+        } else {
+            // Populate the text editors for URL.
+            triliumAddressEditText.setText(settings.triliumAddress)
+            // Indicate successful login.
+            loggedInIndicator.visibility = View.VISIBLE
+            // TODO: This does not actually validate the current API Token.
+            // In the future we should update this to only show after a quick validation of the token.
+        }
+    }
 }

+ 1 - 1
app/src/main/java/io/github/zadam/triliumsender/MainActivity.kt

@@ -16,7 +16,7 @@ class MainActivity : AppCompatActivity() {
         super.onCreate(savedInstanceState)
 
         if (resetSetup) {
-            TriliumSettings(this).save("", "")
+            TriliumSettings(this).save("", "", "")
         }
 
         setContentView(R.layout.activity_main)

+ 20 - 1
app/src/main/java/io/github/zadam/triliumsender/SendNoteActivity.kt

@@ -4,6 +4,7 @@ import android.content.Intent
 import android.content.pm.PackageManager
 import android.os.Bundle
 import android.util.Log
+import android.view.View
 import android.widget.TextView
 import android.widget.Toast
 import androidx.appcompat.app.AppCompatActivity
@@ -18,6 +19,7 @@ import kotlinx.coroutines.withContext
 import okhttp3.OkHttpClient
 import okhttp3.Request
 import okhttp3.RequestBody.Companion.toRequestBody
+import org.json.JSONArray
 import org.json.JSONObject
 
 class SendNoteActivity : AppCompatActivity() {
@@ -28,13 +30,19 @@ class SendNoteActivity : AppCompatActivity() {
         setContentView(R.layout.activity_send_note)
 
         val settings = TriliumSettings(this)
-
         if (!settings.isConfigured()) {
             // We can't do anything useful if we're not configured. Abort out.
             Toast.makeText(this, getString(R.string.sender_not_configured_note), Toast.LENGTH_LONG).show()
             finish()
             return
         }
+        if (settings.noteLabel.isNotEmpty()) {
+            // We have a label to apply to this note! Indicate in the UI.
+            labelList.text = getString(R.string.label_preview_template, settings.noteLabel)
+        } else {
+            // Hide the label text preview.
+            labelList.visibility = View.GONE
+        }
 
         // If we're a share-intent, pre-populate the note.
         when (intent?.action) {
@@ -125,6 +133,7 @@ class SendNoteActivity : AppCompatActivity() {
      */
     private suspend fun sendNote(noteTitle: String, noteText: String, triliumAddress: String, apiToken: String): Boolean {
         val tag = "SendNoteCoroutine"
+        val settings = TriliumSettings(this)
 
         return withContext(Dispatchers.IO) {
             val client = OkHttpClient()
@@ -139,6 +148,16 @@ class SendNoteActivity : AppCompatActivity() {
             }
             json.put("content", HtmlConverter().convertPlainTextToHtml(noteText))
 
+            if (settings.noteLabel.isNotEmpty()) {
+                // The api actually supports a list of key-value pairs, but for now we just write one label.
+                val label = JSONObject()
+                label.put("name", settings.noteLabel)
+                label.put("value", "")
+                val labelList = JSONArray()
+                labelList.put(label)
+                json.put("labels", labelList)
+            }
+
             val body = json.toString().toRequestBody(Utils.JSON)
 
             val request = Request.Builder()

+ 6 - 1
app/src/main/java/io/github/zadam/triliumsender/services/TriliumSettings.kt

@@ -8,12 +8,14 @@ class TriliumSettings constructor(ctx: Activity) {
         const val PREF_NAME = "io.github.zadam.triliumsender.setup"
         const val PREF_TRILIUM_ADDRESS = "trilium_address"
         const val PREF_API_TOKEN = "api_token"
+        const val PREF_NOTE_LABEL = "trilium_note_label"
     }
 
-    fun save(triliumAddress: String, apiToken: String) {
+    fun save(triliumAddress: String, apiToken: String, noteLabel: String) {
         val editor = prefs.edit()
         editor.putString(PREF_TRILIUM_ADDRESS, triliumAddress)
         editor.putString(PREF_API_TOKEN, apiToken)
+        editor.putString(PREF_NOTE_LABEL, noteLabel)
         editor.apply()
     }
 
@@ -25,5 +27,8 @@ class TriliumSettings constructor(ctx: Activity) {
     val apiToken
         get() = prefs.getString(PREF_API_TOKEN, "")!!
 
+    val noteLabel
+        get() = prefs.getString(PREF_NOTE_LABEL, "")!!
+
     fun isConfigured() = !triliumAddress.isBlank() && !apiToken.isBlank()
 }

+ 35 - 0
app/src/main/res/layout/activity_login.xml

@@ -28,6 +28,12 @@
             android:layout_height="wrap_content"
             android:orientation="vertical">
 
+            <TextView
+                android:id="@+id/LoginSettingsLabel"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/login_settings_header" />
+
             <com.google.android.material.textfield.TextInputLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content">
@@ -88,6 +94,35 @@
                 android:text="@string/action_login"
                 android:textStyle="bold" />
 
+            <TextView
+                android:id="@+id/loggedInIndicator"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/logged_in_successfully" />
+
+            <TextView
+                android:id="@+id/NoteSettingsLabel"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="16dp"
+                android:text="@string/note_settings_header" />
+
+            <com.google.android.material.textfield.TextInputLayout
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content">
+
+                <EditText
+                    android:id="@+id/labelEditText"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:hint="@string/prompt_label"
+                    android:importantForAutofill="no"
+                    android:inputType="text"
+                    android:maxLines="1"
+                    android:singleLine="true" />
+
+            </com.google.android.material.textfield.TextInputLayout>
+
         </LinearLayout>
     </ScrollView>
 </LinearLayout>

+ 7 - 0
app/src/main/res/layout/activity_send_note.xml

@@ -22,6 +22,12 @@
             android:hint="@string/note_title"
             android:importantForAutofill="no" />
 
+        <TextView
+            android:id="@+id/labelList"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:text="" />
+
         <EditText
             android:id="@+id/noteContentEditText"
             android:layout_width="match_parent"
@@ -47,5 +53,6 @@
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         android:focusable="true"
+        android:contentDescription="@string/send_fab_content_desc"
         app:srcCompat="@android:drawable/ic_menu_send" />
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 8 - 1
app/src/main/res/values/strings.xml

@@ -3,13 +3,14 @@
     <string name="prompt_trilium_address">https://trilium_host:8080</string>
     <string name="prompt_username">User name</string>
     <string name="prompt_password">Password</string>
+    <string name="prompt_label">Label for sent notes</string>
     <string name="action_login">Login</string>
     <string name="action_login_short">Login</string>
     <string name="error_network_error">Network error</string>
     <string name="error_unexpected_response">Unexpected response from server</string>
     <string name="error_incorrect_credentials">Entered credentials are incorrect</string>
     <string name="error_field_required">This field is required</string>
-    <string name="trilium_connection_settings">Trilium connection settings</string>
+    <string name="trilium_connection_settings">Settings</string>
     <string name="note_title">Note title</string>
     <string name="note_text">Note text</string>
     <string name="setup_not_complete">Trilium connection setup isn\'t finished yet.</string>
@@ -24,4 +25,10 @@
     <string name="sending_note_complete">Note sent to Trilium.</string>
     <string name="sending_note_failed">Sending note to Trilium failed.</string>
     <string name="share_note_title">Shared from %s</string>
+    <string name="login_settings_header">Login Settings</string>
+    <string name="note_settings_header">Note Settings</string>
+    <string name="label_preview_template">#%s</string>
+    <string name="logged_in_successfully">Login credentials are saved! ✅</string>
+    <string name="send_fab_content_desc">Send.</string>
+    <string name="trilium_sender_settings">Trilium Sender Settings</string>
 </resources>

+ 1 - 1
build.gradle

@@ -7,7 +7,7 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:4.0.2'
+        classpath 'com.android.tools.build:gradle:4.1.0'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 
         // NOTE: Do not place your application dependencies here; they belong

+ 2 - 2
gradle/wrapper/gradle-wrapper.properties

@@ -1,6 +1,6 @@
-#Sun Sep 13 12:15:13 PDT 2020
+#Sun Nov 08 18:02:32 PST 2020
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip