Skip to content

Commit

Permalink
WikiGames: the final result screen (#5255)
Browse files Browse the repository at this point in the history
* WikiGames: the final result screen

* Change radius

* Stats view

* Hygiene a bit

* Add functions for stat cards

* Update recycler view item

* Nest it

* Change coordinateLayout to Framelayout to avoid jumpy view when
scrolling

* Add icons

* update the logic of showing card colors

* Add article share function

* Some tweak

* Update color

* Update card color to non-themed group

* Update design

* Tweak design

* Top margin

* Update design

* Add actions for the menu items

* Add actions for the menu items

* Code review comment

* !?

---------

Co-authored-by: Dmitry Brant <[email protected]>
  • Loading branch information
cooltey and dbrant authored Jan 31, 2025
1 parent 307c7ff commit b8582ba
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 372 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.text.format.DateFormat
import android.view.Menu
Expand All @@ -29,8 +30,10 @@ import org.wikipedia.databinding.ActivityOnThisDayGameBinding
import org.wikipedia.dataclient.WikiSite
import org.wikipedia.feed.onthisday.OnThisDay
import org.wikipedia.util.DimenUtil
import org.wikipedia.util.FeedbackUtil
import org.wikipedia.util.Resource
import org.wikipedia.util.ResourceUtil
import org.wikipedia.util.UriUtil
import org.wikipedia.views.ViewUtil
import java.time.LocalDate
import java.time.MonthDay
Expand Down Expand Up @@ -140,8 +143,18 @@ class OnThisDayGameActivity : BaseActivity() {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
super.onOptionsItemSelected(item)
return when (item.itemId) {
R.id.menu_help -> {
// TODO
R.id.menu_learn_more -> {
UriUtil.visitInExternalBrowser(this, Uri.parse(getString(R.string.on_this_day_game_wiki_url)))
true
}
R.id.menu_notifications -> {
// TODO: implement this
true
}
R.id.menu_report_feature -> {
FeedbackUtil.composeEmail(this,
subject = getString(R.string.on_this_day_game_report_email_subject),
body = getString(R.string.on_this_day_game_report_email_body))
true
}
else -> super.onOptionsItemSelected(item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import org.wikipedia.Constants
import org.wikipedia.Constants.InvokeSource
import org.wikipedia.R
Expand All @@ -20,22 +22,22 @@ import org.wikipedia.databinding.FragmentOnThisDayGameFinalBinding
import org.wikipedia.databinding.ItemOnThisDayGameTopicBinding
import org.wikipedia.dataclient.page.PageSummary
import org.wikipedia.history.HistoryEntry
import org.wikipedia.page.PageActivity
import org.wikipedia.readinglist.LongPressMenu
import org.wikipedia.readinglist.ReadingListBehaviorsUtil
import org.wikipedia.readinglist.database.ReadingListPage
import org.wikipedia.util.Resource
import org.wikipedia.util.ResourceUtil
import org.wikipedia.util.ShareUtil
import org.wikipedia.util.StringUtil
import org.wikipedia.views.MarginItemDecoration
import org.wikipedia.views.ViewUtil
import java.time.Duration
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.temporal.WeekFields
import java.util.Locale

class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
class OnThisDayGameFinalFragment : Fragment() {
private var _binding: FragmentOnThisDayGameFinalBinding? = null
val binding get() = _binding!!

Expand All @@ -48,10 +50,7 @@ class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
_binding = FragmentOnThisDayGameFinalBinding.inflate(inflater, container, false)

binding.shareButton.setOnClickListener {
requireActivity().supportFragmentManager.beginTransaction()
.add(R.id.fragmentContainerFinish, OnThisDayGameShareFragment.newInstance(viewModel.invokeSource))
.addToBackStack(null)
.commit()
// TODO: implement this (please remove the share fragment)
}

viewModel.gameState.observe(viewLifecycleOwner) {
Expand Down Expand Up @@ -82,12 +81,6 @@ class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
super.onDestroyView()
}

override fun onDayClick(date: LocalDate) {
viewModel.resetCurrentDayState()
requireActivity().finish()
startActivity(OnThisDayGameActivity.newIntent(requireContext(), viewModel.invokeSource, viewModel.wikiSite, date))
}

private fun updateOnLoading() {
binding.errorView.isVisible = false
binding.scrollContainer.isVisible = false
Expand All @@ -105,32 +98,26 @@ class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
binding.progressBar.isVisible = false
binding.errorView.isVisible = false
binding.scrollContainer.isVisible = true

val totalCorrect = gameState.answerState.count { it }
// binding.resultText.text = getString(R.string.on_this_day_game_result,
// totalCorrect,
// gameState.totalQuestions,
// getString(when (totalCorrect) {
// 0 -> R.string.on_this_day_game_encourage0
// 1 -> R.string.on_this_day_game_encourage1
// 2 -> R.string.on_this_day_game_encourage2
// else -> R.string.on_this_day_game_encourage3
// }))

val streak = calculateStreak(gameState.answerStateHistory)
// binding.streakText.text = StringUtil.fromHtml(resources.getQuantityString(R.plurals.on_this_day_game_streak, streak, streak))

binding.resultArticlesList.layoutManager = LinearLayoutManager(requireContext())
binding.resultArticlesList.isNestedScrollingEnabled = false
binding.resultArticlesList.adapter = RecyclerViewAdapter(viewModel.getArticlesMentioned())
binding.resultText.text = getString(R.string.on_this_day_game_result, totalCorrect, gameState.totalQuestions)

var displayStartDate = getStartOfWeekDate(OnThisDayGameViewModel.gameStartDate)
while (displayStartDate.isBefore(OnThisDayGameViewModel.gameEndDate)) {
val weekView = WeeklyActivityView(requireContext())
weekView.callback = this
binding.weeksContainer.addView(weekView)
weekView.setWeekStats(displayStartDate, gameState)
displayStartDate = displayStartDate.plusDays(7)
val cardContainerColor = when (totalCorrect) {
0, 2 -> R.color.yellow500
3, 4 -> R.color.orange500
else -> R.color.green600
}
binding.resultCardContainer.setBackgroundColor(ContextCompat.getColor(requireContext(), cardContainerColor))
binding.statsGamePlayed.text = String.format(calculateTotalGamesPlayed(gameState.answerStateHistory).toString())
binding.statsAverageScore.text = String.format(Locale.getDefault(), "%.1f", calculateAverageScore(gameState.answerStateHistory))
binding.statsCurrentStreak.text = String.format(calculateStreak(gameState.answerStateHistory).toString())

binding.resultArticlesList.layoutManager = StaggeredGridLayoutManager(2, GridLayoutManager.VERTICAL)
binding.resultArticlesList.addItemDecoration(MarginItemDecoration(requireActivity(),
R.dimen.view_list_card_margin_horizontal, R.dimen.view_list_card_margin_vertical,
R.dimen.view_list_card_margin_horizontal, R.dimen.view_list_card_margin_vertical))
binding.resultArticlesList.isNestedScrollingEnabled = false
binding.resultArticlesList.adapter = RecyclerViewAdapter(viewModel.getArticlesMentioned())
}

private inner class RecyclerViewAdapter(val pages: List<PageSummary>) : RecyclerView.Adapter<RecyclerViewItemHolder>() {
Expand Down Expand Up @@ -161,6 +148,9 @@ class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
binding.listItemTitle.text = StringUtil.fromHtml(page.displayTitle)
binding.listItemDescription.text = StringUtil.fromHtml(page.description)
binding.listItemDescription.isVisible = !page.description.isNullOrEmpty()
binding.listItemShare.setOnClickListener {
ShareUtil.shareText(requireActivity(), page.getPageTitle(WikipediaApp.instance.wikiSite))
}
binding.listItemBookmark.isVisible = true
val isSaved = viewModel.savedPages.contains(page)
binding.listItemBookmark.setOnClickListener {
Expand All @@ -176,8 +166,7 @@ class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
}

override fun onClick(v: View) {
val entry = HistoryEntry(page.getPageTitle(WikipediaApp.instance.wikiSite), HistoryEntry.SOURCE_PLACES)
startActivity(PageActivity.newIntentForNewTab(requireActivity(), entry, entry.title))
// TODO: implement this (bottom sheet).
}
}

Expand Down Expand Up @@ -215,10 +204,16 @@ class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
}
}

fun getStartOfWeekDate(date: LocalDate, locale: Locale = Locale.getDefault()): LocalDate {
val firstDayOfWeek = WeekFields.of(locale).firstDayOfWeek
val daysToSubtract = ((7 + (date.dayOfWeek.value - firstDayOfWeek.value)) % 7).toLong()
return date.minusDays(daysToSubtract)
fun calculateTotalGamesPlayed(answerStateHistory: Map<Int, Map<Int, Map<Int, List<Boolean>>>?>): Int {
var total = 0
answerStateHistory.forEach { year ->
year.value?.forEach { month ->
month.value.forEach { day ->
total++
}
}
}
return total
}

fun calculateStreak(answerStateHistory: Map<Int, Map<Int, Map<Int, List<Boolean>>>?>): Int {
Expand All @@ -231,6 +226,20 @@ class OnThisDayGameFinalFragment : Fragment(), WeeklyActivityView.Callback {
return streak
}

fun calculateAverageScore(answerStateHistory: Map<Int, Map<Int, Map<Int, List<Boolean>>>?>): Float {
var total = 0
var count = 0
answerStateHistory.forEach { year ->
year.value?.forEach { month ->
month.value.forEach { day ->
total += day.value.count { it == true }
count++
}
}
}
return total.toFloat() / count
}

fun timeUntilNextDay(): Duration {
val now = LocalDateTime.now()
val startOfNextDay = LocalDateTime.of(now.toLocalDate().plusDays(1), LocalTime.MIDNIGHT)
Expand Down

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_info_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-6h2v6zM13,9h-2L11,7h2v2z"/>

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/outline_notification_add_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M16,14v3H8v-7c0,-2.21 1.79,-4 4,-4c0.85,0 1.64,0.26 2.28,0.72l1.43,-1.43c-0.64,-0.51 -1.39,-0.88 -2.21,-1.09V3.5C13.5,2.67 12.83,2 12,2s-1.5,0.67 -1.5,1.5v0.7C7.91,4.86 6,7.21 6,10v7H4v2h16v-2h-2v-3H16zM12,22c1.1,0 2,-0.9 2,-2h-4C10,21.1 10.9,22 12,22zM24,8h-3V5h-2v3h-3v2h3v3h2v-3h3V8z"/>

</vector>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_on_this_day_game.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
Expand Down Expand Up @@ -309,4 +309,4 @@
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</FrameLayout>
Loading

0 comments on commit b8582ba

Please sign in to comment.