VancedManager/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt

126 lines
4.9 KiB
Kotlin
Raw Normal View History

package com.vanced.manager.ui.fragments
2020-03-18 18:10:54 +00:00
2020-05-10 13:07:00 +00:00
import android.animation.ObjectAnimator
2020-03-18 18:10:54 +00:00
import android.os.Bundle
import android.view.*
2020-05-23 17:17:27 +00:00
import android.widget.Button
import android.widget.TextView
2020-05-10 14:01:38 +00:00
import androidx.core.animation.addListener
import androidx.viewpager2.widget.ViewPager2
2020-05-20 14:33:53 +00:00
import com.dezlum.codelabs.getjson.GetJson
2020-05-24 15:06:20 +00:00
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
2020-04-22 09:41:18 +00:00
import com.google.android.material.card.MaterialCardView
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
2020-05-23 17:17:27 +00:00
import com.google.gson.JsonObject
import com.vanced.manager.R
import com.vanced.manager.adapter.SectionPageAdapter
import com.vanced.manager.core.fragments.Home
2020-05-24 15:06:20 +00:00
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
2020-03-18 18:10:54 +00:00
class HomeFragment : Home() {
2020-03-18 18:10:54 +00:00
private lateinit var sectionPageAdapter: SectionPageAdapter
private lateinit var viewPager: ViewPager2
2020-05-24 15:06:20 +00:00
private var disposable: Disposable? = null
2020-03-18 18:10:54 +00:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
2020-04-22 09:41:18 +00:00
activity?.title = getString(R.string.title_home)
setHasOptionsMenu(true)
2020-04-22 18:00:00 +00:00
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2020-05-24 15:06:20 +00:00
//connectionStatus()
//checkNetwork()
2020-04-22 09:41:18 +00:00
2020-04-22 18:00:00 +00:00
super.onViewCreated(view, savedInstanceState)
2020-05-24 15:06:20 +00:00
checkNetwork()
sectionPageAdapter = SectionPageAdapter(this)
val tabLayout = view.findViewById(R.id.tablayout) as TabLayout
viewPager = view.findViewById(R.id.viewpager)
viewPager.adapter = sectionPageAdapter
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
when (position) {
0 -> tab.text = "Vanced"
1 -> tab.text = "MicroG"
2020-05-22 14:28:24 +00:00
2 -> tab.text = "Manager"
}
}.attach()
2020-03-18 18:10:54 +00:00
}
2020-05-24 15:06:20 +00:00
private fun checkNetwork() {
2020-05-24 15:15:17 +00:00
val networkErrorLayout = view?.findViewById<MaterialCardView>(R.id.home_network_wrapper)
2020-05-24 15:06:20 +00:00
disposable = ReactiveNetwork.observeInternetConnectivity()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { isConnectedToInternet ->
2020-05-24 15:25:11 +00:00
run {
2020-05-24 16:08:10 +00:00
val microginstallbtn = view?.findViewById<Button>(R.id.microg_installbtn)
val vancedinstallbtn = view?.findViewById<Button>(R.id.vanced_installbtn)
val vancedLatestTxt = view?.findViewById<TextView>(R.id.vanced_latest_version)
val microgLatestTxt = view?.findViewById<TextView>(R.id.microg_latest_version)
2020-05-24 15:25:11 +00:00
if (isConnectedToInternet) {
2020-05-24 16:08:10 +00:00
vancedinstallbtn?.visibility = View.VISIBLE
microginstallbtn?.visibility = View.VISIBLE
val vancedVer: JsonObject = GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/vanced.json")
val microgVer: JsonObject = GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/microg.json")
vancedLatestTxt?.text = vancedVer.get("version").asString
microgLatestTxt?.text = microgVer.get("version").asString
2020-05-24 15:25:11 +00:00
val oa2 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0f, 0.3f)
val oa3 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0.3f, -1f)
oa2.start()
oa3.apply {
oa3.addListener(onEnd = {
networkErrorLayout?.visibility = View.GONE
})
start()
}
} else {
2020-05-24 16:08:10 +00:00
vancedinstallbtn?.visibility = View.INVISIBLE
microginstallbtn?.visibility = View.INVISIBLE
vancedLatestTxt?.text = getString(R.string.unavailable)
microgLatestTxt?.text = getString(R.string.unavailable)
2020-05-24 15:25:11 +00:00
val oa2 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", -1f, 0.3f)
val oa3 = ObjectAnimator.ofFloat(networkErrorLayout, "yFraction", 0.3f, 0f)
oa2.apply {
oa2.addListener(onStart = {
networkErrorLayout?.visibility = View.VISIBLE
})
start()
}
oa3.start()
2020-05-24 15:06:20 +00:00
}
2020-05-24 15:25:11 +00:00
2020-05-24 15:15:17 +00:00
2020-05-24 15:06:20 +00:00
}
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.toolbar_menu, menu)
2020-04-22 09:41:18 +00:00
super .onCreateOptionsMenu(menu, inflater)
}
2020-03-18 18:10:54 +00:00
}