Skip to content

Commit 6e967d0

Browse files
committed
Deprecate OnMonthChangeListener
1 parent 8ad8dda commit 6e967d0

File tree

7 files changed

+72
-38
lines changed

7 files changed

+72
-38
lines changed

core/src/main/java/com/alamkanak/weekview/Listeners.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ interface OnRangeChangeListener {
7272
fun onRangeChanged(firstVisibleDate: Calendar, lastVisibleDate: Calendar)
7373
}
7474

75+
@Deprecated("Use OnLoadMoreListener instead. This interface will be removed in the future.")
7576
@FunctionalInterface
7677
interface OnMonthChangeListener<T> {
7778

core/src/main/java/com/alamkanak/weekview/WeekView.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ class WeekView<T : Any> @JvmOverloads constructor(
12761276
}
12771277
}
12781278

1279+
@Deprecated("Use onLoadMoreListener instead. This property will be removed in the future.")
12791280
@PublicApi
12801281
var onMonthChangeListener: OnMonthChangeListener<T>?
12811282
get() = (eventsLoader as? LegacyEventsLoader)?.onMonthChangeListener
@@ -1284,6 +1285,7 @@ class WeekView<T : Any> @JvmOverloads constructor(
12841285
eventsLoaderWrapper.onListenerChanged(value)
12851286
}
12861287

1288+
@Deprecated("Use setOnLoadMoreListener instead. This method will be removed in the future.")
12871289
@PublicApi
12881290
fun setOnMonthChangeListener(
12891291
block: (startDate: Calendar, endDate: Calendar) -> List<WeekViewDisplayable<T>>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.alamkanak.weekview.sample;
2+
3+
import android.content.Context;
4+
import android.os.Handler;
5+
import android.os.HandlerThread;
6+
import android.os.Looper;
7+
8+
import com.alamkanak.weekview.WeekViewDisplayable;
9+
import com.alamkanak.weekview.sample.data.EventsDatabase;
10+
import com.alamkanak.weekview.sample.data.model.Event;
11+
12+
import java.util.Calendar;
13+
import java.util.List;
14+
15+
class EventsFetcher {
16+
17+
protected EventsDatabase database;
18+
19+
public EventsFetcher(Context context) {
20+
database = new EventsDatabase(context);
21+
}
22+
23+
void fetch(Calendar startDate, Calendar endDate, Listener listener) {
24+
HandlerThread thread = new HandlerThread("events-fetcher");
25+
thread.start();
26+
27+
Looper looper = thread.getLooper();
28+
Handler handler = new Handler(looper);
29+
30+
handler.post(() -> {
31+
List<WeekViewDisplayable<Event>> events = database.getEventsInRange(startDate, endDate);
32+
listener.onEventsFetched(events);
33+
});
34+
}
35+
36+
interface Listener {
37+
void onEventsFetched(List<WeekViewDisplayable<Event>> events);
38+
}
39+
}

sample/src/main/java/com/alamkanak/weekview/sample/LegacyActivity.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,47 @@
1111
import com.alamkanak.weekview.OnEmptyViewLongClickListener;
1212
import com.alamkanak.weekview.OnEventClickListener;
1313
import com.alamkanak.weekview.OnEventLongClickListener;
14-
import com.alamkanak.weekview.OnMonthChangeListener;
14+
import com.alamkanak.weekview.OnLoadMoreListener;
1515
import com.alamkanak.weekview.WeekView;
16-
import com.alamkanak.weekview.WeekViewDisplayable;
1716
import com.alamkanak.weekview.sample.data.model.Event;
18-
import com.alamkanak.weekview.sample.data.EventsDatabase;
1917
import com.alamkanak.weekview.sample.util.ToolbarUtils;
2018

2119
import org.jetbrains.annotations.NotNull;
2220

2321
import java.text.DateFormat;
2422
import java.text.SimpleDateFormat;
2523
import java.util.Calendar;
26-
import java.util.List;
2724

2825
import static android.widget.Toast.LENGTH_SHORT;
2926

3027
public class LegacyActivity extends AppCompatActivity
31-
implements OnEventClickListener<Event>, OnMonthChangeListener<Event>,
28+
implements OnEventClickListener<Event>, OnLoadMoreListener,
3229
OnEventLongClickListener<Event>, OnEmptyViewLongClickListener {
3330

34-
private EventsDatabase database;
31+
private EventsFetcher eventsFetcher;
32+
protected WeekView<Event> weekView;
3533

3634
@Override
3735
protected void onCreate(Bundle savedInstanceState) {
3836
super.onCreate(savedInstanceState);
3937
setContentView(R.layout.activity_basic);
4038

4139
Toolbar toolbar = findViewById(R.id.toolbar);
42-
WeekView<Event> weekView = findViewById(R.id.weekView);
40+
weekView = findViewById(R.id.weekView);
4341
ToolbarUtils.setupWithWeekView(toolbar, weekView);
4442

45-
database = new EventsDatabase(this);
43+
eventsFetcher = new EventsFetcher(this);
4644

4745
weekView.setOnEventClickListener(this);
48-
weekView.setOnMonthChangeListener(this);
46+
// weekView.setOnMonthChangeListener(this);
47+
weekView.setOnLoadMoreListener(this);
4948
weekView.setOnEventLongClickListener(this);
5049
weekView.setOnEmptyViewLongClickListener(this);
5150
}
5251

53-
@NotNull
5452
@Override
55-
public List<WeekViewDisplayable<Event>> onMonthChange(@NonNull Calendar startDate,
56-
@NonNull Calendar endDate) {
57-
return database.getEventsInRange(startDate, endDate);
53+
public void onLoadMore(@NotNull Calendar startDate, @NotNull Calendar endDate) {
54+
eventsFetcher.fetch(startDate, endDate, weekView::submit);
5855
}
5956

6057
@Override
@@ -73,5 +70,4 @@ public void onEmptyViewLongClick(@NonNull Calendar time) {
7370
String formattedTime = sdf.format(time.getTime());
7471
Toast.makeText(this, "Empty view long pressed: " + formattedTime, LENGTH_SHORT).show();
7572
}
76-
7773
}

sample/src/main/java/com/alamkanak/weekview/sample/LimitedActivity.kt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import androidx.appcompat.app.AppCompatActivity
77
import com.alamkanak.weekview.OnEmptyViewLongClickListener
88
import com.alamkanak.weekview.OnEventClickListener
99
import com.alamkanak.weekview.OnEventLongClickListener
10-
import com.alamkanak.weekview.OnMonthChangeListener
10+
import com.alamkanak.weekview.OnLoadMoreListener
1111
import com.alamkanak.weekview.WeekView
12-
import com.alamkanak.weekview.sample.data.EventsDatabase
1312
import com.alamkanak.weekview.sample.data.model.Event
1413
import com.alamkanak.weekview.sample.util.lazyView
1514
import com.alamkanak.weekview.sample.util.setupWithWeekView
@@ -20,10 +19,10 @@ import java.util.Calendar.DAY_OF_MONTH
2019
import kotlinx.android.synthetic.main.view_toolbar.toolbar
2120

2221
class LimitedActivity : AppCompatActivity(), OnEventClickListener<Event>,
23-
OnMonthChangeListener<Event>, OnEventLongClickListener<Event>, OnEmptyViewLongClickListener {
22+
OnLoadMoreListener, OnEventLongClickListener<Event>, OnEmptyViewLongClickListener {
2423

2524
private val weekView: WeekView<Event> by lazyView(R.id.weekView)
26-
private val database: EventsDatabase by lazy { EventsDatabase(this) }
25+
private val eventsFetcher: EventsFetcher by lazy { EventsFetcher(this) }
2726

2827
override fun onCreate(savedInstanceState: Bundle?) {
2928
super.onCreate(savedInstanceState)
@@ -32,7 +31,7 @@ class LimitedActivity : AppCompatActivity(), OnEventClickListener<Event>,
3231
toolbar.setupWithWeekView(weekView)
3332

3433
weekView.onEventClickListener = this
35-
weekView.onMonthChangeListener = this
34+
weekView.onLoadMoreListener = this
3635
weekView.onEventLongClickListener = this
3736
weekView.onEmptyViewLongClickListener = this
3837

@@ -51,18 +50,17 @@ class LimitedActivity : AppCompatActivity(), OnEventClickListener<Event>,
5150
weekView.maxDate = max
5251
}
5352

54-
override fun onMonthChange(
55-
startDate: Calendar,
56-
endDate: Calendar
57-
) = database.getEventsInRange(startDate, endDate)
53+
override fun onLoadMore(startDate: Calendar, endDate: Calendar) {
54+
eventsFetcher.fetch(startDate, endDate, weekView::submit)
55+
}
5856

59-
override fun onEventClick(event: Event, eventRect: RectF) {
60-
showToast("Clicked ${event.title}")
57+
override fun onEventClick(data: Event, eventRect: RectF) {
58+
showToast("Clicked ${data.title}")
6159
}
6260

63-
override fun onEventLongClick(event: Event, eventRect: RectF) {
64-
showToast("Long-clicked ${event.title}")
65-
Toast.makeText(this, "Long pressed event: " + event.title, Toast.LENGTH_SHORT).show()
61+
override fun onEventLongClick(data: Event, eventRect: RectF) {
62+
showToast("Long-clicked ${data.title}")
63+
Toast.makeText(this, "Long pressed event: " + data.title, Toast.LENGTH_SHORT).show()
6664
}
6765

6866
override fun onEmptyViewLongClick(time: Calendar) {

sample/src/main/java/com/alamkanak/weekview/sample/StaticActivity.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import androidx.appcompat.app.AppCompatActivity
77
import com.alamkanak.weekview.OnEmptyViewLongClickListener
88
import com.alamkanak.weekview.OnEventClickListener
99
import com.alamkanak.weekview.OnEventLongClickListener
10-
import com.alamkanak.weekview.OnMonthChangeListener
10+
import com.alamkanak.weekview.OnLoadMoreListener
1111
import com.alamkanak.weekview.OnRangeChangeListener
1212
import com.alamkanak.weekview.WeekView
13-
import com.alamkanak.weekview.sample.data.EventsDatabase
1413
import com.alamkanak.weekview.sample.data.model.Event
1514
import com.alamkanak.weekview.sample.util.lazyView
1615
import com.alamkanak.weekview.sample.util.setupWithWeekView
@@ -24,11 +23,11 @@ import kotlinx.android.synthetic.main.activity_static.previousWeekButton
2423
import kotlinx.android.synthetic.main.view_toolbar.toolbar
2524

2625
class StaticActivity : AppCompatActivity(), OnEventClickListener<Event>,
27-
OnMonthChangeListener<Event>, OnEventLongClickListener<Event>, OnEmptyViewLongClickListener {
26+
OnLoadMoreListener, OnEventLongClickListener<Event>, OnEmptyViewLongClickListener {
2827

2928
private val weekView: WeekView<Event> by lazyView(R.id.weekView)
29+
private val eventsFetcher: EventsFetcher by lazy { EventsFetcher(this) }
3030

31-
private val database: EventsDatabase by lazy { EventsDatabase(this) }
3231
private val dateFormatter = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM)
3332

3433
override fun onCreate(savedInstanceState: Bundle?) {
@@ -38,7 +37,7 @@ class StaticActivity : AppCompatActivity(), OnEventClickListener<Event>,
3837
toolbar.setupWithWeekView(weekView)
3938

4039
weekView.onEventClickListener = this
41-
weekView.onMonthChangeListener = this
40+
weekView.onLoadMoreListener = this
4241
weekView.onEventLongClickListener = this
4342
weekView.onEmptyViewLongClickListener = this
4443

@@ -62,10 +61,9 @@ class StaticActivity : AppCompatActivity(), OnEventClickListener<Event>,
6261
}
6362
}
6463

65-
override fun onMonthChange(
66-
startDate: Calendar,
67-
endDate: Calendar
68-
) = database.getEventsInRange(startDate, endDate)
64+
override fun onLoadMore(startDate: Calendar, endDate: Calendar) {
65+
eventsFetcher.fetch(startDate, endDate, weekView::submit)
66+
}
6967

7068
override fun onEventClick(data: Event, eventRect: RectF) {
7169
showToast("Clicked ${data.title}")

sample/src/main/java/com/alamkanak/weekview/sample/data/EventsDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.text.DateFormat
1212
import java.text.SimpleDateFormat
1313
import java.util.Calendar
1414

15-
class EventsDatabase(context: Context) {
15+
public class EventsDatabase(context: Context) {
1616

1717
private val color1 = ContextCompat.getColor(context, R.color.event_color_01)
1818
private val color2 = ContextCompat.getColor(context, R.color.event_color_02)

0 commit comments

Comments
 (0)