个人碎片和消息记录LoadingDialog
This commit is contained in:
@@ -5,6 +5,27 @@ import android.content.Context
|
|||||||
import com.blankj.utilcode.util.Utils
|
import com.blankj.utilcode.util.Utils
|
||||||
import com.ckkj.defense_device.utils.LanguageUtil
|
import com.ckkj.defense_device.utils.LanguageUtil
|
||||||
|
|
||||||
|
// _ooOoo_
|
||||||
|
// o8888888o
|
||||||
|
// 88" . "88
|
||||||
|
// (| -_- |)
|
||||||
|
// O\ = /O
|
||||||
|
// ____/`---'\____
|
||||||
|
// . ' \\| |// `.
|
||||||
|
// / \\||| : |||// \
|
||||||
|
// / _||||| -:- |||||- \
|
||||||
|
// | | \\\ - /// | |
|
||||||
|
// | \_| ''\---/'' | |
|
||||||
|
// \ .-\__ `-` ___/-. /
|
||||||
|
// ___`. .' /--.--\ `. . __
|
||||||
|
// ."" '< `.___\_<|>_/___.' >'"".
|
||||||
|
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
|
||||||
|
// \ \ `-. \_ __\ /__ _/ .-` / /
|
||||||
|
// ======`-.____`-.___\_____/___.-`____.-'======
|
||||||
|
// `=---='
|
||||||
|
//
|
||||||
|
// 佛祖保佑 永无BUG
|
||||||
|
|
||||||
class MyApplication : Application() {
|
class MyApplication : Application() {
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import android.view.ViewGroup
|
|||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.alibaba.sdk.android.emas.g
|
||||||
import com.ckkj.defense_device.R
|
import com.ckkj.defense_device.R
|
||||||
import com.ckkj.defense_device.adapter.DeviceAdapter
|
import com.ckkj.defense_device.adapter.DeviceAdapter
|
||||||
import com.ckkj.defense_device.adapter.FamilyAdapter
|
import com.ckkj.defense_device.adapter.FamilyAdapter
|
||||||
@@ -465,7 +466,7 @@ class HomeMenuFragment : Fragment() {
|
|||||||
|
|
||||||
isLoading = true
|
isLoading = true
|
||||||
|
|
||||||
loadingDialog = if (showLoading) LoadingDialog(requireActivity()).setMessage("加载中...").apply { show() } else null
|
loadingDialog = if (showLoading) LoadingDialog(requireActivity()).setMessage(getString(R.string.loading)).apply { show() } else null
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import android.view.View
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.ckkj.defense_device.R
|
||||||
import com.ckkj.defense_device.adapter.MessageListAdapter
|
import com.ckkj.defense_device.adapter.MessageListAdapter
|
||||||
import com.ckkj.defense_device.adapter.MessageListItem
|
import com.ckkj.defense_device.adapter.MessageListItem
|
||||||
import com.ckkj.defense_device.databinding.MessageActivityBinding
|
import com.ckkj.defense_device.databinding.MessageActivityBinding
|
||||||
import com.ckkj.defense_device.utils.MessageStatusManager
|
import com.ckkj.defense_device.utils.MessageStatusManager
|
||||||
import com.ckkj.defense_device.utils.RetrofitNetwork
|
import com.ckkj.defense_device.utils.RetrofitNetwork
|
||||||
|
import com.ckkj.defense_device.view.LoadingDialog
|
||||||
import com.gyf.immersionbar.ImmersionBar
|
import com.gyf.immersionbar.ImmersionBar
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -19,13 +21,13 @@ import kotlinx.coroutines.withContext
|
|||||||
class MessageActivity : AppCompatActivity() {
|
class MessageActivity : AppCompatActivity() {
|
||||||
private lateinit var binding: MessageActivityBinding
|
private lateinit var binding: MessageActivityBinding
|
||||||
private val messageAdapter by lazy { MessageListAdapter() }
|
private val messageAdapter by lazy { MessageListAdapter() }
|
||||||
|
private lateinit var loadingDialog: LoadingDialog
|
||||||
|
|
||||||
private var currentPage = 1
|
private var currentPage = 1
|
||||||
private val pageSize = 10
|
private val pageSize = 10
|
||||||
private var isLoading = false
|
private var isLoading = false
|
||||||
private var hasMoreData = true
|
private var hasMoreData = true
|
||||||
private val allMessages = mutableListOf<com.ckkj.defense_device.bean.MessageBean>()
|
private val allMessages = mutableListOf<com.ckkj.defense_device.bean.MessageBean>()
|
||||||
private var lastLoadedCount = 0
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@@ -41,6 +43,7 @@ class MessageActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initView() {
|
private fun initView() {
|
||||||
|
loadingDialog = LoadingDialog(this).setMessage(getString(R.string.loading))
|
||||||
binding.ivBack.setOnClickListener { finish() }
|
binding.ivBack.setOnClickListener { finish() }
|
||||||
binding.rvMessageList.layoutManager = LinearLayoutManager(this)
|
binding.rvMessageList.layoutManager = LinearLayoutManager(this)
|
||||||
binding.rvMessageList.adapter = messageAdapter
|
binding.rvMessageList.adapter = messageAdapter
|
||||||
@@ -70,20 +73,21 @@ class MessageActivity : AppCompatActivity() {
|
|||||||
if (isLoading) return
|
if (isLoading) return
|
||||||
|
|
||||||
isLoading = true
|
isLoading = true
|
||||||
|
|
||||||
|
if (currentPage == 1) {
|
||||||
|
showLoading()
|
||||||
|
}
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
val messageResponse = RetrofitNetwork.getNetworkService().getMessage(
|
val messageResponse = RetrofitNetwork.getNetworkService().getMessage(
|
||||||
pageNum = currentPage,
|
pageNum = currentPage,
|
||||||
pageSize = pageSize
|
pageSize = pageSize
|
||||||
)
|
)
|
||||||
println("total: ${messageResponse.total}")
|
|
||||||
println("rows size: ${messageResponse.rows.size}")
|
|
||||||
println("当前页:$currentPage, 加载条数:${messageResponse.rows.size}")
|
|
||||||
|
|
||||||
val newMessages = messageResponse.rows
|
val newMessages = messageResponse.rows
|
||||||
val loadedCount = newMessages.size
|
|
||||||
|
|
||||||
if (loadedCount < pageSize) {
|
if (newMessages.size < pageSize) {
|
||||||
hasMoreData = false
|
hasMoreData = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,11 +102,11 @@ class MessageActivity : AppCompatActivity() {
|
|||||||
allMessages.clear()
|
allMessages.clear()
|
||||||
}
|
}
|
||||||
allMessages.addAll(newMessages)
|
allMessages.addAll(newMessages)
|
||||||
lastLoadedCount = loadedCount
|
|
||||||
|
|
||||||
val items = convertToMessageListItems(allMessages)
|
val items = convertToMessageListItems(allMessages)
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
|
hideLoading()
|
||||||
hideEmptyState()
|
hideEmptyState()
|
||||||
messageAdapter.submitList(items)
|
messageAdapter.submitList(items)
|
||||||
|
|
||||||
@@ -116,12 +120,10 @@ class MessageActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
if (allMessages.isEmpty()) {
|
if (allMessages.isEmpty()) {
|
||||||
println("items is empty, showing empty state")
|
|
||||||
showEmptyState()
|
showEmptyState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
println("Exception: ${e.message}")
|
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
if (allMessages.isEmpty()) {
|
if (allMessages.isEmpty()) {
|
||||||
@@ -130,6 +132,11 @@ class MessageActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
isLoading = false
|
isLoading = false
|
||||||
|
if (currentPage == 1) {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
hideLoading()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,6 +148,18 @@ class MessageActivity : AppCompatActivity() {
|
|||||||
loadMessageList()
|
loadMessageList()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun showLoading() {
|
||||||
|
binding.rvMessageList.visibility = View.GONE
|
||||||
|
binding.emptyLayout.visibility = View.GONE
|
||||||
|
binding.layoutNoNetwork.visibility = View.GONE
|
||||||
|
loadingDialog.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hideLoading() {
|
||||||
|
binding.rvMessageList.visibility = View.VISIBLE
|
||||||
|
loadingDialog.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
private fun showEmptyState() {
|
private fun showEmptyState() {
|
||||||
binding.rvMessageList.visibility = View.GONE
|
binding.rvMessageList.visibility = View.GONE
|
||||||
binding.emptyLayout.visibility = View.VISIBLE
|
binding.emptyLayout.visibility = View.VISIBLE
|
||||||
@@ -171,9 +190,6 @@ class MessageActivity : AppCompatActivity() {
|
|||||||
val groupedMessages = messages.groupBy { extractDate(it.createTime ?: "") }
|
val groupedMessages = messages.groupBy { extractDate(it.createTime ?: "") }
|
||||||
groupedMessages.forEach { (date, messageList) ->
|
groupedMessages.forEach { (date, messageList) ->
|
||||||
val headerTitle = formatDate(date)
|
val headerTitle = formatDate(date)
|
||||||
messageList.forEachIndexed { index, message ->
|
|
||||||
println(" 消息${index + 1}: id=${message.id}, categoryName=${message.categoryName}, createTime=${message.createTime}")
|
|
||||||
}
|
|
||||||
items.add(MessageListItem.Header(headerTitle))
|
items.add(MessageListItem.Header(headerTitle))
|
||||||
messageList.forEach { message ->
|
messageList.forEach { message ->
|
||||||
items.add(
|
items.add(
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import com.ckkj.defense_device.utils.ActivityManager
|
|||||||
import com.ckkj.defense_device.view.CustomDialogFragmentText
|
import com.ckkj.defense_device.view.CustomDialogFragmentText
|
||||||
import com.ckkj.defense_device.view.FamilySwitchDialog
|
import com.ckkj.defense_device.view.FamilySwitchDialog
|
||||||
import com.ckkj.defense_device.view.LanguageDialog
|
import com.ckkj.defense_device.view.LanguageDialog
|
||||||
|
import com.ckkj.defense_device.view.LoadingDialog
|
||||||
import com.gyf.immersionbar.ImmersionBar
|
import com.gyf.immersionbar.ImmersionBar
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -35,6 +36,7 @@ class PersonalFragment : Fragment() {
|
|||||||
// ViewBinding
|
// ViewBinding
|
||||||
private var _binding: PersonalFragmentBinding? = null
|
private var _binding: PersonalFragmentBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
|
private var loadingDialog: LoadingDialog? = null
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
fetchUserInfo()
|
fetchUserInfo()
|
||||||
@@ -234,6 +236,7 @@ class PersonalFragment : Fragment() {
|
|||||||
|
|
||||||
|
|
||||||
private fun fetchUserInfo() {
|
private fun fetchUserInfo() {
|
||||||
|
loadingDialog = LoadingDialog(requireContext()).setMessage(getString(R.string.loading)).apply { show() }
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
val response = RetrofitNetwork.getNetworkService().getUserInfo()
|
val response = RetrofitNetwork.getNetworkService().getUserInfo()
|
||||||
@@ -244,6 +247,11 @@ class PersonalFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
|
} finally {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
loadingDialog?.dismiss()
|
||||||
|
loadingDialog = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user