From 09227c271240132ba1cd2f8a54940c36dfa5d285 Mon Sep 17 00:00:00 2001 From: yova Date: Mon, 19 Feb 2024 12:52:33 +0100 Subject: [PATCH] refactor push to ghost --- .../java/org/wntr/mdeditor/MainActivity.kt | 211 ++++++++---------- 1 file changed, 94 insertions(+), 117 deletions(-) diff --git a/app/src/main/java/org/wntr/mdeditor/MainActivity.kt b/app/src/main/java/org/wntr/mdeditor/MainActivity.kt index af6c0d8..f66d566 100644 --- a/app/src/main/java/org/wntr/mdeditor/MainActivity.kt +++ b/app/src/main/java/org/wntr/mdeditor/MainActivity.kt @@ -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 { + val instance = credManager.instance + if (instance == "nowhere") { + checkGhostConnection() + return + } + + api = ghostAPI(applicationContext, instance) + var response: retrofit2.Response = 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 - } else return response as retrofit2.Response + 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 { @@ -579,97 +587,66 @@ class MainActivity : AppCompatActivity() { ) } } - fun sendPosting(html: String, author: String): retrofit2.Response { + 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 + 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 - } 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 { - 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 - ) { - shareGhost( - text = text, - file = null, - sendpost = sendpost as (String?, Any?) -> retrofit2.Response - ) - } + 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 - ) { - shareGhost( - text = null, - file, - sendpost = sendimage as (String?, Any?) -> retrofit2.Response - ) - } + metaData.put("url", post.getString("url")) - fun shareGhost( - text: String?, - file: File?, - sendpost: (username: String?, content: Any?) -> retrofit2.Response - ) { - val instance = credManager.instance - if (instance == "nowhere") { - checkGhostConnection() - return - } - val username = credManager.username + saveFile() - api = ghostAPI(applicationContext, instance) - var result: retrofit2.Response = - 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 + } } } }