Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermecarvalhocarneiro/477b4ec31cda9850283f618150a5682d to your computer and use it in GitHub Desktop.
Save guilhermecarvalhocarneiro/477b4ec31cda9850283f618150a5682d to your computer and use it in GitHub Desktop.
class FeedAdapter(private val context: Context,
private val posts: List<FeedDomain>):
RecyclerView.Adapter<FeedAdapter.ViewHolder>(), CoroutineScope {
private lateinit var job: Job
private var downloadJob: Job? = null
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){}
fun curtir(id: Int){
// TODO Verificar como enviar a mensagem para a Main Principal
if (Http.checkConnection(context)) {
downloadJob = launch {
withContext(Dispatchers.IO) {
// Invocando o método/função a ser executada de forma assincrona
FeedHttp.rocketit(id)
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(
R.layout.cardview_feed, parent, false)
// Criando o Job Paralelo da Coroutine
job = Job()
return ViewHolder(view)
}
override fun getItemCount(): Int = posts.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val feed = posts[position]
with(holder.itemView){
this.postText.text = feed.mensagem
this.postProfileName.text = feed.usuarioData?.nome
// Tratando o clique do botão RocKIT
this.postBtnRockIt.setOnClickListener {
curtir(feed.id)
}
this.postBtnComment.setOnClickListener {
FeedSingleton.set(feed)
val intent = Intent(context, PostCommentActivity::class.java)
this.context.startActivity(intent)
}
if (feed.fotos.size > 0){
Glide.with(context).load(feed.fotos[0]).into(this.postImage)
}else{
Glide.with(context).load(WebImages.random()).into(this.postImage)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment