refactor push to ghost

hauntED
yova 3 months ago
parent 8e9e09ba32
commit 09227c2712

@ -423,7 +423,7 @@ class MainActivity : AppCompatActivity() {
} }
} }
} else } else
shareGhost(msg, ::sendPost) sendPosting(html=msg, author = credManager.username)
}) })
}) })
setNeutralButton("No", { dialog, id -> }) setNeutralButton("No", { dialog, id -> })
@ -488,47 +488,55 @@ class MainActivity : AppCompatActivity() {
fun pushImage(uri: Uri) { fun pushImage(uri: Uri) {
// see https://androidfortechs.blogspot.com/2020/12/how-to-convert-uri-to-file-android-10.html // see https://androidfortechs.blogspot.com/2020/12/how-to-convert-uri-to-file-android-10.html
val file = getFile(applicationContext, uri)!! val file = getFile(applicationContext, uri)!!
shareGhost(file, ::pushImageFile)
}
fun pushImageFile(username: String?, file: File): retrofit2.Response<Any> { val instance = credManager.instance
if (instance == "nowhere") {
checkGhostConnection()
return
}
api = ghostAPI(applicationContext, instance)
var response: retrofit2.Response<imagesObj> = retrofit2.Response.error( var response: retrofit2.Response<imagesObj> = retrofit2.Response.error(
444, 444,
"error".toResponseBody("text/plain".toMediaTypeOrNull()) "error".toResponseBody("text/plain".toMediaTypeOrNull())
) )
try { CoroutineScope(Dispatchers.Main).launch {
response = api.postApi.pushMyImage( withContext(Dispatchers.IO) {
MultipartBody.Part.createFormData( try {
"file", response = api.postApi.pushMyImage(
file.name, MultipartBody.Part.createFormData(
file.asRequestBody("image/jpeg".toMediaTypeOrNull()) "file",
) file.name,
).execute() file.asRequestBody("image/jpeg".toMediaTypeOrNull())
} catch (e:Exception) { )
this.runOnUiThread(Runnable() { ).execute()
Toast.makeText( } catch (e: Exception) {
this@MainActivity, this@MainActivity.runOnUiThread(Runnable() {
"Exception during image upload: $e", Toast.makeText(
Toast.LENGTH_SHORT this@MainActivity,
).show() "Exception during image upload: $e",
}) Toast.LENGTH_SHORT
Log.i(javaClass.simpleName, "Exception during image upload:\n$e") ).show()
} })
Log.i(javaClass.simpleName, "Exception during image upload:\n$e")
}
if (response.isSuccessful) { if (response.isSuccessful) {
val imgUrl = response.body()!!.images[0].url val imgUrl = response.body()!!.images[0].url
Log.d(javaClass.simpleName, "\"${file.name}\" uploaded to \"$imgUrl\"") Log.d(javaClass.simpleName, "\"${file.name}\" uploaded to \"$imgUrl\"")
mdToAppend += "![${file.name}]($imgUrl)\n" mdToAppend += "![${file.name}]($imgUrl)\n"
this.runOnUiThread(Runnable() { this@MainActivity.runOnUiThread(Runnable() {
Toast.makeText( Toast.makeText(
this@MainActivity, this@MainActivity,
"\"${file.name}\" uploaded to \"$imgUrl\"", "\"${file.name}\" uploaded to \"$imgUrl\"",
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
webView.evaluateJavascript("pasteText()") {} webView.evaluateJavascript("pasteText()") {}
}) })
return response as retrofit2.Response<Any> }
} else return response as retrofit2.Response<Any> }
}
} }
fun updatePost(title: String, html: String, author: String, id: String) : retrofit2.Response<Any> { fun updatePost(title: String, html: String, author: String, id: String) : retrofit2.Response<Any> {
@ -579,97 +587,66 @@ class MainActivity : AppCompatActivity() {
) )
} }
} }
fun sendPosting(html: String, author: String): retrofit2.Response<Any> { fun sendPosting(html: String, author: String){
val title = metaData.get("title") ?: getDisplayName(applicationContext, thisFileUri) val title = metaData.get("title") ?: getDisplayName(applicationContext, thisFileUri)
val post = sendPost(title, updated_at = Instant.now().toString(), authors = listOf(author), html, feature_image = metaData.get("feature_image")) val post = sendPost(title, updated_at = Instant.now().toString(), authors = listOf(author), html, feature_image = metaData.get("feature_image"))
val postings = sendPostList(listOf(post)) val postings = sendPostList(listOf(post))
lateinit var response: Response<ResponseBody>
try { CoroutineScope(Dispatchers.Main).launch {
val response = api.postApi.pushPost(postings).execute() withContext(Dispatchers.IO) {
Log.d(javaClass.simpleName, "result: ${response.code()}")
if (response.isSuccessful) {
val resp = JSONObject(response.body()!!.string())
val post = resp.getJSONArray("posts").getJSONObject(0)
val uri = parse(post.getString("url"))
metaData.ID = post.getString("id")
metaData.updatedAt = post.getString("updated_at")
Log.d(javaClass.simpleName, "Uploaded to: $uri\nID: ${metaData.ID}")
metaData.put("url", post.getString("url"))
saveFile()
val intent = Intent(Intent.ACTION_VIEW).setData(uri)
try { try {
startActivity(intent) val instance = credManager.instance
} catch (e: ActivityNotFoundException) { if (instance == "nowhere") {
Toast.makeText(this, "No eligble app installed.", Toast.LENGTH_LONG).show() checkGhostConnection()
Log.i(javaClass.simpleName, e.toString()) return@withContext
} }
} else if (response.code()==403){
this.runOnUiThread({
Toast.makeText(this, "You are not authorized to add posts", Toast.LENGTH_LONG).show()
})
}
return response as Response<Any>
} catch (ex: Exception) {
Log.d(javaClass.simpleName, "Couldn't send posting. Exception: ${ex}")
return retrofit2.Response.error(
444,
"error".toResponseBody("text/plain".toMediaTypeOrNull())
)
}
}
fun sendPost(username: String, text: String): retrofit2.Response<Any> { api = ghostAPI(applicationContext, instance)
return sendPosting(html = text, author = username) response = api.postApi.pushPost(postings).execute()
}
fun shareGhost( Log.d(javaClass.simpleName, "result: ${response.code()}")
text: String, if (response.isSuccessful) {
sendpost: (username: String, text: String) -> retrofit2.Response<Any> val resp = JSONObject(response.body()!!.string())
) { val post = resp.getJSONArray("posts").getJSONObject(0)
shareGhost( val uri = parse(post.getString("url"))
text = text, metaData.ID = post.getString("id")
file = null, metaData.updatedAt = post.getString("updated_at")
sendpost = sendpost as (String?, Any?) -> retrofit2.Response<Any> Log.d(javaClass.simpleName, "Uploaded to: $uri\nID: ${metaData.ID}")
)
}
fun shareGhost( metaData.put("url", post.getString("url"))
file: File,
sendimage: (username: String, file: File) -> retrofit2.Response<Any>
) {
shareGhost(
text = null,
file,
sendpost = sendimage as (String?, Any?) -> retrofit2.Response<Any>
)
}
fun shareGhost( saveFile()
text: String?,
file: File?,
sendpost: (username: String?, content: Any?) -> retrofit2.Response<Any>
) {
val instance = credManager.instance
if (instance == "nowhere") {
checkGhostConnection()
return
}
val username = credManager.username
api = ghostAPI(applicationContext, instance) deleteVisible = true
var result: retrofit2.Response<Any> = invalidateOptionsMenu()
retrofit2.Response.error(444, "error".toResponseBody("text/plain".toMediaTypeOrNull()))
CoroutineScope(Dispatchers.Main).launch { val intent = Intent(Intent.ACTION_VIEW).setData(uri)
withContext(Dispatchers.IO) { try {
if (text !== null) result = sendpost(username, text) startActivity(intent)
if (file !== null) result = sendpost(null, file) } catch (e: ActivityNotFoundException) {
} this@MainActivity.runOnUiThread({
if (text !== null && result.isSuccessful) { Toast.makeText(
deleteVisible = true this@MainActivity,
invalidateOptionsMenu() "No eligble app installed.",
Toast.LENGTH_LONG
)
.show()
Log.i(javaClass.simpleName, e.toString())
})
}
} else if (response.code() == 403) {
this@MainActivity.runOnUiThread({
Toast.makeText(
this@MainActivity,
"You are not authorized to add posts",
Toast.LENGTH_LONG
).show()
})
}
} catch (ex: Exception) {
Log.d(javaClass.simpleName, "Couldn't send posting. Exception: ${ex}")
return@withContext
}
} }
} }
} }

Loading…
Cancel
Save