Skip to content

Commit

Permalink
Tweak EventsDatabase in sample app to prevent UI inconsistencies
Browse files Browse the repository at this point in the history
Even when called with startDate and endDate, EventsDatabase would sometimes create events outside this range.
  • Loading branch information
thellmund committed Nov 1, 2019
1 parent 7770a59 commit 6561371
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.alamkanak.weekview.WeekView
import com.alamkanak.weekview.sample.apiclient.ApiEvent
import com.alamkanak.weekview.sample.data.EventsApi
import com.alamkanak.weekview.sample.data.FakeEventsApi
import com.alamkanak.weekview.sample.data.model.ApiEvent
import com.alamkanak.weekview.sample.util.lazyView
import com.alamkanak.weekview.sample.util.setupWithWeekView
import com.alamkanak.weekview.sample.util.showToast
Expand Down Expand Up @@ -45,7 +44,7 @@ class AsyncActivity : AppCompatActivity() {
private val weekView: WeekView<ApiEvent> by lazyView(R.id.weekView)

private val viewModel: AsyncViewModel by lazy {
AsyncViewModel(FakeEventsApi(this))
AsyncViewModel(EventsApi(this))
}

@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.alamkanak.weekview.WeekView
import com.alamkanak.weekview.WeekViewDisplayable
import com.alamkanak.weekview.sample.apiclient.Event
import com.alamkanak.weekview.sample.data.model.Event
import com.alamkanak.weekview.sample.data.EventsDatabase
import com.alamkanak.weekview.sample.data.FakeEventsDatabase
import com.alamkanak.weekview.sample.util.lazyView
import com.alamkanak.weekview.sample.util.observe
import com.alamkanak.weekview.sample.util.setupWithWeekView
Expand Down Expand Up @@ -36,7 +35,7 @@ class BasicActivity : AppCompatActivity() {
private val weekView: WeekView<Event> by lazyView(R.id.weekView)

private val viewModel: ViewModel by lazy {
ViewModel(FakeEventsDatabase(this))
ViewModel(EventsDatabase(this))
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package com.alamkanak.weekview.sample
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.alamkanak.weekview.WeekView
import com.alamkanak.weekview.sample.apiclient.Event
import com.alamkanak.weekview.sample.data.model.Event
import com.alamkanak.weekview.sample.data.EventsDatabase
import com.alamkanak.weekview.sample.data.FakeEventsDatabase
import com.alamkanak.weekview.sample.util.lazyView
import com.alamkanak.weekview.sample.util.setupWithWeekView
import com.alamkanak.weekview.sample.util.showToast
Expand All @@ -16,7 +15,7 @@ import java.util.Calendar
class CustomFontActivity : AppCompatActivity() {

private val weekView: WeekView<Event> by lazyView(R.id.weekView)
private val database: EventsDatabase by lazy { FakeEventsDatabase(this) }
private val database: EventsDatabase by lazy { EventsDatabase(this) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import com.alamkanak.weekview.OnMonthChangeListener;
import com.alamkanak.weekview.WeekView;
import com.alamkanak.weekview.WeekViewDisplayable;
import com.alamkanak.weekview.sample.apiclient.Event;
import com.alamkanak.weekview.sample.data.model.Event;
import com.alamkanak.weekview.sample.data.EventsDatabase;
import com.alamkanak.weekview.sample.data.FakeEventsDatabase;
import com.alamkanak.weekview.sample.util.ToolbarUtils;

import org.jetbrains.annotations.NotNull;
Expand All @@ -43,7 +42,7 @@ protected void onCreate(Bundle savedInstanceState) {
WeekView<Event> weekView = findViewById(R.id.weekView);
ToolbarUtils.setupWithWeekView(toolbar, weekView);

database = new FakeEventsDatabase(this);
database = new EventsDatabase(this);

weekView.setOnEventClickListener(this);
weekView.setOnMonthChangeListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import com.alamkanak.weekview.OnEventClickListener
import com.alamkanak.weekview.OnEventLongClickListener
import com.alamkanak.weekview.OnMonthChangeListener
import com.alamkanak.weekview.WeekView
import com.alamkanak.weekview.sample.apiclient.Event
import com.alamkanak.weekview.sample.data.model.Event
import com.alamkanak.weekview.sample.data.EventsDatabase
import com.alamkanak.weekview.sample.data.FakeEventsDatabase
import com.alamkanak.weekview.sample.util.lazyView
import com.alamkanak.weekview.sample.util.setupWithWeekView
import com.alamkanak.weekview.sample.util.showToast
Expand All @@ -24,7 +23,7 @@ class LimitedActivity : AppCompatActivity(), OnEventClickListener<Event>,
OnMonthChangeListener<Event>, OnEventLongClickListener<Event>, OnEmptyViewLongClickListener {

private val weekView: WeekView<Event> by lazyView(R.id.weekView)
private val database: EventsDatabase by lazy { FakeEventsDatabase(this) }
private val database: EventsDatabase by lazy { EventsDatabase(this) }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import com.alamkanak.weekview.OnEventLongClickListener
import com.alamkanak.weekview.OnMonthChangeListener
import com.alamkanak.weekview.OnRangeChangeListener
import com.alamkanak.weekview.WeekView
import com.alamkanak.weekview.sample.apiclient.Event
import com.alamkanak.weekview.sample.data.model.Event
import com.alamkanak.weekview.sample.data.EventsDatabase
import com.alamkanak.weekview.sample.data.FakeEventsDatabase
import com.alamkanak.weekview.sample.util.lazyView
import com.alamkanak.weekview.sample.util.setupWithWeekView
import com.alamkanak.weekview.sample.util.showToast
Expand All @@ -30,7 +29,7 @@ class StaticActivity : AppCompatActivity(), OnEventClickListener<Event>,

private val weekView: WeekView<Event> by lazyView(R.id.weekView)

private val database: EventsDatabase by lazy { FakeEventsDatabase(this) }
private val database: EventsDatabase by lazy { EventsDatabase(this) }
private val dateFormatter = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM)

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.alamkanak.weekview.WeekView
import com.alamkanak.weekview.sample.apiclient.Event
import com.alamkanak.weekview.sample.data.model.Event
import com.alamkanak.weekview.sample.data.EventsDatabase
import com.alamkanak.weekview.sample.data.FakeEventsDatabase
import com.alamkanak.weekview.sample.util.lazyView
import com.alamkanak.weekview.sample.util.setupWithWeekView
import com.google.android.material.appbar.MaterialToolbar
Expand All @@ -32,9 +31,7 @@ class WeekFragment : Fragment(R.layout.fragment_week) {
private val toolbar: MaterialToolbar by lazyView(R.id.toolbar)
private val weekView: WeekView<Event> by lazyView(R.id.weekView)

private val database: EventsDatabase by lazy {
FakeEventsDatabase(requireContext())
}
private val database: EventsDatabase by lazy { EventsDatabase(requireContext()) }

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
toolbar.setupWithWeekView(weekView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ package com.alamkanak.weekview.sample.data
import android.app.Activity
import android.content.Context
import android.os.AsyncTask
import com.alamkanak.weekview.sample.apiclient.ApiEvent
import com.alamkanak.weekview.sample.data.model.ApiEvent
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.lang.Thread.sleep

interface EventsApi {
fun fetchEvents(onSuccess: (List<ApiEvent>) -> Unit)
}

class FakeEventsApi(
class EventsApi(
private val context: Context
) : EventsApi {
) {

private val responseType = object : TypeToken<List<ApiEvent>>() {}.type

override fun fetchEvents(
fun fetchEvents(
onSuccess: (List<ApiEvent>) -> Unit
) {
AsyncTask.execute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import android.content.Context
import androidx.core.content.ContextCompat
import com.alamkanak.weekview.WeekViewDisplayable
import com.alamkanak.weekview.sample.R
import com.alamkanak.weekview.sample.apiclient.Event
import com.alamkanak.weekview.sample.data.model.Event
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar

interface EventsDatabase {
fun getEventsInRange(startDate: Calendar, endDate: Calendar): List<WeekViewDisplayable<Event>>
}
class EventsDatabase(context: Context) {

class FakeEventsDatabase(private val context: Context) : EventsDatabase {
private val color1 = ContextCompat.getColor(context, R.color.event_color_01)
private val color2 = ContextCompat.getColor(context, R.color.event_color_02)
private val color3 = ContextCompat.getColor(context, R.color.event_color_03)
private val color4 = ContextCompat.getColor(context, R.color.event_color_04)

override fun getEventsInRange(
fun getEventsInRange(
startDate: Calendar,
endDate: Calendar
): List<WeekViewDisplayable<Event>> {
Expand All @@ -24,15 +25,11 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {

val idOffset = newYear + 10L * newMonth

val color1 = ContextCompat.getColor(context, R.color.event_color_01)
val color2 = ContextCompat.getColor(context, R.color.event_color_02)
val color3 = ContextCompat.getColor(context, R.color.event_color_03)
val color4 = ContextCompat.getColor(context, R.color.event_color_04)

val events = mutableListOf<WeekViewDisplayable<Event>>()
var event: Event

var startTime = Calendar.getInstance()
startTime.set(Calendar.DAY_OF_MONTH, 28)
startTime.set(Calendar.HOUR_OF_DAY, 16)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.MONTH, newMonth)
Expand All @@ -46,6 +43,7 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {

// Add multi-day event
startTime = Calendar.getInstance()
startTime.set(Calendar.DAY_OF_MONTH, 28)
startTime.set(Calendar.HOUR_OF_DAY, 20)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.MONTH, newMonth)
Expand All @@ -60,6 +58,7 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
events.add(event)

startTime = Calendar.getInstance()
startTime.set(Calendar.DAY_OF_MONTH, 28)
startTime.set(Calendar.HOUR_OF_DAY, 9)
startTime.set(Calendar.MINUTE, 30)
startTime.set(Calendar.MONTH, newMonth)
Expand All @@ -73,6 +72,7 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
events.add(event)

startTime = Calendar.getInstance()
startTime.set(Calendar.DAY_OF_MONTH, 28)
startTime.set(Calendar.HOUR_OF_DAY, 10)
startTime.set(Calendar.MINUTE, 30)
startTime.set(Calendar.MONTH, newMonth)
Expand All @@ -85,6 +85,7 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
events.add(event)

startTime = Calendar.getInstance()
startTime.set(Calendar.DAY_OF_MONTH, 28)
startTime.set(Calendar.HOUR_OF_DAY, 12)
startTime.set(Calendar.MINUTE, 30)
startTime.set(Calendar.MONTH, newMonth)
Expand All @@ -97,7 +98,7 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {
events.add(event)

startTime = Calendar.getInstance()
startTime.set(Calendar.DATE, 17)
startTime.set(Calendar.DAY_OF_MONTH, 17)
startTime.set(Calendar.HOUR_OF_DAY, 11)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.MONTH, newMonth)
Expand Down Expand Up @@ -148,6 +149,7 @@ class FakeEventsDatabase(private val context: Context) : EventsDatabase {

// All-day event
startTime = Calendar.getInstance()
startTime.set(Calendar.DAY_OF_MONTH, 28)
startTime.set(Calendar.HOUR_OF_DAY, 0)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.MONTH, newMonth)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.alamkanak.weekview.sample.apiclient
package com.alamkanak.weekview.sample.data.model

import android.graphics.Color
import com.alamkanak.weekview.WeekViewDisplayable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.alamkanak.weekview.sample.apiclient
package com.alamkanak.weekview.sample.data.model

import android.graphics.Color
import com.alamkanak.weekview.WeekViewDisplayable
Expand Down

0 comments on commit 6561371

Please sign in to comment.