Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tgs.app.submission1_aplikasifootballleague
- import android.database.sqlite.SQLiteConstraintException
- import android.os.Bundle
- import android.view.Menu
- import android.view.MenuItem
- import androidx.appcompat.app.AppCompatActivity
- import androidx.core.content.ContextCompat
- import com.bumptech.glide.Glide
- import com.google.android.material.snackbar.Snackbar
- import com.google.gson.Gson
- import kotlinx.android.synthetic.main.activity_detail_match.*
- import org.jetbrains.anko.db.classParser
- import org.jetbrains.anko.db.delete
- import org.jetbrains.anko.db.insert
- import org.jetbrains.anko.db.select
- import tgs.app.submission1_aplikasifootballleague.api.ApiRepository
- import tgs.app.submission1_aplikasifootballleague.db.Favorite
- import tgs.app.submission1_aplikasifootballleague.db.database
- import tgs.app.submission1_aplikasifootballleague.model.LastMatch
- import tgs.app.submission1_aplikasifootballleague.model.Team
- import tgs.app.submission1_aplikasifootballleague.presenter.DetailEventPresenter
- import tgs.app.submission1_aplikasifootballleague.presenter.DetailPresenter
- import tgs.app.submission1_aplikasifootballleague.util.invisible
- import tgs.app.submission1_aplikasifootballleague.util.visible
- import tgs.app.submission1_aplikasifootballleague.view.DetailView
- import tgs.app.submission1_aplikasifootballleague.view.LastMatchView
- class DetailFavoriteActivity : AppCompatActivity(), DetailView, LastMatchView {
- companion object {
- const val ID_DETAIL_EVENT = "ID_DETAIL_EVENT"
- }
- private lateinit var lastMatch: LastMatch
- private lateinit var presenter: DetailPresenter
- private lateinit var detailPresenter: DetailEventPresenter
- private lateinit var id: String
- private var isFavorite: Boolean = false
- private var menuItem: Menu? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_detail_favorite)
- id = intent.extras?.getString(ID_DETAIL_EVENT).toString()
- detailPresenter = DetailEventPresenter(
- this, ApiRepository(), Gson()
- )
- detailPresenter.getEventDetail(id)
- presenter = DetailPresenter(
- this,
- ApiRepository(),
- Gson()
- )
- favoriteState()
- supportActionBar?.title = "Detail Match"
- supportActionBar?.setDisplayHomeAsUpEnabled(true)
- }
- private fun favoriteState() {
- database.use {
- val result = select(Favorite.TABLE_FAVORITE)
- .whereArgs("(ID_EVENT = {id})",
- "id" to id)
- val favorite = result.parseList(classParser<Favorite>())
- if (favorite.isNotEmpty()) isFavorite = true
- }
- }
- private fun setupLayout(data: List<LastMatch>) {
- lastMatch = LastMatch(
- data[0].idEvent,
- data[0].dateEvent,
- data[0].strHomeTeam,
- data[0].strAwayTeam,
- data[0].intHomeScore,
- data[0].intAwayScore
- )
- last_event_detail.text = data[0].strEvent
- last_date_event_detail.text = data[0].dateEvent
- last_home_team_detail.text = data[0].strHomeTeam
- last_away_team_detail.text = data[0].strAwayTeam
- if (data[0].intHomeScore == null && data[0].intAwayScore == null){
- last_home_score_detail.text = "-"
- last_away_score_detail.text = "-"
- } else {
- last_home_score_detail.text = data[0].intHomeScore
- last_away_score_detail.text = data[0].intAwayScore
- }
- goal_home.text = data[0].strHomeGoalDetails
- goal_away.text = data[0].strAwayGoalDetails
- goal_keeper_home.text = data[0].strHomeLineupGoalkeeper
- goal_keeper_away.text = data[0].strAwayLineupGoalkeeper
- defense_home.text = data[0].strHomeLineupDefense
- defense_away.text = data[0].strAwayLineupDefense
- midfield_home.text = data[0].strHomeLineupMidfield
- midfield_away.text = data[0].strAwayLineupMidfield
- fordward_home.text = data[0].strHomeLineupForward
- fordward_away.text = data[0].strAwayLineupForward
- substitutes_home.text = data[0].strHomeLineupSubstitutes
- substitutes_away.text = data[0].strAwayLineupSubstitutes
- }
- override fun hideLoading() {
- loading_detail.invisible()
- }
- override fun showLoading() {
- loading_detail.visible()
- }
- override fun showLastMatch(data: List<LastMatch>) {
- setupLayout(data)
- presenter.getTeamLogo(data[0].idHomeTeam!!, data[0].idAwayTeam!!)
- }
- override fun showTeamLogo(logoHome: List<Team>, logoAway: List<Team>) {
- Glide.with(applicationContext)
- .load(logoHome[0].strTeamBadge)
- .into(last_gambar_home_team_detail)
- Glide.with(applicationContext)
- .load(logoAway[0].strTeamBadge)
- .into(last_gambar_away_team_detail)
- }
- override fun onCreateOptionsMenu(menu: Menu?): Boolean {
- menuInflater.inflate(R.menu.detail_menu, menu)
- menuItem = menu
- setFavorite()
- return true
- }
- private fun setFavorite() {
- if (isFavorite){
- menuItem?.getItem(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_added_to_favorites)
- } else {
- menuItem?.getItem(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_add_to_favorites)
- }
- }
- override fun onOptionsItemSelected(item: MenuItem): Boolean {
- return when (item.itemId) {
- android.R.id.home -> {
- finish()
- true
- }
- R.id.add_to_favorite -> {
- if (isFavorite) removeFromFavorite() else addToFavorite()
- isFavorite = !isFavorite
- setFavorite()
- true
- }
- else -> super.onOptionsItemSelected(item)
- }
- }
- private fun addToFavorite(){
- try {
- database.use {
- insert(Favorite.TABLE_FAVORITE,
- Favorite.ID_EVENT to lastMatch.idEvent,
- Favorite.DATE_EVENT to lastMatch.dateEvent,
- Favorite.STR_HOME_TEAM to lastMatch.strHomeTeam,
- Favorite.STR_AWAY_TEAM to lastMatch.strAwayTeam,
- Favorite.INT_HOME_TEAM to lastMatch.intHomeScore,
- Favorite.INT_AWAY_TEAM to lastMatch.intAwayScore)
- }
- Snackbar.make(relativeLayoutUtama, "Added to Favorite", Snackbar.LENGTH_SHORT).show()
- } catch (e: SQLiteConstraintException){
- Snackbar.make(relativeLayoutUtama, "Failed added to Favorite", Snackbar.LENGTH_SHORT).show()
- }
- }
- private fun removeFromFavorite(){
- try {
- database.use {
- delete(Favorite.TABLE_FAVORITE, "(ID_EVENT = {id})",
- "id" to id)
- }
- Snackbar.make(relativeLayoutUtama, "Remove from Favorite", Snackbar.LENGTH_SHORT).show()
- } catch (e: SQLiteConstraintException){
- Snackbar.make(relativeLayoutUtama, "Failed remove from Favorite", Snackbar.LENGTH_SHORT).show()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement