Simple-Calendar/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/DayFragment.kt

153 lines
5.4 KiB
Kotlin
Raw Normal View History

2018-11-09 17:12:02 +01:00
package com.simplemobiletools.calendar.pro.fragments
2016-09-16 23:01:46 +02:00
2016-09-18 10:45:21 +02:00
import android.content.Intent
2016-09-16 23:01:46 +02:00
import android.os.Bundle
import android.os.Handler
2016-11-23 20:48:32 +01:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.simplemobiletools.calendar.pro.activities.MainActivity
2018-11-09 17:12:02 +01:00
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.adapters.DayEventsAdapter
import com.simplemobiletools.calendar.pro.databinding.FragmentDayBinding
import com.simplemobiletools.calendar.pro.databinding.TopNavigationBinding
2018-11-09 17:12:02 +01:00
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.eventsHelper
2020-10-25 20:05:23 +01:00
import com.simplemobiletools.calendar.pro.extensions.getViewBitmap
import com.simplemobiletools.calendar.pro.extensions.printBitmap
import com.simplemobiletools.calendar.pro.helpers.*
2018-11-09 17:12:02 +01:00
import com.simplemobiletools.calendar.pro.interfaces.NavigationListener
import com.simplemobiletools.calendar.pro.models.Event
2022-04-06 23:49:04 +02:00
import com.simplemobiletools.commons.extensions.*
2016-09-16 23:01:46 +02:00
2018-01-22 17:36:11 +01:00
class DayFragment : Fragment() {
2017-10-21 19:17:34 +02:00
var mListener: NavigationListener? = null
private var mTextColor = 0
private var mDayCode = ""
2017-10-21 18:14:01 +02:00
private var lastHash = 0
private lateinit var binding: FragmentDayBinding
private lateinit var topNavigationBinding: TopNavigationBinding
2016-09-17 23:31:07 +02:00
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentDayBinding.inflate(inflater, container, false)
topNavigationBinding = TopNavigationBinding.bind(binding.root)
2021-11-21 15:10:20 +01:00
mDayCode = requireArguments().getString(DAY_CODE)!!
2016-09-17 23:31:07 +02:00
setupButtons()
return binding.root
2016-09-17 23:31:07 +02:00
}
override fun onResume() {
super.onResume()
updateCalendar()
2016-09-17 23:31:07 +02:00
}
2016-09-17 23:31:07 +02:00
private fun setupButtons() {
2022-04-06 23:49:04 +02:00
mTextColor = requireContext().getProperTextColor()
2016-09-17 23:31:07 +02:00
topNavigationBinding.topLeftArrow.apply {
2018-01-19 23:48:07 +01:00
applyColorFilter(mTextColor)
background = null
setOnClickListener {
2016-09-17 23:31:07 +02:00
mListener?.goLeft()
}
2019-02-07 18:42:50 +01:00
2023-09-04 12:13:10 +02:00
val pointerLeft = requireContext().getDrawable(com.simplemobiletools.commons.R.drawable.ic_chevron_left_vector)
2019-02-07 18:42:50 +01:00
pointerLeft?.isAutoMirrored = true
setImageDrawable(pointerLeft)
2018-01-19 23:48:07 +01:00
}
2016-09-17 23:31:07 +02:00
topNavigationBinding.topRightArrow.apply {
2018-01-19 23:48:07 +01:00
applyColorFilter(mTextColor)
background = null
setOnClickListener {
2016-09-17 23:31:07 +02:00
mListener?.goRight()
}
2019-02-07 18:42:50 +01:00
2023-09-04 12:13:10 +02:00
val pointerRight = requireContext().getDrawable(com.simplemobiletools.commons.R.drawable.ic_chevron_right_vector)
2019-02-07 18:42:50 +01:00
pointerRight?.isAutoMirrored = true
setImageDrawable(pointerRight)
2016-09-17 23:31:07 +02:00
}
2018-01-19 23:48:07 +01:00
2021-11-21 15:10:20 +01:00
val day = Formatter.getDayTitle(requireContext(), mDayCode)
topNavigationBinding.topValue.apply {
2018-01-19 23:48:07 +01:00
text = day
contentDescription = text
setOnClickListener {
(activity as MainActivity).showGoToDateDialog()
}
2022-04-06 23:49:04 +02:00
setTextColor(context.getProperTextColor())
2018-01-19 23:48:07 +01:00
}
2016-09-16 23:01:46 +02:00
}
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) {
2017-10-21 18:14:01 +02:00
return
}
lastHash = newHash
2021-11-21 15:10:20 +01:00
val replaceDescription = requireContext().config.replaceDescription
2020-10-25 20:05:23 +01:00
val sorted = ArrayList(events.sortedWith(compareBy({ !it.getIsAllDay() }, { it.startTS }, { it.endTS }, { it.title }, {
if (replaceDescription) it.location else it.description
})))
activity?.runOnUiThread {
updateEvents(sorted)
}
}
private fun updateEvents(events: ArrayList<Event>) {
2016-09-19 22:18:19 +02:00
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)
2016-09-18 15:13:03 +02:00
}
}
2020-10-25 20:05:23 +01:00
fun printCurrentView() {
topNavigationBinding.apply {
topLeftArrow.beGone()
topRightArrow.beGone()
2023-09-04 12:13:10 +02:00
topValue.setTextColor(resources.getColor(com.simplemobiletools.commons.R.color.theme_light_text_color))
(binding.dayEvents.adapter as? DayEventsAdapter)?.togglePrintMode()
2020-10-25 20:05:23 +01:00
Handler().postDelayed({
requireContext().printBitmap(binding.dayHolder.getViewBitmap())
2020-10-25 20:05:23 +01:00
Handler().postDelayed({
topLeftArrow.beVisible()
topRightArrow.beVisible()
topValue.setTextColor(requireContext().getProperTextColor())
(binding.dayEvents.adapter as? DayEventsAdapter)?.togglePrintMode()
}, 1000)
}, 1000)
2020-10-25 20:05:23 +01:00
}
}
2016-09-16 23:01:46 +02:00
}