More coroutines.
This commit is contained in:
parent
1254a3566d
commit
f38e823578
|
@ -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<Status> {
|
||||
override fun onFailure(call: Call<Status>, 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<Status>, response: Response<Status>) {
|
||||
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){
|
||||
|
|
|
@ -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<Status> {
|
||||
override fun onFailure(call: Call<Status>, 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<Status>, response: Response<Status>) {
|
||||
//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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -105,15 +105,10 @@ interface PixelfedAPI {
|
|||
@Path("id") statusId: String
|
||||
) : Call<Status>
|
||||
|
||||
@GET("/api/v1/statuses/{id}/favourited_by")
|
||||
fun postLikedBy(
|
||||
@Path("id") statusId: String
|
||||
) : Call<List<Account>>
|
||||
|
||||
//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 <token>"
|
||||
@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>
|
||||
) : Status
|
||||
|
||||
@DELETE("/api/v1/statuses/{id}")
|
||||
suspend fun deleteStatus(
|
||||
|
|
|
@ -45,7 +45,7 @@
|
|||
<string name="request_format_error">Upload error: bad request format</string>
|
||||
<string name="upload_post_failed">Post upload failed</string>
|
||||
<string name="upload_post_success">Post uploaded successfully</string>
|
||||
<string name="upload_post_error">Post upload failed</string>
|
||||
<string name="upload_post_error">Post upload error</string>
|
||||
<string name="description">Description…</string>
|
||||
<string name="post">post</string>
|
||||
<string name="add_photo">Add a photo</string>
|
||||
|
|
Loading…
Reference in New Issue