-
Notifications
You must be signed in to change notification settings - Fork 6
/
NetStateLayoutActivity.kt
100 lines (86 loc) · 3.41 KB
/
NetStateLayoutActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2022 VastGui guihy2019@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ave.vastgui.app.activity.view
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.activity.result.contract.ActivityResultContracts
import com.ave.vastgui.app.R
import com.ave.vastgui.app.databinding.ActivityNetStateLayoutBinding
import com.ave.vastgui.app.viewmodel.NetVM
import com.ave.vastgui.netstatelayout.NetStateMgr
import com.ave.vastgui.netstatelayout.listener.OnLoadingErrorListener
import com.ave.vastgui.netstatelayout.listener.OnNetErrorListener
import com.ave.vastgui.tools.activity.VastVmActivity
import com.ave.vastgui.tools.utils.NetStateUtils
import com.ave.vastgui.tools.viewbinding.viewBinding
// Author: Vast Gui
// Email: guihy2019@gmail.com
// Date: 2024/1/3 11:31
class NetStateLayoutActivity(override val layoutId: Int = R.layout.activity_net_state_layout) :
VastVmActivity<NetVM>(), OnNetErrorListener, OnLoadingErrorListener {
private val mBinding by viewBinding(ActivityNetStateLayoutBinding::bind)
private val mNetStateLayout by lazy {
mBinding.netStateLayout
}
private val wifiSetting =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (NetStateUtils.isNetworkAvailable(this)) {
mNetStateLayout.showSuccess()
} else {
mNetStateLayout.showNetError()
}
}
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val netStateMgr = NetStateMgr(this).apply {
setOnNetErrorListener(this@NetStateLayoutActivity)
setOnLoadingErrorListener(this@NetStateLayoutActivity)
setEmptyDataView(R.layout.page_empty_data_state)
}
mNetStateLayout.setNetStateMgr(netStateMgr)
if (!NetStateUtils.isNetworkAvailable(this)) {
mNetStateLayout.showNetError(RuntimeException("网络不可用"))
} else {
getSnackbar().setText("网络可用").show()
}
mBinding.anslBtn.setOnClickListener {
getViewModel().getVideos(0, 10)
}
getViewModel().videos.observeState(this) {
onFailed = { code, message ->
mNetStateLayout.showLoadingError(code, message)
}
onError = { exception ->
mNetStateLayout.showNetError(exception)
}
onEmpty = {
mNetStateLayout.showEmptyData()
}
}
getViewModel().videos.observe(this) {
mBinding.anslSentences.text = it.message
}
}
override fun onNetErrorClick() {
wifiSetting.launch(Intent(Settings.ACTION_WIFI_SETTINGS))
}
override fun onLoadingErrorClick() {
mNetStateLayout.showSuccess()
}
}