Skip to content

Commit

Permalink
orgzly#38 WebDAV sync support
Browse files Browse the repository at this point in the history
Add test button
Translations to Russian
  • Loading branch information
bemyak committed Sep 1, 2019
1 parent 6a87dac commit 8096217
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 8 deletions.
100 changes: 93 additions & 7 deletions app/src/main/java/com/orgzly/android/ui/repo/WebdavRepoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ package com.orgzly.android.ui.repo
import android.app.Activity
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import android.net.Uri
import android.os.AsyncTask
import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import androidx.databinding.DataBindingUtil
import com.orgzly.R
import com.orgzly.android.prefs.RepoPreferences
import com.orgzly.android.repos.RepoFactory
import com.orgzly.android.repos.WebdavRepo
import com.orgzly.android.repos.WebdavRepo.Companion.PASSWORD_PREF_KEY
import com.orgzly.android.repos.WebdavRepo.Companion.USERNAME_PREF_KEY
import com.orgzly.android.ui.CommonActivity
import com.orgzly.databinding.ActivityRepoWebdavBinding
import com.thegrizzlylabs.sardineandroid.impl.SardineException
import javax.inject.Inject

class WebdavRepoActivity : CommonActivity() {
Expand Down Expand Up @@ -44,6 +50,8 @@ class WebdavRepoActivity : CommonActivity() {
val password = prefs.getStringValue(PASSWORD_PREF_KEY, "")
binding.activityRepoWebdavPassword.setText(password)
}

binding.activityRepoWebdavTestButton.setOnClickListener { testRepo() }
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
Expand Down Expand Up @@ -72,12 +80,7 @@ class WebdavRepoActivity : CommonActivity() {
}

private fun saveAndFinish() {
val uriString = binding.activityRepoWebdavUrl.text.toString().trim { it <= ' ' }.let {
if (it.startsWith("http")) {
it.replaceFirst("http", "webdav")
}
it
}
val uriString = getUriString()
val username = binding.activityRepoWebdavUsername.text.toString().trim { it <= ' ' }
val password = binding.activityRepoWebdavPassword.text.toString().trim { it <= ' ' }

Expand All @@ -103,16 +106,99 @@ class WebdavRepoActivity : CommonActivity() {

}

private fun getUriString(): String {
return binding.activityRepoWebdavUrl.text.toString().trim { it <= ' ' }.let {
if (it.startsWith("http")) {
it.replaceFirst("http", "webdav")
}
it
}
}

private fun getWebdavUrlError(url: String): String? {
return when {
TextUtils.isEmpty(url) -> getString(R.string.can_not_be_empty)
TextUtils.isEmpty(url) -> getString(R.string.webdav_url_can_not_be_empty)
!webdavUrlRegex.matches(url) -> getString(R.string.invalid_repo_url)
else -> null
}
}

private fun testRepo() {
val uriString = getUriString()
val username = binding.activityRepoWebdavUsername.text.toString().trim { it <= ' ' }
val password = binding.activityRepoWebdavPassword.text.toString().trim { it <= ' ' }

getWebdavUrlError(uriString)?.let {
setTestResultErrorText(it)
return
}

binding.activityRepoWebdavTestButton.isEnabled = false

RepoTester().execute(uriString, username, password)

}

private fun setTestResultErrorText(text: String) {
binding.activityRepoWebdavTestResult.setTextColor(Color.RED)
binding.activityRepoWebdavTestResult.text = text
}

private fun setTestResultOkText(text: String) {
binding.activityRepoWebdavTestResult.setTextColor(Color.GREEN)
binding.activityRepoWebdavTestResult.text = text
}

private fun setTestResultWarnText(text: String) {
binding.activityRepoWebdavTestResult.setTextColor(Color.YELLOW)
binding.activityRepoWebdavTestResult.text = text
}

private inner class RepoTester : AsyncTask<String, Void, Void>() {
override fun doInBackground(vararg params: String?): Void? {

try {
binding.activityRepoWebdavTestResult.text = ""

val uriString = params[0]
val username = params[1]
val password = params[2]

val uri = Uri.parse(uriString)
val repo = WebdavRepo(uri, username, password)
if (repo.books.isEmpty()) {
setTestResultWarnText(getString(R.string.webdav_test_warn_no_books))
} else {
setTestResultOkText(getString(R.string.webdav_test_ok))
}

} catch (e: Exception) {
when (e) {
is SardineException -> {
when (e.statusCode) {
401 -> setTestResultErrorText(getString(R.string.webdav_test_error_auth))
else -> setTestResultErrorText(arrayOf(e.statusCode, ":", e.responsePhrase).joinToString(" "))
}

}
else -> setTestResultErrorText(e.message
?: getString(R.string.webdav_test_error_unknown))

}
} finally {
// Avoiding bug described here: https://stackoverflow.com/a/10687660
handler.post {
binding.activityRepoWebdavTestButton.isEnabled = true
}
return null
}
}

}

companion object {
private const val ARG_REPO_ID = "repo_id"
private val handler = Handler()

@JvmStatic
@JvmOverloads
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_repo_webdav.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/activity_repo_webdav_test_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:text="@string/webdav_test" />

<TextView
android:id="@+id/activity_repo_webdav_test_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />

</LinearLayout>

</ScrollView>
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values-ru-rRU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,14 @@
<item quantity="many">Переместить <b>%d</b> заметок</item>
<item quantity="other">Refile <b>%d</b> notes</item>
</plurals>
<string name="webdav_auth_password_hint">Пароль</string>
<string name="webdav_auth_username_hint">Имя пользователя</string>
<string name="webdav_test">Тест</string>
<string name="webdav_test_ok">Ок</string>
<string name="webdav_url_hint">URL (пример: webdavs://example.com/my-org-files/)</string>
<string name="webdav_test_error_auth">Неверный логин или пароль</string>
<string name="webdav_test_error_unknown">Произошла неизвестная ошибка</string>
<string name="webdav_test_error_url">Неверный URL</string>
<string name="webdav_test_warn_no_books">Блокноты не найдены</string>
<string name="webdav_url_can_not_be_empty">Url не может быть пустым</string>
</resources>
9 changes: 8 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,17 @@
<string name="git_clone_error_target_not_empty">The target directory is not empty</string>
<string name="git_clone_error_uri_not_supported">The remote address is not supported</string>

<string name="webdav">Webdav</string>
<string name="webdav" translatable="false">Webdav</string>
<string name="webdav_url_hint">URL (e.g. webdavs://example.com/my-org-files/)</string>
<string name="webdav_auth_username_hint">Username</string>
<string name="webdav_auth_password_hint">Password</string>
<string name="webdav_url_can_not_be_empty">URL cannot be empty</string>
<string name="webdav_test">Test</string>
<string name="webdav_test_ok">OK</string>
<string name="webdav_test_warn_no_books">No books found</string>
<string name="webdav_test_error_auth">Login or password is invalid</string>
<string name="webdav_test_error_url">URL is invalid</string>
<string name="webdav_test_error_unknown">Unknown error occurred</string>

<!-- Share button - error messages (set as title). -->
<string name="share_action_not_set">Share action not set</string>
Expand Down

0 comments on commit 8096217

Please sign in to comment.