-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathDayFragment.kt
More file actions
163 lines (140 loc) · 5.91 KB
/
DayFragment.kt
File metadata and controls
163 lines (140 loc) · 5.91 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package org.fossify.calendar.fragments
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import org.fossify.calendar.R
import org.fossify.calendar.activities.MainActivity
import org.fossify.calendar.activities.SimpleActivity
import org.fossify.calendar.adapters.DayEventsAdapter
import org.fossify.calendar.databinding.FragmentDayBinding
import org.fossify.calendar.databinding.TopNavigationBinding
import org.fossify.calendar.extensions.config
import org.fossify.calendar.extensions.eventsHelper
import org.fossify.calendar.extensions.getViewBitmap
import org.fossify.calendar.extensions.printBitmap
import org.fossify.calendar.extensions.seconds
import org.fossify.calendar.helpers.*
import org.fossify.calendar.interfaces.NavigationListener
import org.fossify.calendar.models.Event
import org.fossify.commons.extensions.*
class DayFragment : Fragment() {
var mListener: NavigationListener? = null
private var mTextColor = 0
private var mDayCode = ""
private var lastHash = 0
private lateinit var binding: FragmentDayBinding
private lateinit var topNavigationBinding: TopNavigationBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentDayBinding.inflate(inflater, container, false)
topNavigationBinding = TopNavigationBinding.bind(binding.root)
mDayCode = requireArguments().getString(DAY_CODE)!!
setupButtons()
return binding.root
}
override fun onResume() {
super.onResume()
updateCalendar()
}
private fun setupButtons() {
mTextColor = requireContext().getProperTextColor()
topNavigationBinding.topLeftArrow.apply {
applyColorFilter(mTextColor)
background = null
setOnClickListener {
mListener?.goLeft()
}
val pointerLeft = requireContext().getDrawable(org.fossify.commons.R.drawable.ic_chevron_left_vector)
pointerLeft?.isAutoMirrored = true
setImageDrawable(pointerLeft)
contentDescription = getString(R.string.accessibility_previous_day)
}
topNavigationBinding.topRightArrow.apply {
applyColorFilter(mTextColor)
background = null
setOnClickListener {
mListener?.goRight()
}
val pointerRight = requireContext().getDrawable(org.fossify.commons.R.drawable.ic_chevron_right_vector)
pointerRight?.isAutoMirrored = true
setImageDrawable(pointerRight)
contentDescription = getString(R.string.accessibility_next_day)
}
val day = Formatter.getDayTitle(requireContext(), mDayCode)
topNavigationBinding.topValue.apply {
text = day
contentDescription = text
setOnClickListener {
(activity as MainActivity).showGoToDateDialog()
}
setTextColor(context.getProperTextColor())
}
}
fun updateCalendar() {
val startTS = Formatter.getDayStartTS(mDayCode)
val endTS = Formatter.getDayEndTS(mDayCode)
context?.eventsHelper?.getEvents(startTS, endTS) {
receivedEvents(it)
}
}
private fun receivedEvents(events: List<Event>) {
val newHash = events.hashCode()
if (newHash == lastHash || !isAdded) {
return
}
lastHash = newHash
val replaceDescription = requireContext().config.replaceDescription
val sorted = ArrayList(events.sortedWith(compareBy({ !it.getIsAllDay() }, { it.startTS }, { it.endTS }, { it.title }, {
if (replaceDescription) it.location else it.description
})))
// remove events that do not occur today, but happen to end on midnight
sorted.removeIf {
val startDateTimeSeconds = Formatter.getDateTimeFromTS(it.startTS).seconds()
val endDateTimeSeconds = Formatter.getDateTimeFromTS(it.endTS).seconds()
startDateTimeSeconds != endDateTimeSeconds && Formatter.getDateTimeFromTS(endDateTimeSeconds) == Formatter.getDateTimeFromTS(Formatter.getDayStartTS(mDayCode)).withTimeAtStartOfDay()
}
activity?.runOnUiThread {
updateEvents(sorted)
}
}
private fun updateEvents(events: ArrayList<Event>) {
if (activity == null)
return
DayEventsAdapter(activity as SimpleActivity, events, binding.dayEvents, mDayCode) {
editEvent(it as Event)
}.apply {
binding.dayEvents.adapter = this
}
if (requireContext().areSystemAnimationsEnabled) {
binding.dayEvents.scheduleLayoutAnimation()
}
}
private fun editEvent(event: Event) {
Intent(context, getActivityToOpen(event.isTask())).apply {
putExtra(EVENT_ID, event.id)
putExtra(EVENT_OCCURRENCE_TS, event.startTS)
putExtra(IS_TASK_COMPLETED, event.isTaskCompleted())
startActivity(this)
}
}
fun printCurrentView() {
topNavigationBinding.apply {
topLeftArrow.beGone()
topRightArrow.beGone()
topValue.setTextColor(resources.getColor(org.fossify.commons.R.color.theme_light_text_color))
(binding.dayEvents.adapter as? DayEventsAdapter)?.togglePrintMode()
Handler().postDelayed({
requireContext().printBitmap(binding.dayHolder.getViewBitmap())
Handler().postDelayed({
topLeftArrow.beVisible()
topRightArrow.beVisible()
topValue.setTextColor(requireContext().getProperTextColor())
(binding.dayEvents.adapter as? DayEventsAdapter)?.togglePrintMode()
}, 1000)
}, 1000)
}
}
}