Bimba.git

ref: fce99bb7fe691fea197b4cac75ee9cbc1eb836a4

app/src/main/java/xyz/apiote/bimba/czwek/search/ui/LineGraphFragment.kt


  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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.search.ui

import android.content.Context
import android.content.Intent
import android.content.res.TypedArray
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import dev.bandb.graphview.AbstractGraphAdapter
import dev.bandb.graphview.layouts.layered.SugiyamaArrowEdgeDecoration
import dev.bandb.graphview.layouts.layered.SugiyamaConfiguration
import dev.bandb.graphview.layouts.layered.SugiyamaLayoutManager
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.databinding.FragmentLineGraphBinding
import xyz.apiote.bimba.czwek.departures.DeparturesActivity
import xyz.apiote.bimba.czwek.repo.LineGraph
import xyz.apiote.bimba.czwek.repo.StopStub
import xyz.apiote.bimba.czwek.search.BimbaViewHolder


class LineGraphFragment : Fragment() {

	private lateinit var pageViewModel: PageViewModel
	private var _binding: FragmentLineGraphBinding? = null
	private val binding get() = _binding!!
	private lateinit var adapter: LineGraphAdapter

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		adapter = LineGraphAdapter(
			arguments?.getString("lineID", "") ?: "",
			arguments?.getString("lineName", "") ?: "",
			arguments?.getString("feedID", "") ?: ""
		)
		pageViewModel = ViewModelProvider(this)[PageViewModel::class.java].apply {
		}
	}

	override fun onCreateView(
		inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
	): View {

		_binding = FragmentLineGraphBinding.inflate(inflater, container, false)

		ViewCompat.setOnApplyWindowInsetsListener(binding.recycler) { v, windowInsets ->
			val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
			v.updatePadding(right = insets.right, left = insets.left, top = insets.top, bottom = insets.bottom)
			windowInsets
		}

		val configuration = SugiyamaConfiguration.Builder()
			.setLevelSeparation(100)
			.build()

		binding.recycler.layoutManager = SugiyamaLayoutManager(requireContext(), configuration)
		binding.recycler.addItemDecoration(SugiyamaArrowEdgeDecoration(Paint(Paint.ANTI_ALIAS_FLAG).apply {
			strokeWidth = 5f
			val a: TypedArray? = context?.theme?.obtainStyledAttributes(
				R.style.Theme_Bimba, intArrayOf(com.google.android.material.R.attr.colorOnBackground)
			)
			val intColor = a?.getColor(0, 0)
			a?.recycle()
			color = intColor ?: 0
			style = Paint.Style.STROKE
			strokeJoin = Paint.Join.ROUND
			pathEffect = CornerPathEffect(10f)
		}))
		binding.recycler.adapter = adapter
		pageViewModel.let {
			val lineGraph = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
				arguments?.getParcelable("graph", LineGraph::class.java)
			} else {
				@Suppress("DEPRECATION")
				arguments?.getParcelable("graph") as LineGraph?
			}
			it.setupGraphView(lineGraph!!)
			it.data.observe(viewLifecycleOwner) { graph ->
				adapter.submitGraph(graph)
			}
		}

		return binding.root
	}

	companion object {
		@JvmStatic
		fun newInstance(
			lineGraph: LineGraph,
			lineID: String,
			lineName: String,
			feedID: String
		): LineGraphFragment {
			return LineGraphFragment().apply {
				arguments = Bundle().apply {
					putParcelable("graph", lineGraph)
					putString("lineID", lineID)
					putString("lineName", lineName)
					putString("feedID", feedID)
				}
			}
		}
	}

	override fun onDestroyView() {
		super.onDestroyView()
		_binding = null
	}
}

class LineGraphAdapter(
	private val lineID: String,
	private val lineName: String,
	private val feedID: String
) :
	AbstractGraphAdapter<BimbaViewHolder>() {
	private lateinit var context: Context
	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaViewHolder {
		context = parent.context
		val view = LayoutInflater.from(parent.context)
			.inflate(R.layout.result, parent, false)
		return BimbaViewHolder(view)
	}

	override fun onBindViewHolder(holder: BimbaViewHolder, position: Int) {
		BimbaViewHolder.bind(getNodeData(position) as StopStub, holder, context) {
			val intent = Intent(context, DeparturesActivity::class.java).apply {
				putExtra("code", it.code)
				putExtra("name", it.name)
				putExtra("line", lineName)
				putExtra("lineID", lineID)
				putExtra("feedID", feedID)
			}
			context.startActivity(intent)
		}
	}
}