Bimba.git

ref: 3877e1e462c33778518e8c48dbf9e53076c00a53

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

package xyz.apiote.bimba.czwek.search

import android.content.Context
import android.net.ConnectivityManager
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
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?) {
		super.onCreate(savedInstanceState)

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

		val lineName = intent.getStringExtra("line")!!
		val feedID = intent.getStringExtra("feedID")!!
		val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
		binding.title.text = lineName
		getGraph(lineName, feedID, cm)
	}

	private fun getGraph(
		lineName: String,
		feedID: String,
		cm: ConnectivityManager,
	) {
		MainScope().launch {
			try {
				val repository = OnlineRepository()
				val line = repository.getLine(cm, feedID, lineName, this@LineGraphActivity)
				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))
	}
}