mirror of https://github.com/jlelse/teleposter
Enable login
This commit is contained in:
parent
d82166935f
commit
3e704f3080
|
@ -32,6 +32,9 @@
|
|||
<data android:scheme="http" />
|
||||
<data android:scheme="https" />
|
||||
<data android:host="telegra.ph" />
|
||||
<data android:host="graph.org" />
|
||||
<data android:host="edit.telegra.ph" />
|
||||
<data android:host="edit.graph.org" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -29,14 +29,14 @@ fun Context.saveBookmarks(bookmarks: List<Pair<String, String>>) {
|
|||
).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()
|
||||
}
|
||||
var Context.authorName: String?
|
||||
get() = PreferenceManager.getDefaultSharedPreferences(this).getString("authorName", null)
|
||||
set(value) {
|
||||
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("authorName", value).apply()
|
||||
}
|
|
@ -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<Pair<String, Any?>>, handler: (Request, Response, Result<Json, FuelError>) -> 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
|
||||
}
|
||||
|
||||
}
|
|
@ -19,4 +19,7 @@
|
|||
<string name="published">Published posts</string>
|
||||
<string name="name_question">Your name?</string>
|
||||
<string name="name_hint">Awesome Writer</string>
|
||||
<string name="login_failed">Login failed. Try again!</string>
|
||||
<string name="success">Success</string>
|
||||
<string name="login_success">You successfully logged in!</string>
|
||||
</resources>
|
Loading…
Reference in New Issue