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="http" />
|
||||||
<data android:scheme="https" />
|
<data android:scheme="https" />
|
||||||
<data android:host="telegra.ph" />
|
<data android:host="telegra.ph" />
|
||||||
|
<data android:host="graph.org" />
|
||||||
|
<data android:host="edit.telegra.ph" />
|
||||||
|
<data android:host="edit.graph.org" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
|
@ -9,6 +9,7 @@ import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import com.afollestad.materialdialogs.MaterialDialog
|
import com.afollestad.materialdialogs.MaterialDialog
|
||||||
import im.delight.android.webview.AdvancedWebView
|
import im.delight.android.webview.AdvancedWebView
|
||||||
|
import java.net.URI
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
private val viewer: Viewer? by lazy {
|
private val viewer: Viewer? by lazy {
|
||||||
|
@ -30,15 +31,20 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
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) {
|
if (success && account != null && account.accessToken != null) {
|
||||||
saveAccessToken(account.accessToken)
|
accessToken = account.accessToken
|
||||||
} else {
|
} else {
|
||||||
showError(error)
|
showError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (intent.action == Intent.ACTION_VIEW && intent.dataString.contains("telegra.ph")) loadPage(intent.dataString.split("/").last())
|
if (intent.action == Intent.ACTION_VIEW) {
|
||||||
else loadEditor()
|
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) {
|
private fun loadEditor(path: String? = null) {
|
||||||
|
@ -51,7 +57,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
viewer?.visibility = View.GONE
|
viewer?.visibility = View.GONE
|
||||||
currentPage = null
|
currentPage = null
|
||||||
// Load
|
// 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) {
|
if (success && page != null) {
|
||||||
isEdit = true
|
isEdit = true
|
||||||
currentPage = page
|
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) {
|
private fun loadPage(path: String) {
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
editorMode = false
|
editorMode = false
|
||||||
|
@ -75,7 +91,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
editor?.visibility = View.GONE
|
editor?.visibility = View.GONE
|
||||||
currentPage = null
|
currentPage = null
|
||||||
// Load
|
// Load
|
||||||
TelegraphApi.getPage(accessToken(), path, true) { success, page, error ->
|
TelegraphApi.getPage(accessToken, path, true) { success, page, error ->
|
||||||
if (success && page != null) showPage(page)
|
if (success && page != null) showPage(page)
|
||||||
else showError(error)
|
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 {
|
runOnUiThread {
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.error)
|
.title(title ?: "")
|
||||||
.content(message ?: getString(R.string.error_desc))
|
.content(message ?: "")
|
||||||
.positiveText(android.R.string.ok)
|
.positiveText(android.R.string.ok)
|
||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
@ -170,15 +189,15 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.name_question)
|
.title(R.string.name_question)
|
||||||
.input(getString(R.string.name_hint), if (isEdit) currentPage?.authorName
|
.input(getString(R.string.name_hint), if (isEdit) currentPage?.authorName
|
||||||
?: authorName() ?: "" else authorName()
|
?: authorName ?: "" else authorName
|
||||||
?: "", { _, name ->
|
?: "", { _, name ->
|
||||||
if (!isEdit) saveAuthorName(name.toString())
|
if (!isEdit) authorName = name.toString()
|
||||||
if (isEdit) TelegraphApi.editPage(accessToken(), currentPage?.path
|
if (isEdit) TelegraphApi.editPage(accessToken, currentPage?.path
|
||||||
?: "", authorName = name.toString(), title = title.toString(), content = json
|
?: "", authorName = name.toString(), title = title.toString(), content = json
|
||||||
?: "", returnContent = true) { success, page, error ->
|
?: "", returnContent = true) { success, page, error ->
|
||||||
if (success && page != null) showPage(page)
|
if (success && page != null) showPage(page)
|
||||||
else showError(error)
|
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 ->
|
?: "", title = title.toString(), authorName = name.toString(), returnContent = true) { success, page, error ->
|
||||||
if (success && page != null) showPage(page)
|
if (success && page != null) showPage(page)
|
||||||
else showError(error)
|
else showError(error)
|
||||||
|
@ -218,7 +237,7 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
R.id.published -> {
|
R.id.published -> {
|
||||||
TelegraphApi.getPageList(accessToken()) { success, pageList, error ->
|
TelegraphApi.getPageList(accessToken) { success, pageList, error ->
|
||||||
if (success && pageList != null && pageList.pages != null) {
|
if (success && pageList != null && pageList.pages != null) {
|
||||||
MaterialDialog.Builder(this)
|
MaterialDialog.Builder(this)
|
||||||
.title(R.string.published)
|
.title(R.string.published)
|
||||||
|
|
|
@ -29,14 +29,14 @@ fun Context.saveBookmarks(bookmarks: List<Pair<String, String>>) {
|
||||||
).apply()
|
).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) {
|
var Context.authorName: String?
|
||||||
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("accessToken", token).apply()
|
get() = PreferenceManager.getDefaultSharedPreferences(this).getString("authorName", null)
|
||||||
}
|
set(value) {
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("authorName", value).apply()
|
||||||
fun Context.authorName(): String? = PreferenceManager.getDefaultSharedPreferences(this).getString("authorName", null)
|
}
|
||||||
|
|
||||||
fun Context.saveAuthorName(name: String) {
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("authorName", name).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.FuelManager
|
||||||
import com.github.kittinunf.fuel.core.Request
|
import com.github.kittinunf.fuel.core.Request
|
||||||
import com.github.kittinunf.fuel.core.Response
|
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.fuel.httpPost
|
||||||
import com.github.kittinunf.result.Result
|
import com.github.kittinunf.result.Result
|
||||||
import org.json.JSONArray
|
import org.json.JSONArray
|
||||||
|
@ -14,10 +16,20 @@ import java.net.HttpCookie
|
||||||
|
|
||||||
object TelegraphApi {
|
object TelegraphApi {
|
||||||
|
|
||||||
// Telegraph
|
private var loginAccessToken: String? = null
|
||||||
|
|
||||||
init {
|
init {
|
||||||
FuelManager.instance.basePath = "https://api.telegra.ph"
|
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) {
|
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) {
|
fun login(authUrl: String, callback: (success: Boolean, accessToken: String?, account: Account?) -> Unit) {
|
||||||
authUrl.httpPost().response { _, response, _ ->
|
loginAccessToken = null
|
||||||
var token: String? = 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun telegraphLoginInterceptor(): (Request, Response) -> Response =
|
||||||
|
{ _, response ->
|
||||||
response.headers["Set-Cookie"]
|
response.headers["Set-Cookie"]
|
||||||
?.flatMap { HttpCookie.parse(it) }
|
?.flatMap { HttpCookie.parse(it) }
|
||||||
?.find { it.name == "tph_token" }
|
?.find { it.name == "tph_token" }
|
||||||
?.let {
|
?.let {
|
||||||
token = it.value
|
loginAccessToken = it.value
|
||||||
}
|
|
||||||
if (token != null) getAccountInfo(accessToken = token!!) { success, account, _ ->
|
|
||||||
if (success) callback(true, token, account)
|
|
||||||
else callback(false, null, null)
|
|
||||||
}
|
|
||||||
else callback(false, null, null)
|
|
||||||
}
|
}
|
||||||
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -19,4 +19,7 @@
|
||||||
<string name="published">Published posts</string>
|
<string name="published">Published posts</string>
|
||||||
<string name="name_question">Your name?</string>
|
<string name="name_question">Your name?</string>
|
||||||
<string name="name_hint">Awesome Writer</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>
|
</resources>
|
Loading…
Reference in New Issue