forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCalendarViewActivity.kt
More file actions
80 lines (64 loc) · 2.55 KB
/
CalendarViewActivity.kt
File metadata and controls
80 lines (64 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
package com.microsoft.fluentuidemo.demos
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View.TEXT_ALIGNMENT_TEXT_START
import com.microsoft.fluentui.calendar.OnDateSelectedListener
import com.microsoft.fluentui.util.DateStringUtils
import com.microsoft.fluentuidemo.DemoActivity
import com.microsoft.fluentuidemo.databinding.ActivityCalendarViewBinding
import java.time.Duration
import java.time.ZonedDateTime
class CalendarViewActivity : DemoActivity() {
companion object {
private const val DATE = "date"
}
override val contentNeedsScrollableContainer: Boolean
get() = false
private var savedDate: ZonedDateTime? = null
private lateinit var calenderBinding: ActivityCalendarViewBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
calenderBinding = ActivityCalendarViewBinding.inflate(
LayoutInflater.from(container.context),
container,
true
)
calenderBinding.calendarView.onDateSelectedListener = object : OnDateSelectedListener {
override fun onDateSelected(date: ZonedDateTime) {
setExampleDate(date)
}
}
(savedInstanceState?.getSerializable(DATE) as? ZonedDateTime)?.let {
setExampleDate(it)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putSerializable(DATE, savedDate)
}
private fun setExampleDate(date: ZonedDateTime) {
savedDate = date
calenderBinding.exampleDateTitle.text = DateStringUtils.formatDateWithWeekDay(this, date)
calenderBinding.calendarView.setSelectedDateRange(date.toLocalDate(), Duration.ZERO, false)
}
/**
* This function allows the user to leave the calendar focus by pressing the tab key.
* @param keyCode The value in event.getKeyCode().
* @param event The KeyEvent object.
*/
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_TAB && calenderBinding.calendarView.hasFocus()) {
// Find the currently focused view
val focusedView = currentFocus
// Remove focus from the currently focused view
focusedView?.clearFocus()
return true // Consume the event
}
return super.onKeyDown(keyCode, event)
}
}