Created
November 6, 2019 20:26
-
-
Save guilhermecarvalhocarneiro/477b4ec31cda9850283f618150a5682d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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