diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index a5b10b5..8a2cec4 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -32,6 +32,9 @@
+
+
+
diff --git a/app/src/main/java/telegra/ph/MainActivity.kt b/app/src/main/java/telegra/ph/MainActivity.kt
index 7123ba4..7867c2e 100644
--- a/app/src/main/java/telegra/ph/MainActivity.kt
+++ b/app/src/main/java/telegra/ph/MainActivity.kt
@@ -9,6 +9,7 @@ import android.view.MenuItem
import android.view.View
import com.afollestad.materialdialogs.MaterialDialog
import im.delight.android.webview.AdvancedWebView
+import java.net.URI
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
private val viewer: Viewer? by lazy {
@@ -30,15 +31,20 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
- if (accessToken().isBlank()) TelegraphApi.createAccount(shortName = "teleposter") { success, account, error ->
+ if (accessToken.isBlank()) TelegraphApi.createAccount(shortName = "teleposter") { success, account, error ->
if (success && account != null && account.accessToken != null) {
- saveAccessToken(account.accessToken)
+ accessToken = account.accessToken
} else {
showError(error)
}
}
- if (intent.action == Intent.ACTION_VIEW && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last())
- else loadEditor()
+ if (intent.action == Intent.ACTION_VIEW) {
+ val uri = URI.create(intent.dataString)
+ when (uri.host) {
+ "telegra.ph", "graph.org" -> loadPage(uri.path)
+ "edit.telegra.ph", "edit.graph.org" -> login(uri.toString())
+ }
+ }
}
private fun loadEditor(path: String? = null) {
@@ -51,7 +57,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
viewer?.visibility = View.GONE
currentPage = null
// Load
- if (path != null) TelegraphApi.getPage(accessToken(), path, true) { success, page, error ->
+ if (path != null) TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
if (success && page != null) {
isEdit = true
currentPage = page
@@ -66,6 +72,16 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
}
}
+ private fun login(authUrl: String) {
+ TelegraphApi.login(authUrl) { success, accessToken, account ->
+ if (success && accessToken != null) {
+ this.accessToken = accessToken
+ this.authorName = account?.authorName
+ showMessage(getString(R.string.success), getString(R.string.login_success))
+ } else showError(getString(R.string.login_failed))
+ }
+ }
+
private fun loadPage(path: String) {
runOnUiThread {
editorMode = false
@@ -75,7 +91,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
editor?.visibility = View.GONE
currentPage = null
// Load
- TelegraphApi.getPage(accessToken(), path, true) { success, page, error ->
+ TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
}
@@ -102,11 +118,14 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
}
}
- private fun showError(message: String? = null) {
+ private fun showError(message: String? = null) = showMessage(getString(R.string.error), message
+ ?: getString(R.string.error_desc))
+
+ private fun showMessage(title: String? = null, message: String? = null) {
runOnUiThread {
MaterialDialog.Builder(this)
- .title(R.string.error)
- .content(message ?: getString(R.string.error_desc))
+ .title(title ?: "")
+ .content(message ?: "")
.positiveText(android.R.string.ok)
.show()
}
@@ -170,15 +189,15 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
MaterialDialog.Builder(this)
.title(R.string.name_question)
.input(getString(R.string.name_hint), if (isEdit) currentPage?.authorName
- ?: authorName() ?: "" else authorName()
+ ?: authorName ?: "" else authorName
?: "", { _, name ->
- if (!isEdit) saveAuthorName(name.toString())
- if (isEdit) TelegraphApi.editPage(accessToken(), currentPage?.path
+ if (!isEdit) authorName = name.toString()
+ if (isEdit) TelegraphApi.editPage(accessToken, currentPage?.path
?: "", authorName = name.toString(), title = title.toString(), content = json
?: "", returnContent = true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
- } else TelegraphApi.createPage(accessToken(), content = json
+ } else TelegraphApi.createPage(accessToken, content = json
?: "", title = title.toString(), authorName = name.toString(), returnContent = true) { success, page, error ->
if (success && page != null) showPage(page)
else showError(error)
@@ -218,7 +237,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
true
}
R.id.published -> {
- TelegraphApi.getPageList(accessToken()) { success, pageList, error ->
+ TelegraphApi.getPageList(accessToken) { success, pageList, error ->
if (success && pageList != null && pageList.pages != null) {
MaterialDialog.Builder(this)
.title(R.string.published)
diff --git a/app/src/main/java/telegra/ph/Prefs.kt b/app/src/main/java/telegra/ph/Prefs.kt
index 800881c..9fa7bc5 100644
--- a/app/src/main/java/telegra/ph/Prefs.kt
+++ b/app/src/main/java/telegra/ph/Prefs.kt
@@ -29,14 +29,14 @@ fun Context.saveBookmarks(bookmarks: List>) {
).apply()
}
-fun Context.accessToken(): String = PreferenceManager.getDefaultSharedPreferences(this).getString("accessToken", "")
+var Context.accessToken: String
+ get() = PreferenceManager.getDefaultSharedPreferences(this).getString("accessToken", "")
+ set(value) {
+ PreferenceManager.getDefaultSharedPreferences(this).edit().putString("accessToken", value).apply()
+ }
-fun Context.saveAccessToken(token: String) {
- PreferenceManager.getDefaultSharedPreferences(this).edit().putString("accessToken", token).apply()
-}
-
-fun Context.authorName(): String? = PreferenceManager.getDefaultSharedPreferences(this).getString("authorName", null)
-
-fun Context.saveAuthorName(name: String) {
- PreferenceManager.getDefaultSharedPreferences(this).edit().putString("authorName", name).apply()
-}
\ No newline at end of file
+var Context.authorName: String?
+ get() = PreferenceManager.getDefaultSharedPreferences(this).getString("authorName", null)
+ set(value) {
+ PreferenceManager.getDefaultSharedPreferences(this).edit().putString("authorName", value).apply()
+ }
\ No newline at end of file
diff --git a/app/src/main/java/telegra/ph/TelegraphApi.kt b/app/src/main/java/telegra/ph/TelegraphApi.kt
index 59c1159..f3a344e 100644
--- a/app/src/main/java/telegra/ph/TelegraphApi.kt
+++ b/app/src/main/java/telegra/ph/TelegraphApi.kt
@@ -6,6 +6,8 @@ import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.FuelManager
import com.github.kittinunf.fuel.core.Request
import com.github.kittinunf.fuel.core.Response
+import com.github.kittinunf.fuel.core.interceptors.redirectResponseInterceptor
+import com.github.kittinunf.fuel.core.interceptors.validatorResponseInterceptor
import com.github.kittinunf.fuel.httpPost
import com.github.kittinunf.result.Result
import org.json.JSONArray
@@ -14,10 +16,20 @@ import java.net.HttpCookie
object TelegraphApi {
- // Telegraph
+ private var loginAccessToken: String? = null
init {
FuelManager.instance.basePath = "https://api.telegra.ph"
+ FuelManager.instance.removeAllResponseInterceptors()
+ FuelManager.instance.addResponseInterceptor {
+ telegraphLoginInterceptor()
+ }
+ // Fix login
+ FuelManager.instance.addResponseInterceptor {
+ redirectResponseInterceptor(FuelManager.instance)
+ validatorResponseInterceptor(200..299)
+ it
+ }
}
private fun callService(method: String, parameters: List>, handler: (Request, Response, Result) -> Unit) {
@@ -152,23 +164,27 @@ object TelegraphApi {
}
}
- // Telegra.ph
+ // Dirty hacks
fun login(authUrl: String, callback: (success: Boolean, accessToken: String?, account: Account?) -> Unit) {
- authUrl.httpPost().response { _, response, _ ->
- var token: String? = null
- response.headers["Set-Cookie"]
- ?.flatMap { HttpCookie.parse(it) }
- ?.find { it.name == "tph_token" }
- ?.let {
- token = it.value
- }
- if (token != null) getAccountInfo(accessToken = token!!) { success, account, _ ->
- if (success) callback(true, token, account)
+ loginAccessToken = null
+ authUrl.httpPost().response { _, _, _ ->
+ if (loginAccessToken != null) getAccountInfo(accessToken = loginAccessToken!!) { success, account, _ ->
+ if (success) callback(true, loginAccessToken, account)
else callback(false, null, null)
- }
- else callback(false, null, null)
+ } else callback(false, null, null)
}
}
+ private fun telegraphLoginInterceptor(): (Request, Response) -> Response =
+ { _, response ->
+ response.headers["Set-Cookie"]
+ ?.flatMap { HttpCookie.parse(it) }
+ ?.find { it.name == "tph_token" }
+ ?.let {
+ loginAccessToken = it.value
+ }
+ response
+ }
+
}
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 503a699..3bf6819 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -19,4 +19,7 @@
Published posts
Your name?
Awesome Writer
+ Login failed. Try again!
+ Success
+ You successfully logged in!
\ No newline at end of file