|
| 1 | +package dev.andus.niedu |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Context |
| 5 | +import android.content.SharedPreferences |
| 6 | +import android.os.Bundle |
| 7 | +import android.webkit.CookieManager |
| 8 | +import android.webkit.WebSettings |
| 9 | +import android.webkit.WebView |
| 10 | +import android.webkit.WebViewClient |
| 11 | +import android.widget.FrameLayout |
| 12 | +import androidx.appcompat.app.AppCompatActivity |
| 13 | +import com.google.android.material.tabs.TabLayout |
| 14 | + |
| 15 | +class MainActivity : AppCompatActivity() { |
| 16 | + |
| 17 | + private lateinit var webView: WebView |
| 18 | + private lateinit var sharedPreferences: SharedPreferences |
| 19 | + private var baseUrl: String? = null |
| 20 | + private var city: String? = null |
| 21 | + |
| 22 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 23 | + super.onCreate(savedInstanceState) |
| 24 | + setContentView(R.layout.main_activity) |
| 25 | + sharedPreferences = getSharedPreferences("app_prefs", Context.MODE_PRIVATE) |
| 26 | + baseUrl = getBaseUrl() |
| 27 | + val tabLayout = findViewById<TabLayout>(R.id.tabLayout) |
| 28 | + webView = WebView(this) |
| 29 | + val frameLayout = findViewById<FrameLayout>(R.id.frameLayout) |
| 30 | + frameLayout.addView(webView) |
| 31 | + setupWebView() |
| 32 | + |
| 33 | + tabLayout.addTab(tabLayout.newTab().setText("Strona główna")) |
| 34 | + tabLayout.addTab(tabLayout.newTab().setText("Wiadomości")) |
| 35 | + tabLayout.addTab(tabLayout.newTab().setText("Frekwencja")) |
| 36 | + tabLayout.addTab(tabLayout.newTab().setText("Oceny")) |
| 37 | + tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { |
| 38 | + override fun onTabSelected(tab: TabLayout.Tab?) { |
| 39 | + when (tab?.position) { |
| 40 | + 0 -> loadUrl("/tablica") // Strona główna |
| 41 | + 1 -> loadMessagesUrl() // Wiadomości |
| 42 | + 2 -> loadUrl("/frekwencja") // Frekwencja |
| 43 | + 3 -> loadUrl("/oceny") // Oceny |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + override fun onTabUnselected(tab: TabLayout.Tab?) {} |
| 48 | + override fun onTabReselected(tab: TabLayout.Tab?) {} |
| 49 | + }) |
| 50 | + |
| 51 | + webView.loadUrl("https://eduvulcan.pl/logowanie") |
| 52 | + } |
| 53 | + |
| 54 | + @SuppressLint("SetJavaScriptEnabled") |
| 55 | + private fun setupWebView() { |
| 56 | + webView.webViewClient = object : WebViewClient() { |
| 57 | + override fun onPageFinished(view: WebView?, url: String?) { |
| 58 | + super.onPageFinished(view, url) |
| 59 | + if (url != null && url.contains("/App/")) { |
| 60 | + saveBaseUrl(url) |
| 61 | + extractCity(url) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + val webSettings: WebSettings = webView.settings |
| 67 | + webSettings.javaScriptEnabled = true |
| 68 | + webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK |
| 69 | + webSettings.allowFileAccess = true |
| 70 | + webSettings.domStorageEnabled = true |
| 71 | + webSettings.useWideViewPort = true |
| 72 | + webSettings.loadWithOverviewMode = true |
| 73 | + webSettings.builtInZoomControls = true |
| 74 | + webSettings.setSupportZoom(true) |
| 75 | + webSettings.mediaPlaybackRequiresUserGesture = false |
| 76 | + |
| 77 | + val cookieManager: CookieManager = CookieManager.getInstance() |
| 78 | + cookieManager.setAcceptCookie(true) |
| 79 | + } |
| 80 | + |
| 81 | + private fun loadUrl(endpoint: String) { |
| 82 | + if (baseUrl == null) { |
| 83 | + webView.loadUrl("https://eduvulcan.pl/logowanie") |
| 84 | + } else { |
| 85 | + val fullUrl = "$baseUrl$endpoint" |
| 86 | + if (webView.url != fullUrl) { |
| 87 | + webView.loadUrl(fullUrl) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private fun loadMessagesUrl() { |
| 93 | + if (city != null) { |
| 94 | + val messagesUrl = "https://wiadomosci.eduvulcan.pl/$city/App/odebrane" |
| 95 | + webView.loadUrl(messagesUrl) |
| 96 | + } else { |
| 97 | + webView.loadUrl("https://eduvulcan.pl/logowanie") |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private fun saveBaseUrl(url: String) { |
| 102 | + val currentBaseUrl = getBaseUrl() |
| 103 | + if (currentBaseUrl == null) { |
| 104 | + val newBaseUrl = url.substringBeforeLast("/") |
| 105 | + with(sharedPreferences.edit()) { |
| 106 | + putString("base_url", newBaseUrl) |
| 107 | + apply() |
| 108 | + baseUrl = newBaseUrl |
| 109 | + println("Zapisano baseUrl: $newBaseUrl") |
| 110 | + } |
| 111 | + } else { |
| 112 | + println("BaseUrl już ustawione: $currentBaseUrl") |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private fun extractCity(url: String) { |
| 117 | + city = url.substringAfter("uczen.eduvulcan.pl/").substringBefore("/App") |
| 118 | + println("Wyciągnięto miasto: $city") |
| 119 | + } |
| 120 | + |
| 121 | + private fun getBaseUrl(): String? { |
| 122 | + return sharedPreferences.getString("base_url", null) |
| 123 | + } |
| 124 | + |
| 125 | + override fun onBackPressed() { |
| 126 | + if (webView.canGoBack()) { |
| 127 | + webView.goBack() |
| 128 | + } else { |
| 129 | + super.onBackPressed() |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments