api refactored and cleaned
This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
package com.meloda.fast.common
|
||||
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import com.meloda.fast.R
|
||||
|
||||
object FragmentSwitcher {
|
||||
|
||||
fun getCurrentFragment(fragmentManager: FragmentManager): Fragment? {
|
||||
val fragments = fragmentManager.fragments
|
||||
|
||||
if (fragments.isEmpty()) throw RuntimeException("FragmentManager's fragments is empty")
|
||||
|
||||
for (fragment in fragments) {
|
||||
if (fragment.isVisible) {
|
||||
return fragment
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun addFragments(
|
||||
fragmentManager: FragmentManager,
|
||||
containerId: Int,
|
||||
fragments: Collection<Fragment>
|
||||
) {
|
||||
val transaction = fragmentManager.beginTransaction()
|
||||
|
||||
for (fragment in fragments) {
|
||||
transaction.add(containerId, fragment, fragment.javaClass.simpleName)
|
||||
}
|
||||
|
||||
transaction.commitNow()
|
||||
}
|
||||
|
||||
fun showFragment(fragmentManager: FragmentManager, tag: String) {
|
||||
showFragment(fragmentManager, tag, false)
|
||||
}
|
||||
|
||||
fun showFragment(
|
||||
fragmentManager: FragmentManager,
|
||||
tag: String,
|
||||
hideOthers: Boolean,
|
||||
containerId: Int = R.id.fragmentContainer
|
||||
) {
|
||||
val fragments = fragmentManager.fragments
|
||||
|
||||
if (fragments.isEmpty()) throw RuntimeException("FragmentManager's fragments is empty")
|
||||
|
||||
var fragmentToShow: Fragment? = null
|
||||
|
||||
for (fragment in fragments) {
|
||||
if (fragment.tag != null && fragment.tag == tag) {
|
||||
fragmentToShow = fragment
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
val transaction = fragmentManager.beginTransaction()
|
||||
|
||||
if (fragmentToShow == null) {
|
||||
throw NullPointerException("Required fragment is null")
|
||||
} else {
|
||||
transaction.show(fragmentToShow)
|
||||
}
|
||||
|
||||
if (hideOthers) {
|
||||
for (fragment in fragments) {
|
||||
if (fragment.tag != null && fragment.tag == tag) continue
|
||||
transaction.hide(fragment)
|
||||
}
|
||||
}
|
||||
|
||||
transaction.commit()
|
||||
}
|
||||
|
||||
fun clearFragments(fragmentManager: FragmentManager) {
|
||||
val fragments = fragmentManager.fragments
|
||||
|
||||
if (fragments.isEmpty()) throw RuntimeException("FragmentManager's fragments is empty")
|
||||
|
||||
val transaction = fragmentManager.beginTransaction()
|
||||
|
||||
for (fragment in fragments) {
|
||||
transaction.remove(fragment)
|
||||
}
|
||||
|
||||
transaction.commitNow()
|
||||
}
|
||||
|
||||
fun hideFragments(fragmentManager: FragmentManager) {
|
||||
val fragments = fragmentManager.fragments
|
||||
|
||||
if (fragments.isEmpty()) throw RuntimeException("FragmentManager's fragments is empty")
|
||||
|
||||
val transaction = fragmentManager.beginTransaction()
|
||||
|
||||
for (fragment in fragments) {
|
||||
transaction.hide(fragment)
|
||||
}
|
||||
|
||||
transaction.commitNow()
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.meloda.fast.common
|
||||
|
||||
import android.util.Log
|
||||
import androidx.collection.arrayMapOf
|
||||
import com.meloda.fast.concurrent.TaskManager
|
||||
import com.meloda.fast.BuildConfig
|
||||
import com.meloda.fast.model.NewUpdateInfo
|
||||
import com.meloda.fast.net.HttpRequest
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
object UpdateManager {
|
||||
|
||||
interface OnUpdateListener {
|
||||
fun onNewUpdate(updateInfo: NewUpdateInfo)
|
||||
|
||||
fun onNoUpdates()
|
||||
}
|
||||
|
||||
private const val checkLink = "https://melodev.procsec.top/vkm/project_vkm_ota.json"
|
||||
|
||||
private const val PRODUCT_NAME = "project_vkm"
|
||||
private const val BRANCH = "alpha"
|
||||
private const val OFFSET = 0
|
||||
|
||||
private const val TAG = "UpdateManager"
|
||||
|
||||
fun checkUpdates(onUpdateListener: OnUpdateListener) {
|
||||
TaskManager.execute {
|
||||
val newLink = "https://temply.procsec.top/prop/deploy/api/method/getOTA"
|
||||
|
||||
val params = arrayMapOf<String, String>()
|
||||
params["product"] = PRODUCT_NAME
|
||||
params["branch"] = BRANCH
|
||||
params["offset"] = OFFSET.toString()
|
||||
params["code"] = AppGlobal.versionCode.toString()
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "Request started")
|
||||
}
|
||||
|
||||
HttpRequest[newLink, params].asString().let {
|
||||
AppGlobal.post {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "response: $it")
|
||||
}
|
||||
|
||||
val response: Any = if (it == "[]") JSONArray(it) else JSONObject(it)
|
||||
|
||||
val newUpdateInfo: NewUpdateInfo? =
|
||||
if (response is JSONArray) null else NewUpdateInfo(response as JSONObject)
|
||||
|
||||
if (response is JSONArray || newUpdateInfo?.version?.isEmpty() == true || newUpdateInfo?.version == AppGlobal.versionName) {
|
||||
onUpdateListener.onNoUpdates()
|
||||
return@post
|
||||
} else {
|
||||
newUpdateInfo?.let { onUpdateListener.onNewUpdate(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HttpRequest[checkLink].asString().let {
|
||||
// val response = JSONObject(it)
|
||||
//
|
||||
// val updateInfo = UpdateInfo(response)
|
||||
//
|
||||
// AppGlobal.handler.post {
|
||||
// if (updateInfo.version.isEmpty() || updateInfo.version == AppGlobal.versionName) {
|
||||
// onUpdateListener.onNoUpdates()
|
||||
// return@post
|
||||
// }
|
||||
//
|
||||
// if (AppGlobal.versionName != updateInfo.version) {
|
||||
// onUpdateListener.onNewUpdate(updateInfo)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user