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

153 lines
5.4 KiB
Kotlin

package com.simplemobiletools.calendar.pro.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 com.simplemobiletools.calendar.pro.activities.MainActivity
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
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.eventsHelper
import com.simplemobiletools.calendar.pro.extensions.getViewBitmap
import com.simplemobiletools.calendar.pro.extensions.printBitmap
import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.interfaces.NavigationListener
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.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(com.simplemobiletools.commons.R.drawable.ic_chevron_left_vector)
pointerLeft?.isAutoMirrored = true
setImageDrawable(pointerLeft)
}
topNavigationBinding.topRightArrow.apply {
applyColorFilter(mTextColor)
background = null
setOnClickListener {
mListener?.goRight()
}
val pointerRight = requireContext().getDrawable(com.simplemobiletools.commons.R.drawable.ic_chevron_right_vector)
pointerRight?.isAutoMirrored = true
setImageDrawable(pointerRight)
}
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
})))
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(com.simplemobiletools.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)
}
}
}