Page Menu
Home
GitPull.it
Search
Configure Global Search
Log In
Files
F15187244
AlertsFragment.kt
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
AlertsFragment.kt
View Options
/*
BusTO - Fragments components
Copyright (C) 2026 Fabio Mazza
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package it.reyboz.bustorino.fragments
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.google.transit.realtime.GtfsRealtime
import it.reyboz.bustorino.R
import it.reyboz.bustorino.backend.Palina
import it.reyboz.bustorino.viewmodels.ServiceAlertsViewModel
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import java.util.TimeZone
/**
* A simple [Fragment] subclass.
* Use the [AlertsFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class AlertsFragment : ScreenBaseFragment() {
private val alertsViewModel: ServiceAlertsViewModel by activityViewModels()
private lateinit var textView: TextView
private lateinit var statusTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
//param1 = it.getString(ARG_PARAM1)
//param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val root = inflater.inflate(R.layout.fragment_alerts, container, false)
textView = root.findViewById(R.id.simpleTextView)
statusTextView = root.findViewById(R.id.statusTextView)
alertsViewModel.allAlertsLiveData.observe(viewLifecycleOwner, { alerts ->
if(alerts==null){
return@observe
}
if(alerts.isEmpty()){
textView.text = "No Alerts to show"
} else {
val sb = StringBuilder()
val unixTimestamp = (System.currentTimeMillis() / 1000)
for (x in alerts) {
sb.append(x.longPrint())
sb.append("----- Alert active: ").append(x.isActive(unixTimestamp)).append("\n\n")
}
textView.text = sb.toString()
}
})
alertsViewModel.getDownloadStatusLiveData(requireContext()).observe(viewLifecycleOwner, { workinfos ->
val sb = StringBuilder()
var c = 1
if(workinfos!=null && workinfos.isNotEmpty()){
for (worki in workinfos){
sb.append("$c - state: ${worki.state}, attempt ${worki.runAttemptCount}").append("\n")
c++
}
}
statusTextView.text = sb.toString()
})
//alertsViewModel.setStopFilter(Palina("472")
/*alertsViewModel.alertsForStop.observe(viewLifecycleOwner){
Log.d(DEBUG_TAG, "Got ${it.size} alerts")
it?.let {
showAlerts(it)
}
}
*/
/*
alertsViewModel.alertsByRouteLiveData.observe(viewLifecycleOwner) { map ->
Log.d(DEBUG_TAG, "Alerts for routes: ${map.keys}")
val keys = map.keys
if(keys.isNotEmpty()){
val sb = StringBuilder()
for (key in keys.sorted()) {
sb.append(" ======== Route: $key =======").append("\n")
sb.append(makeAlertListText(map[key]!!)).append("\n")
Log.d(DEBUG_TAG, "Route: $key len: ${map[key]!!.size}")
}
textView.text = sb.toString()
}
}
*/
return root
}
override fun getBaseViewForSnackBar(): View? {
TODO("Not yet implemented")
}
private fun makeAlertListText(alerts: List<GtfsRealtime.Alert>) : String{
val sb = StringBuilder()
for (al in alerts) {
sb.append("=========== Alert ===========\n")
sb.append("Title:\n")
for (t in al.headerText.translationList) {
sb.append(t.language).append(": ").append(t.text).append("\n")
}
sb.append("Description:\n")
val transl = al.descriptionText.translationList
for (t in transl) {
sb.append(t.language).append(": ").append(t.text).append("\n")
}
val infE = al.informedEntityList
sb.append("--- Active periods count: ${al.activePeriodCount}\n")
val timeActive = al.getActivePeriod(0)
sb.append("Start: ").append(getTimeStampToString(timeActive.start)).append(" ")
sb.append("End: ").append(getTimeStampToString(timeActive.end)).append("\n")
sb.append("--- Cause:\n")
sb.append(al.cause.name).append("\n")
sb.append("--- Informed entities:\n")
for (e in infE) {
if(e.hasTrip()){
sb.append("Trip: ${e.trip.tripId} for route ${e.trip.routeId}, ")
} else{
sb.append("No Trip, ")
}
sb.append("Stop: ${e.stopId}, Route: ${e.routeId}\n")
}
sb.append("\n")
}
return sb.toString()
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment AlertsFragment.
*/
@JvmStatic
fun newInstance() =
AlertsFragment().apply {
arguments = Bundle().apply {
//putString(ARG_PARAM1, param1)
//putString(ARG_PARAM2, param2)
}
}
fun getTimeStampToString(timestamp: Long): String? {
val date = Date(timestamp*1000)
val sdf= SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
sdf.timeZone = TimeZone.getTimeZone("Europe/Rome")
return sdf.format(date)
}
private const val DEBUG_TAG = "BusTO-AlertsFragment"
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 19, 09:45 (13 m, 30 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2265675
Default Alt Text
AlertsFragment.kt (6 KB)
Attached To
Mode
R4 Libre BusTO
Attached
Detach File
Event Timeline