代码提交
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
package com.shuwei.dish.match.base
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.shuwei.dish.match.R
|
||||
import com.shuwei.dish.match.databinding.ActivityBaseBinding
|
||||
import com.shuwei.dish.match.dialog.Loading
|
||||
import com.shuwei.dish.match.http.HttpUtil
|
||||
import com.shuwei.dish.match.http.UrlConfig
|
||||
import com.shuwei.dish.match.ui.HomeActivity.Companion.TAG
|
||||
import com.shuwei.dish.match.utils.ActivityManager
|
||||
import com.shuwei.dish.match.utils.ext.gone
|
||||
import com.shuwei.dish.match.utils.ext.put
|
||||
import com.shuwei.dish.match.utils.ext.toast
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Locale
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
open class BaseActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityBaseBinding
|
||||
private var launcher: ActivityResultLauncher<Intent>? = null
|
||||
private var launchCallback: ((Intent?) -> Unit)? = null
|
||||
|
||||
public fun launch(cls: Class<*>, launchCallback: (Intent?) -> Unit) {
|
||||
this.launchCallback = launchCallback
|
||||
launcher?.launch(Intent(this, cls))
|
||||
}
|
||||
|
||||
public fun launch(intent: Intent, launchCallback: (Intent?) -> Unit) {
|
||||
this.launchCallback = launchCallback
|
||||
launcher?.launch(intent)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
ActivityManager.addActivity(this)
|
||||
binding = ActivityBaseBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
statusBarDarkFont(enable = false)
|
||||
launcher =
|
||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == RESULT_OK) {
|
||||
launchCallback?.invoke(result.data)
|
||||
}
|
||||
}
|
||||
|
||||
// window.setDecorFitsSystemWindows(false) // 启用 Edge-to-Edge
|
||||
// window.insetsController?.apply {
|
||||
// hide(WindowInsets.Type.statusBars()) // 隐藏状态栏
|
||||
// systemBarsBehavior =
|
||||
// WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE // 滑动时临时显示
|
||||
// }
|
||||
binding.llTitleBar.gone()
|
||||
binding.ivBack.setOnClickListener { finish() }
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
dismissLoading()
|
||||
super.onDestroy()
|
||||
ActivityManager.removeActivity(this)
|
||||
}
|
||||
|
||||
fun hideStatusBar() {
|
||||
enableEdgeToEdge()
|
||||
// val uiOptions = (View.SYSTEM_UI_FLAG_FULLSCREEN // 隐藏状态栏
|
||||
// or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) // 隐藏导航栏(可选)
|
||||
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
|
||||
}
|
||||
|
||||
fun statusBarDarkFont(enable: Boolean) {
|
||||
//window.decorView.systemUiVisibility = if (enable) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR else 0
|
||||
val color = ContextCompat.getColor(this,
|
||||
if (enable) R.color.black else R.color.white
|
||||
)
|
||||
binding.tvLeftTime.setTextColor(color)
|
||||
binding.tvRightTime.setTextColor(color)
|
||||
}
|
||||
|
||||
fun setTitleBar(
|
||||
titleBarAction: ((FrameLayout) -> Unit)? = null,
|
||||
backAction: ((ImageView) -> Unit)? = null,
|
||||
titleAction: ((TextView) -> Unit)? = null,
|
||||
rightIconActon: ((ImageView) -> Unit)? = null
|
||||
) {
|
||||
titleBarAction?.invoke(binding.llTitleBar)
|
||||
backAction?.invoke(binding.ivBack)
|
||||
titleAction?.invoke(binding.tvTitle)
|
||||
rightIconActon?.invoke(binding.ivRightIcon)
|
||||
}
|
||||
|
||||
public fun setHeaderBackground(isHomePage: Boolean = false) {
|
||||
binding.ivHeaderBg.setImageResource(
|
||||
if (isHomePage) R.drawable.bg_home_page else R.drawable.bg_other_page
|
||||
)
|
||||
}
|
||||
|
||||
override fun setContentView(view: View?) {
|
||||
if (view == binding.root) {
|
||||
super.setContentView(view)
|
||||
} else {
|
||||
binding.flContainer.addView(view)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDateTime() {
|
||||
val formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 EEEE***HH:mm:ss", Locale.CHINA)
|
||||
val dateTime = formatter.format(LocalDateTime.now(ZoneId.of("Asia/Shanghai")))
|
||||
val arr = dateTime.split("***")
|
||||
binding.tvRightTime.text = arr[0]
|
||||
binding.tvLeftTime.text = arr[1]
|
||||
}
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private val updateTask = object : Runnable {
|
||||
override fun run() {
|
||||
updateDateTime()
|
||||
handler.postDelayed(this, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
hideStatusBar()
|
||||
handler.post(updateTask)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
handler.removeCallbacks(updateTask)
|
||||
}
|
||||
|
||||
fun showLoading() {
|
||||
Loading.show(this)
|
||||
}
|
||||
|
||||
fun dismissLoading() {
|
||||
Loading.dismiss()
|
||||
}
|
||||
|
||||
fun delayDismissLoading() {
|
||||
window.decorView.postDelayed({
|
||||
Loading.dismiss()
|
||||
},500)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.shuwei.dish.match.base
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.shuwei.dish.match.R
|
||||
import com.shuwei.dish.match.db.DatabaseProvider
|
||||
|
||||
class BaseApp : Application() {
|
||||
|
||||
val database by lazy { DatabaseProvider(this).instance }
|
||||
|
||||
var token: String? = null
|
||||
var androidId: String? = null
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
instance = this
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
private var sharedPref: SharedPreferences? = null
|
||||
|
||||
public var instance: BaseApp? = null
|
||||
fun getSharedPref(): SharedPreferences? {
|
||||
if (sharedPref != null) {
|
||||
return sharedPref
|
||||
}
|
||||
val saveName = instance?.getString(R.string.sp_save_name)
|
||||
sharedPref = instance?.getSharedPreferences(saveName, Context.MODE_PRIVATE)
|
||||
return sharedPref
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.shuwei.dish.match.base
|
||||
|
||||
import androidx.fragment.app.Fragment
|
||||
|
||||
open class BaseFragment : Fragment() {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.shuwei.dish.match.base
|
||||
|
||||
class BaseReq<T> {
|
||||
var code: Int? = null
|
||||
var success: Boolean? = null
|
||||
var data: T? = null
|
||||
var message: String? = null
|
||||
var result: T? = null
|
||||
var msg: String? = null
|
||||
}
|
||||
Reference in New Issue
Block a user