forked from upstream-mirrors/quik
correctly bind actionbar in messagelist activity
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.moez.QKSMS.dagger
|
||||
|
||||
import android.content.Context
|
||||
import com.moez.QKSMS.data.repository.ConversationRepository
|
||||
import com.moez.QKSMS.ui.conversations.ConversationListFragment
|
||||
import dagger.Component
|
||||
import javax.inject.Singleton
|
||||
@@ -12,5 +13,6 @@ interface AppComponent {
|
||||
fun inject(fragment: ConversationListFragment)
|
||||
|
||||
fun provideContext(): Context
|
||||
fun provideConversationRepository(): ConversationRepository
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.moez.QKSMS.dagger
|
||||
|
||||
import com.moez.QKSMS.data.repository.ConversationRepository
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.ui.messages.MessageListViewModel
|
||||
import dagger.Module
|
||||
@@ -23,8 +24,8 @@ class MessagesModule(val threadId: Long) {
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
fun provideViewModel(repository: MessageRepository): MessageListViewModel {
|
||||
return MessageListViewModel(threadId, repository)
|
||||
fun provideViewModel(conversations: ConversationRepository, repository: MessageRepository): MessageListViewModel {
|
||||
return MessageListViewModel(threadId, conversations, repository)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,4 +40,16 @@ open class Conversation() : RealmObject() {
|
||||
error = cursor.getInt(ConversationColumns.ERROR)
|
||||
hasAttachment = cursor.getInt(ConversationColumns.HAS_ATTACHMENT)
|
||||
}
|
||||
|
||||
fun getTitle(): String {
|
||||
var title = ""
|
||||
contacts.forEachIndexed { index, recipient ->
|
||||
title += recipient.address
|
||||
if (index < contacts.size - 1) {
|
||||
title += ", "
|
||||
}
|
||||
}
|
||||
|
||||
return title
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,13 @@ import io.realm.RealmResults
|
||||
|
||||
class ConversationRepository {
|
||||
|
||||
fun getConversation(threadId: Long): RealmResults<Conversation> {
|
||||
return Realm.getDefaultInstance()
|
||||
.where(Conversation::class.java)
|
||||
.equalTo("id", threadId)
|
||||
.findAllAsync()
|
||||
}
|
||||
|
||||
fun getConversations(): RealmResults<Conversation> {
|
||||
return Realm.getDefaultInstance().where(Conversation::class.java).findAllAsync()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.moez.QKSMS.ui.base
|
||||
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.MenuItem
|
||||
import com.moez.QKSMS.QKApplication
|
||||
import com.moez.QKSMS.dagger.AppComponent
|
||||
|
||||
@@ -10,4 +11,14 @@ abstract class QkActivity : AppCompatActivity() {
|
||||
return (application as QKApplication).appComponent
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when(item.itemId) {
|
||||
android.R.id.home -> {
|
||||
onBackPressed()
|
||||
true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,15 +30,7 @@ class ConversationAdapter(context: Context, data: OrderedRealmCollection<Convers
|
||||
mContext.startActivity(intent)
|
||||
}
|
||||
|
||||
var title = ""
|
||||
conversation?.contacts?.forEachIndexed { index, recipient ->
|
||||
title += recipient.address
|
||||
if (index < conversation.contacts.size - 1) {
|
||||
title += ", "
|
||||
}
|
||||
}
|
||||
|
||||
viewHolder.title.text = title
|
||||
viewHolder.title.text = conversation?.getTitle()
|
||||
viewHolder.snippet.text = conversation?.snippet
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ class MessageListActivity : QkActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.base_activity)
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
val threadId = intent.getLongExtra("thread_id", 0)
|
||||
|
||||
|
||||
@@ -15,6 +15,13 @@ class MessageListFragment : QkFragment() {
|
||||
override fun provideLayoutRes(): Int = R.layout.message_list_fragment
|
||||
override fun provideMenuRes(): Int = R.menu.messages
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
viewModel.conversation.addChangeListener { realmResults ->
|
||||
context.title = realmResults[0]?.getTitle()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
|
||||
val layoutManager = LinearLayoutManager(context)
|
||||
layoutManager.stackFromEnd = true
|
||||
|
||||
@@ -2,11 +2,19 @@ package com.moez.QKSMS.ui.messages
|
||||
|
||||
import android.arch.lifecycle.ViewModel
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import com.moez.QKSMS.data.repository.ConversationRepository
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.realm.RealmResults
|
||||
|
||||
class MessageListViewModel(val threadId: Long, repository: MessageRepository) : ViewModel() {
|
||||
class MessageListViewModel(val threadId: Long, conversations: ConversationRepository, repository: MessageRepository) : ViewModel() {
|
||||
|
||||
val conversation = conversations.getConversation(threadId)
|
||||
val messages: RealmResults<Message> = repository.getMessages()
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
conversation.removeAllChangeListeners()
|
||||
messages.removeAllChangeListeners()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user