diff --git a/app/src/main/java/com/h/pixeldroid/postCreation/PostCreationActivity.kt b/app/src/main/java/com/h/pixeldroid/postCreation/PostCreationActivity.kt index f8b48b84..0a0aae17 100644 --- a/app/src/main/java/com/h/pixeldroid/postCreation/PostCreationActivity.kt +++ b/app/src/main/java/com/h/pixeldroid/postCreation/PostCreationActivity.kt @@ -12,6 +12,7 @@ import android.view.ViewGroup import android.widget.Button import android.widget.Toast import androidx.core.net.toFile +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import com.bumptech.glide.Glide @@ -34,7 +35,9 @@ import kotlinx.android.synthetic.main.image_album_creation.view.* import okhttp3.MultipartBody import retrofit2.Call import retrofit2.Callback +import retrofit2.HttpException import retrofit2.Response +import java.io.IOException private const val TAG = "Post Creation Activity" private const val MORE_PICTURES_REQUEST_CODE = 0xffff @@ -213,33 +216,30 @@ class PostCreationActivity : BaseActivity() { private fun post() { val description = new_post_description_input_field.text.toString() enableButton(false) - pixelfedAPI.postStatus( - authorization = "Bearer $accessToken", - statusText = description, - media_ids = muListOfIds.toList() - ).enqueue(object: Callback { - override fun onFailure(call: Call, t: Throwable) { + lifecycleScope.launchWhenCreated { + try { + pixelfedAPI.postStatus( + authorization = "Bearer $accessToken", + statusText = description, + media_ids = muListOfIds.toList() + ) + Toast.makeText(applicationContext,getString(R.string.upload_post_success), + Toast.LENGTH_SHORT).show() + val intent = Intent(this@PostCreationActivity, MainActivity::class.java) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + startActivity(intent) + } catch (exception: IOException) { + Toast.makeText(applicationContext,getString(R.string.upload_post_error), + Toast.LENGTH_SHORT).show() + Log.e(TAG, exception.toString()) enableButton(true) + } catch (exception: HttpException) { Toast.makeText(applicationContext,getString(R.string.upload_post_failed), Toast.LENGTH_SHORT).show() - Log.e(TAG, t.message + call.request()) + Log.e(TAG, exception.response().toString() + exception.message().toString()) + enableButton(true) } - - override fun onResponse(call: Call, response: Response) { - if (response.code() == 200) { - Toast.makeText(applicationContext,getString(R.string.upload_post_success), - Toast.LENGTH_SHORT).show() - val intent = Intent(this@PostCreationActivity, MainActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - startActivity(intent) - } else { - Toast.makeText(applicationContext,getString(R.string.upload_post_error), - Toast.LENGTH_SHORT).show() - Log.e(TAG, call.request().toString() + response.raw().toString()) - enableButton(true) - } - } - }) + } } private fun enableButton(enable: Boolean = true){ diff --git a/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt b/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt index 8544e7f4..f020a3c2 100644 --- a/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt +++ b/app/src/main/java/com/h/pixeldroid/posts/StatusViewHolder.kt @@ -245,7 +245,7 @@ class StatusViewHolder(val view: View) : RecyclerView.ViewHolder(view) { holder, api, credential, status?.reblogged ?: false ) - activateCommenter(holder, api, credential) + activateCommenter(holder, api, credential, lifecycleScope) showComments(holder, api, credential) @@ -602,7 +602,8 @@ class StatusViewHolder(val view: View) : RecyclerView.ViewHolder(view) { private fun activateCommenter( holder: StatusViewHolder, api: PixelfedAPI, - credential: String + credential: String, + lifecycleScope: LifecycleCoroutineScope ) { //Toggle comment button toggleCommentInput(holder) @@ -618,9 +619,10 @@ class StatusViewHolder(val view: View) : RecyclerView.ViewHolder(view) { Toast.LENGTH_SHORT ).show() } else { - //Post the comment - postComment(holder, api, credential) + lifecycleScope.launchWhenCreated { + postComment(holder, api, credential) + } } } } @@ -696,7 +698,7 @@ class StatusViewHolder(val view: View) : RecyclerView.ViewHolder(view) { } } - private fun postComment( + private suspend fun postComment( holder : StatusViewHolder, api: PixelfedAPI, credential: String, @@ -704,39 +706,34 @@ class StatusViewHolder(val view: View) : RecyclerView.ViewHolder(view) { val textIn = holder.comment.text val nonNullText = textIn.toString() status?.id?.let { - api.postStatus(credential, nonNullText, it).enqueue(object : - Callback { - override fun onFailure(call: Call, t: Throwable) { - Log.e("COMMENT ERROR", t.toString()) - Toast.makeText( - holder.view.context, holder.view.context.getString(R.string.comment_error), - Toast.LENGTH_SHORT - ).show() - } + try { + val response = api.postStatus(credential, nonNullText, it) + holder.commentIn.visibility = View.GONE - override fun onResponse(call: Call, response: Response) { - //Check that the received response code is valid - if (response.code() == 200) { - val resp = response.body()!! - holder.commentIn.visibility = View.GONE + //Add the comment to the comment section + addComment( + holder.view.context, holder.commentCont, response.account!!.username!!, + response.content!! + ) - //Add the comment to the comment section - addComment( - holder.view.context, holder.commentCont, resp.account!!.username!!, - resp.content!! - ) - - Toast.makeText( - holder.view.context, - holder.view.context.getString(R.string.comment_posted).format(textIn), - Toast.LENGTH_SHORT - ).show() - Log.e("COMMENT SUCCESS", "posted: $textIn") - } else { - Log.e("ERROR_CODE", response.code().toString()) - } - } - }) + Toast.makeText( + holder.view.context, + holder.view.context.getString(R.string.comment_posted).format(textIn), + Toast.LENGTH_SHORT + ).show() + } catch (exception: IOException) { + Log.e("COMMENT ERROR", exception.toString()) + Toast.makeText( + holder.view.context, holder.view.context.getString(R.string.comment_error), + Toast.LENGTH_SHORT + ).show() + } catch (exception: HttpException) { + Toast.makeText( + holder.view.context, holder.view.context.getString(R.string.comment_error), + Toast.LENGTH_SHORT + ).show() + Log.e("ERROR_CODE", exception.code().toString()) + } } } diff --git a/app/src/main/java/com/h/pixeldroid/utils/api/PixelfedAPI.kt b/app/src/main/java/com/h/pixeldroid/utils/api/PixelfedAPI.kt index 1333aa00..99487f9e 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/api/PixelfedAPI.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/api/PixelfedAPI.kt @@ -105,15 +105,10 @@ interface PixelfedAPI { @Path("id") statusId: String ) : Call - @GET("/api/v1/statuses/{id}/favourited_by") - fun postLikedBy( - @Path("id") statusId: String - ) : Call> - //Used in our case to post a comment or a status @FormUrlEncoded @POST("/api/v1/statuses") - fun postStatus( + suspend fun postStatus( //The authorization header needs to be of the form "Bearer " @Header("Authorization") authorization: String, @Field("status") statusText : String, @@ -128,7 +123,7 @@ interface PixelfedAPI { @Field("visibility") visibility : String = "public", @Field("scheduled_at") scheduled_at : String? = null, @Field("language") language : String? = null - ) : Call + ) : Status @DELETE("/api/v1/statuses/{id}") suspend fun deleteStatus( diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 23096e5e..17b4133d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -45,7 +45,7 @@ Upload error: bad request format Post upload failed Post uploaded successfully - Post upload failed + Post upload error Description… post Add a photo