This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
WeChatAuthorActivity.kt
141 lines (118 loc) · 5.45 KB
/
WeChatAuthorActivity.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.numeron.wandroid.activity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.paging.PagedList
import com.numeron.adapter.BindingHolder
import com.numeron.adapter.PagedBindingAdapter
import com.numeron.adapter.SpaceItemDecoration
import com.numeron.brick.lazyViewModel
import com.numeron.chameleon.Chameleon
import com.numeron.common.State
import com.numeron.stateful.livedata.StatefulCallback
import com.numeron.stateful.livedata.StatefulObserver
import com.numeron.util.dp
import com.numeron.util.toast
import com.numeron.wandroid.entity.db.WeChatAuthor
import com.numeron.wandroid.other.*
import com.numeron.wandroid.R
import com.numeron.wandroid.contract.WeChatAuthorViewModel
import com.numeron.wandroid.databinding.RecyclerItemWeChatAuthorLayoutBinding
import kotlinx.android.synthetic.main.activity_we_chat_author_layout.*
class WeChatAuthorActivity : BaseActivity() {
private val weChatAuthorAdapter = WeChatAuthorAdapter()
private val weChatAuthorViewModel: WeChatAuthorViewModel by lazyViewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_we_chat_author_layout)
setSupportActionBar(weChatAuthorToolbar)
weChatAuthorRecyclerView.adapter = weChatAuthorAdapter
weChatAuthorRecyclerView.addItemDecoration(SpaceItemDecoration(4.dp))
weChatAuthorRefreshLayout.setOnRefreshListener(weChatAuthorViewModel::refresh)
weChatAuthorRefreshLayout.isEnabled = connectivityManager.isDefaultNetworkActive
weChatAuthorViewModel.weChatAuthorLiveData.observe(this, StatefulObserver(WeChatAuthorStatefulCallback()))
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_we_chat_author, menu)
return true
}
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
if (menu != null) {
val isNightMode = Chameleon.getThemeId(this) == R.style.AppTheme_Night
menu.findItem(R.id.changeTheme)?.isChecked = isNightMode
return true
}
return super.onPrepareOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.changeTheme -> {
val themeId = if (item.isChecked) R.style.AppTheme else R.style.AppTheme_Night
changeTheme(themeId)
item.isChecked = !item.isChecked
return true
}
else -> super.onOptionsItemSelected(item)
}
}
private fun changeTheme(themeId: Int) {
Chameleon(this, themeId)
.setBackgroundColor(R.id.weChatAuthorToolbar, R.attr.colorPrimary)
.setBackgroundColor(R.id.weChatAuthorRefreshLayout, R.attr.colorBackground)
.setBackgroundColor(R.id.weChatAuthorRootView, R.attr.colorBackgroundLayer1)
.setTextColor(R.id.weChatAuthorNameTextView, R.attr.colorText)
.apply(this, R.attr.colorPrimaryDark)
}
private inner class WeChatAuthorAdapter : PagedBindingAdapter<WeChatAuthor,
RecyclerItemWeChatAuthorLayoutBinding>(R.layout.recycler_item_we_chat_author_layout) {
override fun onBindViewHolder(
holder: BindingHolder<RecyclerItemWeChatAuthorLayoutBinding>, position: Int) {
val weChatAuthor = getItem(position)
holder.binding.run {
if (weChatAuthor == null) {
weChatAuthorNameTextView.text = "正在加载,请稍候..."
} else {
setWeChatAuthor(weChatAuthor)
executePendingBindings()
root.setOnClickListener {
startArticleListActivity(weChatAuthor.sid)
}
}
}
}
override fun onCurrentListChanged(previousList: PagedList<WeChatAuthor>?, currentList: PagedList<WeChatAuthor>?) {
val isFirst = intent.getBooleanExtra(EXTRA_KEY_FIRST_FLAG, true)
if (currentList.isNullOrEmpty()) {
if (isFirst) {
weChatAuthorViewModel.getWeChatAuthors()
intent.putExtra(EXTRA_KEY_FIRST_FLAG, false)
}
weChatAuthorStatusLayout.state = State.Empty
} else {
weChatAuthorStatusLayout.state = State.Success
}
}
}
private inner class WeChatAuthorStatefulCallback : StatefulCallback<PagedList<WeChatAuthor>> {
override fun onMessage(message: String) {
toast(message)
}
override fun onSuccess(value: PagedList<WeChatAuthor>) {
weChatAuthorAdapter.submitList(value)
weChatAuthorRefreshLayout.isRefreshing = false
weChatAuthorStatusLayout.state = if (value.isEmpty()) State.Empty else State.Success
}
override fun onLoading(message: String, progress: Float) {
weChatAuthorStatusLayout.setLoadingText(message)
weChatAuthorStatusLayout.state = State.Loading
}
override fun onFailure(message: String, cause: Throwable) {
weChatAuthorStatusLayout.setFailureText(message)
weChatAuthorStatusLayout.state = State.Failure
weChatAuthorRefreshLayout.isRefreshing = false
}
}
companion object {
private const val EXTRA_KEY_FIRST_FLAG = "EXTRA_KEY_FIRST_FLAG"
}
}