Skip to content

Commit 650bd27

Browse files
Pass LocalDate directly to the game view model
1 parent 0bfc853 commit 650bd27

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

app/src/main/java/org/wikipedia/games/onthisday/OnThisDayGameActivity.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ import org.wikipedia.util.DimenUtil
3232
import org.wikipedia.util.FeedbackUtil
3333
import org.wikipedia.util.Resource
3434
import org.wikipedia.util.UriUtil
35-
import java.time.LocalDate
36-
import java.time.ZoneOffset
37-
import java.time.format.DateTimeFormatter
3835

3936
class OnThisDayGameActivity : BaseActivity(), BaseActivity.Callback {
4037

@@ -219,18 +216,10 @@ class OnThisDayGameActivity : BaseActivity(), BaseActivity.Callback {
219216

220217
companion object {
221218
fun newIntent(context: Context, invokeSource: Constants.InvokeSource, wikiSite: WikiSite): Intent {
222-
val intent = Intent(context, OnThisDayGameActivity::class.java)
219+
return Intent(context, OnThisDayGameActivity::class.java)
223220
.putExtra(Constants.ARG_WIKISITE, wikiSite)
224221
.putExtra(Constants.INTENT_EXTRA_INVOKE_SOURCE, invokeSource)
225-
if (Prefs.lastOtdGameDateOverride.isNotEmpty()) {
226-
val date = try {
227-
LocalDate.parse(Prefs.lastOtdGameDateOverride, DateTimeFormatter.ISO_LOCAL_DATE)
228-
} catch (_: Exception) {
229-
LocalDate.now()
230-
}
231-
intent.putExtra(OnThisDayGameViewModel.EXTRA_DATE, date.atStartOfDay().toInstant(ZoneOffset.UTC).epochSecond)
232-
}
233-
return intent
222+
.putExtra(OnThisDayGameViewModel.EXTRA_DATE, Prefs.lastOtdGameDateOverride)
234223
}
235224
}
236225
}

app/src/main/java/org/wikipedia/games/onthisday/OnThisDayGameViewModel.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ import org.wikipedia.settings.Prefs
2727
import org.wikipedia.util.ReleaseUtil
2828
import org.wikipedia.util.Resource
2929
import org.wikipedia.util.log.L
30-
import java.time.Instant
3130
import java.time.LocalDate
32-
import java.time.ZoneOffset
3331
import java.time.format.DateTimeFormatter
3432
import kotlin.math.abs
3533
import kotlin.math.max
@@ -49,7 +47,7 @@ class OnThisDayGameViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
4947
private lateinit var currentState: GameState
5048

5149
private val overrideDate = savedStateHandle.contains(EXTRA_DATE)
52-
var currentDate = if (overrideDate) LocalDate.ofInstant(Instant.ofEpochSecond(savedStateHandle.get<Long>(EXTRA_DATE)!!), ZoneOffset.UTC) else LocalDate.now()
50+
var currentDate: LocalDate = savedStateHandle.get<LocalDate>(EXTRA_DATE) ?: LocalDate.now()
5351
val currentMonth get() = currentDate.monthValue
5452
val currentDay get() = currentDate.dayOfMonth
5553
var isArchiveGame = false

app/src/main/java/org/wikipedia/settings/Prefs.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import org.wikipedia.util.StringUtil
3131
import org.wikipedia.watchlist.WatchlistFilterTypes
3232
import org.wikipedia.yearinreview.YearInReviewModel
3333
import org.wikipedia.yearinreview.YearInReviewSurveyState
34+
import java.time.LocalDate
35+
import java.time.format.DateTimeParseException
3436
import java.util.Date
3537

3638
/** Shared preferences utility for convenient POJO access. */
@@ -743,9 +745,13 @@ object Prefs {
743745
get() = JsonUtil.decodeFromString<List<DonationResult>>(PrefsIoUtil.getString(R.string.preference_key_donation_results, null)).orEmpty()
744746
set(value) = PrefsIoUtil.setString(R.string.preference_key_donation_results, JsonUtil.encodeToString(value))
745747

746-
var lastOtdGameDateOverride
747-
get() = PrefsIoUtil.getString(R.string.preference_key_otd_game_date_override, null).orEmpty()
748-
set(value) = PrefsIoUtil.setString(R.string.preference_key_otd_game_date_override, value)
748+
var lastOtdGameDateOverride: LocalDate?
749+
get() = try {
750+
LocalDate.parse(PrefsIoUtil.getString(R.string.preference_key_otd_game_date_override, null).orEmpty())
751+
} catch (_: DateTimeParseException) {
752+
null
753+
}
754+
set(value) = PrefsIoUtil.setString(R.string.preference_key_otd_game_date_override, value?.toString())
749755

750756
var otdGameState
751757
get() = PrefsIoUtil.getString(R.string.preference_key_otd_game_state, null).orEmpty()

0 commit comments

Comments
 (0)