Bimba.git

ref: 343255d5c2e7108dd510ca9e7707d2d3d67e0bc6

app/src/main/java/xyz/apiote/bimba/czwek/search/LineGraphActivity.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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.search

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import xyz.apiote.bimba.czwek.databinding.ActivityLineGraphBinding
import xyz.apiote.bimba.czwek.repo.OnlineRepository
import xyz.apiote.bimba.czwek.repo.TrafficResponseException
import xyz.apiote.bimba.czwek.search.ui.SectionsPagerAdapter

class LineGraphActivity : AppCompatActivity() {

	private lateinit var binding: ActivityLineGraphBinding
	private lateinit var sectionsPagerAdapter: SectionsPagerAdapter

	override fun onCreate(savedInstanceState: Bundle?) {
		enableEdgeToEdge()
		super.onCreate(savedInstanceState)

		binding = ActivityLineGraphBinding.inflate(layoutInflater)
		setContentView(binding.root)

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

		val lineName = intent.getStringExtra("lineName")!!
		val lineID = intent.getStringExtra("lineID")!!
		val feedID = intent.getStringExtra("feedID")!!
		binding.title.text = lineName
		getGraph(lineName, lineID, feedID)
	}

	private fun getGraph(
		lineName: String,
		lineID: String,
		feedID: String,
	) {
		MainScope().launch {
			try {
				val repository = OnlineRepository()
				val line = repository.getLine(this@LineGraphActivity, feedID, lineName, lineID)
				line?.let {
					sectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager, it)
					val viewPager: ViewPager = binding.viewPager
					viewPager.adapter = sectionsPagerAdapter
					val tabs: TabLayout = binding.tabs
					// todo [optimisation] hangs before changing progress to graph
					tabs.setupWithViewPager(viewPager)
					binding.lineOverlay.visibility = View.GONE
					binding.viewPager.visibility = View.VISIBLE
				}
			} catch (e: TrafficResponseException) {
				showError(e.error)
				Log.w("Line", "$e")
			}
		}
	}

	private fun showError(e: xyz.apiote.bimba.czwek.api.Error) {
		binding.lineProgress.visibility = View.GONE
		binding.errorImage.visibility = View.VISIBLE
		binding.errorText.visibility = View.VISIBLE
		binding.errorText.text = getString(e.stringResource)
		binding.errorImage.setImageDrawable(AppCompatResources.getDrawable(this, e.imageResource))
	}
}