Permalink
Browse files

Merge pull request #617 from squanchy-dev/freeze-time

Freeze time
  • Loading branch information...
rock3r committed Dec 21, 2018
2 parents c963d86 + 6cfcce9 commit 3db99a10cb37716ab3018b745992ed955ec46f7a
@@ -1,33 +1,48 @@
package net.squanchy.support.debug

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import arrow.core.Option
import com.google.android.material.snackbar.Snackbar
import net.squanchy.R
import net.squanchy.eventdetails.domain.view.ExperienceLevel
import net.squanchy.injection.createApplicationComponent
import net.squanchy.notification.NotificationCreator
import net.squanchy.notification.Notifier
import net.squanchy.notification.scheduleNotificationWork
import net.squanchy.schedule.domain.view.Event
import net.squanchy.schedule.domain.view.Place
import net.squanchy.schedule.domain.view.Track
import net.squanchy.speaker.domain.view.Speaker
import net.squanchy.support.system.FreezableCurrentTime
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime
import org.threeten.bp.ZoneId
import org.threeten.bp.ZonedDateTime
import org.threeten.bp.format.DateTimeFormatter
import java.util.ArrayList
import java.util.Random

class DebugActivity : AppCompatActivity() {

private lateinit var notificationCreator: NotificationCreator

private lateinit var currentTime: FreezableCurrentTime

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_debug)

createApplicationComponent(application).also {
currentTime = it.currentTime() as FreezableCurrentTime
}

val buttonSingleNotification = findViewById<View>(R.id.button_test_single_notification)
buttonSingleNotification.setOnClickListener { testSingleNotification() }

@@ -41,6 +56,10 @@ class DebugActivity : AppCompatActivity() {
buttonResetOnboarding.setOnClickListener { resetOnboarding() }

notificationCreator = NotificationCreator(this)

findViewById<Button>(R.id.freezeTime).setOnClickListener { freezeTime() }
findViewById<Button>(R.id.unfreezeTime).setOnClickListener { unfreezeTime() }
updateFrozenTimeLabel()
}

private fun testSingleNotification() {
@@ -134,4 +153,60 @@ class DebugActivity : AppCompatActivity() {
OnboardingResetter(this).resetOnboarding()
Snackbar.make(findViewById<View>(R.id.debug_root), "It's daaaawnnnn", Snackbar.LENGTH_SHORT).show()
}

private fun freezeTime() {
val now = currentTime.currentDateTime()
pickDate(now)
}

private fun pickDate(now: ZonedDateTime) {
val datePickerDialog = DatePickerDialog(
this,
DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
pickTime(now, LocalDate.of(year, month.to1Based(), dayOfMonth))
},
now.year,
now.monthValue.to0Based(),
now.dayOfMonth
)

datePickerDialog.show()
}

private fun Int.to1Based() = this + 1

private fun Int.to0Based() = this - 1

private fun pickTime(now: ZonedDateTime, frozenDate: LocalDate) {
val timePickerDialog = TimePickerDialog(
this,
TimePickerDialog.OnTimeSetListener { _, hourOfDay, minute ->
freezeAt(frozenDate.atTime(hourOfDay, minute).atZone(ZoneId.systemDefault()))
},
now.hour,
now.minute,
true
)

timePickerDialog.show()
}

private fun freezeAt(frozenDateTime: ZonedDateTime) {
currentTime.freeze(frozenDateTime)
updateFrozenTimeLabel()
}

private fun unfreezeTime() {
currentTime.unfreeze()
updateFrozenTimeLabel()
}

private fun updateFrozenTimeLabel() {
findViewById<TextView>(R.id.timeFrozenIndicator).text = if (currentTime.isTimeFrozen()) {
val time = currentTime.currentDateTime().format(DateTimeFormatter.ofPattern("MM/dd/yyyy h:mm a"))
getString(R.string.debug_time_frozen_at, time)
} else {
getString(R.string.debug_time_not_frozen)
}
}
}
@@ -1,13 +1,14 @@
package net.squanchy.support.injection

import android.app.Application
import dagger.Module
import dagger.Provides
import net.squanchy.support.system.CurrentTime
import net.squanchy.support.system.DebugCurrentTime
import net.squanchy.support.system.FreezableCurrentTime

@Module
class CurrentTimeModule {

@Provides
internal fun currentTime(): CurrentTime = DebugCurrentTime()
internal fun currentTime(application: Application): CurrentTime = FreezableCurrentTime(application)
}

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,33 @@
package net.squanchy.support.system

import android.content.Context
import android.preference.PreferenceManager
import androidx.core.content.edit
import org.threeten.bp.ZonedDateTime
import org.threeten.bp.format.DateTimeFormatter

class FreezableCurrentTime(context: Context) : CurrentTime {

private val preferences = PreferenceManager.getDefaultSharedPreferences(context)

override fun currentDateTime(): ZonedDateTime {
return preferences.getString(KEY_FROZEN_TIME, null)?.let {
ZonedDateTime.parse(it, DATE_TIME_FORMATTER)
} ?: ZonedDateTime.now()
}

fun freeze(frozenDateTime: ZonedDateTime) {
preferences.edit { putString(KEY_FROZEN_TIME, frozenDateTime.format(DATE_TIME_FORMATTER)) }
}

fun unfreeze() {
preferences.edit { remove(KEY_FROZEN_TIME) }
}

fun isTimeFrozen() = preferences.contains(KEY_FROZEN_TIME)

companion object {
private const val KEY_FROZEN_TIME = "FROZEN_TIME"
private val DATE_TIME_FORMATTER = DateTimeFormatter.ISO_DATE_TIME
}
}
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/debug_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -84,6 +85,46 @@

</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/debug_header_margin_top"
android:layout_marginBottom="@dimen/debug_header_margin_bottom"
android:text="@string/debug_header_time"
android:textAppearance="@style/TextAppearance.Squanchy.EventDetails.Body.Label"
android:textSize="@dimen/debug_header_text" />

<TextView
android:id="@+id/timeFrozenIndicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Time frozen at 15/02/18 15:30 AM"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/freezeTime"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.Squanchy.Card.Title"
android:text="@string/debug_freeze_time" />

<Button
android:id="@+id/unfreezeTime"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@style/TextAppearance.Squanchy.Card.Title"
android:text="@string/debug_unfreeze_time" />

</LinearLayout>

</LinearLayout>

</ScrollView>
@@ -8,5 +8,10 @@
<string name="debug_test_notifications">Test Notifications</string>
<string name="debug_reset_onboarding">Reset onboarding</string>
<string name="debug_reset_onboarding_button">DEWWEEETTT</string>
<string name="debug_header_time">Time</string>
<string name="debug_freeze_time">Freeze time</string>
<string name="debug_unfreeze_time">Unfreeze time</string>
<string name="debug_time_frozen_at">Time frozen at %1$s</string>
<string name="debug_time_not_frozen">Time not frozen</string>

</resources>
@@ -21,6 +21,7 @@ import net.squanchy.service.repository.TracksRepository
import net.squanchy.service.repository.injection.RepositoryModule
import net.squanchy.support.checksum.ChecksumModule
import net.squanchy.support.injection.CurrentTimeModule
import net.squanchy.support.system.CurrentTime

fun createApplicationComponent(application: Application): ApplicationComponent {
return DaggerApplicationComponent.builder()
@@ -65,6 +66,8 @@ interface ApplicationComponent {

fun algoliaSearchEngine(): AlgoliaSearchEngine

fun currentTime(): CurrentTime

@Component.Builder
interface Builder {
@BindsInstance
@@ -6,7 +6,6 @@ import net.squanchy.injection.ActivityLifecycle
import net.squanchy.injection.ApplicationComponent
import net.squanchy.injection.applicationComponent
import net.squanchy.notification.NotificationModule.Companion.UPCOMING_EVENT_THRESHOLD
import net.squanchy.support.injection.CurrentTimeModule
import net.squanchy.support.system.CurrentTime
import org.threeten.bp.Duration
import javax.inject.Named
@@ -17,7 +16,7 @@ internal fun notificationComponent(context: Context): NotificationComponent =
.build()

@ActivityLifecycle
@Component(modules = [NotificationModule::class, CurrentTimeModule::class], dependencies = [ApplicationComponent::class])
@Component(modules = [NotificationModule::class], dependencies = [ApplicationComponent::class])
internal interface NotificationComponent {

fun notificationCreator(): NotificationCreator
@@ -3,16 +3,15 @@ package net.squanchy.schedule
import androidx.appcompat.app.AppCompatActivity
import dagger.Component
import net.squanchy.analytics.Analytics
import net.squanchy.injection.BaseActivityComponentBuilder
import net.squanchy.injection.ActivityLifecycle
import net.squanchy.injection.ApplicationComponent
import net.squanchy.injection.BaseActivityComponentBuilder
import net.squanchy.injection.applicationComponent
import net.squanchy.navigation.NavigationModule
import net.squanchy.navigation.Navigator
import net.squanchy.remoteconfig.FeatureFlags
import net.squanchy.schedule.tracksfilter.TracksFilter
import net.squanchy.service.repository.TracksRepository
import net.squanchy.support.injection.CurrentTimeModule
import net.squanchy.support.system.CurrentTime

internal fun scheduleComponent(activity: AppCompatActivity): ScheduleComponent =
@@ -23,7 +22,7 @@ internal fun scheduleComponent(activity: AppCompatActivity): ScheduleComponent =

@ActivityLifecycle
@Component(
modules = [ScheduleModule::class, NavigationModule::class, CurrentTimeModule::class],
modules = [ScheduleModule::class, NavigationModule::class],
dependencies = [ApplicationComponent::class]
)
internal interface ScheduleComponent {

0 comments on commit 3db99a1

Please sign in to comment.