refactor push to ghost

hauntED
yova 3 months ago
parent 8e9e09ba32
commit 09227c2712

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