2020-12-28 15:20:31 +01:00
|
|
|
package com.h.pixeldroid.posts
|
2020-10-04 23:27:06 +02:00
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.util.Log
|
|
|
|
import android.view.View
|
2020-12-29 22:14:32 +01:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2020-12-28 15:20:31 +01:00
|
|
|
import com.h.pixeldroid.R
|
2020-12-26 12:10:54 +01:00
|
|
|
import com.h.pixeldroid.utils.api.objects.Report
|
|
|
|
import com.h.pixeldroid.utils.api.objects.Status
|
|
|
|
import com.h.pixeldroid.utils.BaseActivity
|
2020-10-04 23:27:06 +02:00
|
|
|
import kotlinx.android.synthetic.main.activity_report.*
|
|
|
|
import retrofit2.Call
|
|
|
|
import retrofit2.Callback
|
2020-12-29 22:14:32 +01:00
|
|
|
import retrofit2.HttpException
|
2020-10-04 23:27:06 +02:00
|
|
|
import retrofit2.Response
|
2020-12-29 22:14:32 +01:00
|
|
|
import java.io.IOException
|
2020-10-04 23:27:06 +02:00
|
|
|
|
2020-12-11 16:53:12 +01:00
|
|
|
class ReportActivity : BaseActivity() {
|
2020-10-04 23:27:06 +02:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_report)
|
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
|
|
supportActionBar?.setTitle(R.string.report)
|
|
|
|
|
|
|
|
val status = intent.getSerializableExtra(Status.POST_TAG) as Status?
|
|
|
|
|
|
|
|
//get the currently active user
|
|
|
|
val user = db.userDao().getActiveUser()
|
|
|
|
|
|
|
|
|
|
|
|
report_target_textview.text = getString(R.string.report_target).format(status?.account?.acct)
|
|
|
|
|
|
|
|
|
|
|
|
reportButton.setOnClickListener{
|
|
|
|
reportButton.visibility = View.INVISIBLE
|
|
|
|
reportProgressBar.visibility = View.VISIBLE
|
|
|
|
|
|
|
|
textInputLayout.editText?.isEnabled = false
|
|
|
|
|
|
|
|
val accessToken = user?.accessToken.orEmpty()
|
|
|
|
val api = apiHolder.api ?: apiHolder.setDomainToCurrentUser(db)
|
|
|
|
|
2020-12-29 22:14:32 +01:00
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
try {
|
|
|
|
api.report("Bearer $accessToken", status?.account?.id!!, listOf(status), textInputLayout.editText?.text.toString())
|
2020-10-04 23:27:06 +02:00
|
|
|
|
2020-12-29 22:14:32 +01:00
|
|
|
reportStatus(true)
|
|
|
|
} catch (exception: IOException) {
|
|
|
|
reportStatus(false)
|
|
|
|
} catch (exception: HttpException) {
|
|
|
|
reportStatus(false)
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 23:27:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 22:14:32 +01:00
|
|
|
private fun reportStatus(success: Boolean){
|
|
|
|
if(success){
|
|
|
|
reportProgressBar.visibility = View.GONE
|
|
|
|
reportButton.isEnabled = false
|
|
|
|
reportButton.text = getString(R.string.reported)
|
|
|
|
reportButton.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
textInputLayout.error = getString(R.string.report_error)
|
|
|
|
reportButton.visibility = View.VISIBLE
|
|
|
|
textInputLayout.editText?.isEnabled = true
|
|
|
|
reportProgressBar.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
2020-10-04 23:27:06 +02:00
|
|
|
|
|
|
|
override fun onSupportNavigateUp(): Boolean {
|
|
|
|
onBackPressed()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|