mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Clean up MessageListActivity and MessageListViewModel
This commit is contained in:
@@ -22,12 +22,10 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
viewModel = ViewModelProviders.of(this)[MessageListViewModel::class.java]
|
||||
viewModel.setThreadId(intent.getLongExtra("thread_id", 0))
|
||||
viewModel.threadId = intent.getLongExtra("thread_id", 0)
|
||||
viewModel.state.observe(this)
|
||||
|
||||
val layoutManager = LinearLayoutManager(this)
|
||||
layoutManager.stackFromEnd = true
|
||||
messageList.layoutManager = layoutManager
|
||||
messageList.layoutManager = LinearLayoutManager(this).apply { stackFromEnd = true }
|
||||
|
||||
RxTextView.textChanges(message).subscribe { text -> viewModel.textChanged(text.toString()) }
|
||||
RxView.clicks(attach).subscribe { }
|
||||
@@ -36,25 +34,14 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
|
||||
|
||||
override fun onChanged(state: MessageListViewState?) {
|
||||
state?.let {
|
||||
if (title != state.title) {
|
||||
title = state.title
|
||||
}
|
||||
|
||||
if (messageList.adapter == null) {
|
||||
messageList.adapter = MessageAdapter(this, state.messages)
|
||||
}
|
||||
|
||||
if (message.text.toString() != state.draft) {
|
||||
message.setText(state.draft)
|
||||
}
|
||||
if (title != state.title) title = state.title
|
||||
if (messageList.adapter == null) messageList.adapter = MessageAdapter(this, state.messages)
|
||||
if (message.text.toString() != state.draft) message.setText(state.draft)
|
||||
if (state.hasError) finish()
|
||||
|
||||
val color = if (state.canSend) R.color.colorPrimary else R.color.textTertiary
|
||||
send.setTint(resources.getColor(color))
|
||||
send.isEnabled = state.canSend
|
||||
|
||||
if (state.hasError) {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,40 +16,34 @@ class MessageListViewModel : ViewModel() {
|
||||
@Inject lateinit var conversationRepo: ConversationRepository
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
|
||||
val partialStates: PublishSubject<PartialState> = PublishSubject.create()
|
||||
val state: MutableLiveData<MessageListViewState> = MutableLiveData()
|
||||
var threadId: Long = 0
|
||||
set(value) {
|
||||
field = value
|
||||
newThreadId()
|
||||
}
|
||||
|
||||
private var threadId: Long = 0
|
||||
|
||||
private val partialStates: PublishSubject<PartialState> = PublishSubject.create()
|
||||
private var conversation: RealmResults<Conversation>? = null
|
||||
private var messages: RealmResults<Message>? = null
|
||||
|
||||
init {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
|
||||
val initialState = MessageListViewState()
|
||||
partialStates
|
||||
.scan(initialState, { previousState, changes -> changes.reduce(previousState) })
|
||||
.scan(MessageListViewState(), { previous, changes -> changes.reduce(previous) })
|
||||
.subscribe { newState -> state.value = newState }
|
||||
}
|
||||
|
||||
fun setThreadId(threadId: Long) {
|
||||
onCleared()
|
||||
private fun newThreadId() {
|
||||
messages = messageRepo.getMessages(threadId)
|
||||
messages?.let { partialStates.onNext(PartialState.MessagesLoaded(it)) }
|
||||
|
||||
this.threadId = threadId
|
||||
|
||||
messageRepo.getMessages(threadId).let {
|
||||
messages = it
|
||||
partialStates.onNext(PartialState.MessagesLoaded(it))
|
||||
}
|
||||
|
||||
conversationRepo.getConversation(threadId).let {
|
||||
conversation = it
|
||||
it.addChangeListener { realmResults ->
|
||||
when (realmResults.size) {
|
||||
0 -> partialStates.onNext(PartialState.ConversationError(true))
|
||||
else -> partialStates.onNext(PartialState.ConversationLoaded(realmResults[0]))
|
||||
}
|
||||
conversation = conversationRepo.getConversation(threadId)
|
||||
conversation?.addChangeListener { realmResults ->
|
||||
when (realmResults.size) {
|
||||
0 -> partialStates.onNext(PartialState.ConversationError(true))
|
||||
else -> partialStates.onNext(PartialState.ConversationLoaded(realmResults[0]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user