From 19b91aa432c98126105fc9574d0043a643ab0d08 Mon Sep 17 00:00:00 2001 From: Lucas Lima Date: Fri, 12 Jun 2020 08:33:16 -0300 Subject: [PATCH] Add retry button --- .../antimine/history/views/HistoryAdapter.kt | 69 +++++++++++++++---- .../history/views/HistoryViewHolder.kt | 4 +- app/src/main/res/drawable/play.xml | 9 +++ app/src/main/res/drawable/retry.xml | 9 +++ app/src/main/res/layout/view_history_item.xml | 15 ++-- common/src/main/res/values-night/colors.xml | 4 ++ common/src/main/res/values/colors.xml | 4 ++ 7 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 app/src/main/res/drawable/play.xml create mode 100644 app/src/main/res/drawable/retry.xml diff --git a/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryAdapter.kt b/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryAdapter.kt index 8327a5d0..0df02cb0 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryAdapter.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryAdapter.kt @@ -1,15 +1,18 @@ package dev.lucasnlm.antimine.history.views import android.content.Intent +import android.graphics.PorterDuff import android.net.Uri import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup +import androidx.core.content.ContextCompat import androidx.recyclerview.widget.RecyclerView import dev.lucasnlm.antimine.DeepLink import dev.lucasnlm.antimine.R import dev.lucasnlm.antimine.common.level.database.models.Save +import dev.lucasnlm.antimine.common.level.database.models.SaveStatus import dev.lucasnlm.antimine.common.level.models.Difficulty -import java.text.DateFormat class HistoryAdapter( private val saveHistory: List @@ -34,20 +37,60 @@ class HistoryAdapter( } ) - holder.minefieldSize.text = String.format("%d x %d", minefield.width, minefield.height) - holder.minesCount.text = holder.itemView.context.getString(R.string.mines_remaining, minefield.mines) - holder.date.text = DateFormat.getDateInstance().format(startDate) + val context = holder.itemView.context + holder.flag.setColorFilter( + when (status) { + SaveStatus.VICTORY -> ContextCompat.getColor(context, R.color.victory) + SaveStatus.ON_GOING -> ContextCompat.getColor(context, R.color.ongoing) + SaveStatus.DEFEAT -> ContextCompat.getColor(context, R.color.lose) + }, PorterDuff.Mode.SRC_IN + ) - holder.itemView.setOnClickListener { - val intent = Intent(Intent.ACTION_VIEW).apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - data = Uri.Builder() - .scheme(DeepLink.SCHEME) - .authority(DeepLink.RETRY_HOST_AUTHORITY) - .appendPath(uid.toString()) - .build() + holder.minefieldSize.text = String.format("%d x %d", minefield.width, minefield.height) + holder.minesCount.text = context.getString(R.string.mines_remaining, minefield.mines) + + if (status != SaveStatus.VICTORY) { + holder.replay.setImageResource(R.drawable.replay) + holder.replay.setOnClickListener { + val intent = Intent(Intent.ACTION_VIEW).apply { + flags = Intent.FLAG_ACTIVITY_CLEAR_TASK + data = Uri.Builder() + .scheme(DeepLink.SCHEME) + .authority(DeepLink.RETRY_HOST_AUTHORITY) + .appendPath(uid.toString()) + .build() + } + it.context.startActivity(intent) } - it.context.startActivity(intent) + } else { + holder.replay.setImageResource(R.drawable.play) + holder.replay.setOnClickListener { replayGame(it, uid) } } + + holder.itemView.setOnClickListener { loadGame(it, uid) } + } + + private fun replayGame(view: View, uid: Int) { + val intent = Intent(Intent.ACTION_VIEW).apply { + flags = Intent.FLAG_ACTIVITY_CLEAR_TASK + data = Uri.Builder() + .scheme(DeepLink.SCHEME) + .authority(DeepLink.RETRY_HOST_AUTHORITY) + .appendPath(uid.toString()) + .build() + } + view.context.startActivity(intent) + } + + private fun loadGame(view: View, uid: Int) { + val intent = Intent(Intent.ACTION_VIEW).apply { + flags = Intent.FLAG_ACTIVITY_CLEAR_TASK + data = Uri.Builder() + .scheme(DeepLink.SCHEME) + .authority(DeepLink.LOAD_GAME_AUTHORITY) + .appendPath(uid.toString()) + .build() + } + view.context.startActivity(intent) } } diff --git a/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryViewHolder.kt b/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryViewHolder.kt index 432140b0..7b2f5a09 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryViewHolder.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/history/views/HistoryViewHolder.kt @@ -2,12 +2,14 @@ package dev.lucasnlm.antimine.history.views import android.view.View import android.widget.TextView +import androidx.appcompat.widget.AppCompatImageView import androidx.recyclerview.widget.RecyclerView import dev.lucasnlm.antimine.R class HistoryViewHolder(view: View) : RecyclerView.ViewHolder(view) { + val flag: AppCompatImageView = view.findViewById(R.id.badge) val difficulty: TextView = view.findViewById(R.id.difficulty) val minefieldSize: TextView = view.findViewById(R.id.minefieldSize) val minesCount: TextView = view.findViewById(R.id.minesCount) - val date: TextView = view.findViewById(R.id.date) + val replay: AppCompatImageView = view.findViewById(R.id.replay) } diff --git a/app/src/main/res/drawable/play.xml b/app/src/main/res/drawable/play.xml new file mode 100644 index 00000000..76d03c34 --- /dev/null +++ b/app/src/main/res/drawable/play.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/retry.xml b/app/src/main/res/drawable/retry.xml new file mode 100644 index 00000000..c9701566 --- /dev/null +++ b/app/src/main/res/drawable/retry.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/view_history_item.xml b/app/src/main/res/layout/view_history_item.xml index a6f9f8db..af573c49 100644 --- a/app/src/main/res/layout/view_history_item.xml +++ b/app/src/main/res/layout/view_history_item.xml @@ -66,13 +66,16 @@ app:layout_constraintTop_toBottomOf="@id/difficulty" tools:text="9 mines" /> - + app:layout_constraintTop_toTopOf="parent" + app:tint="@color/text_color" + app:srcCompat="@drawable/retry" /> \ No newline at end of file diff --git a/common/src/main/res/values-night/colors.xml b/common/src/main/res/values-night/colors.xml index 2f783b7b..5028a198 100644 --- a/common/src/main/res/values-night/colors.xml +++ b/common/src/main/res/values-night/colors.xml @@ -6,6 +6,10 @@ #212121 #212121 + #FFFFFF + #616161 + #616161 + #d5d2cc #d5d2cc #d5d2cc diff --git a/common/src/main/res/values/colors.xml b/common/src/main/res/values/colors.xml index 5ce292fb..dba49adb 100644 --- a/common/src/main/res/values/colors.xml +++ b/common/src/main/res/values/colors.xml @@ -6,6 +6,10 @@ #FFFFFF #9E9E9E + #4CAF50 + #455a64 + #F44336 + #527F8D #2B8D43 #E65100