Flatten hierarchy in presentation module

This commit is contained in:
Moez Bhatti
2018-03-22 01:43:09 -04:00
parent 2e4d96059a
commit 7212983ffe
95 changed files with 51 additions and 65 deletions
@@ -0,0 +1,109 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.conversationinfo
import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import injection.appComponent
import common.util.extensions.setVisible
import io.reactivex.subjects.PublishSubject
import kotlinx.android.synthetic.main.conversation_info_activity.*
import presentation.common.Navigator
import presentation.common.base.QkThemedActivity
import javax.inject.Inject
class ConversationInfoActivity : QkThemedActivity<ConversationInfoViewModel>(), ConversationInfoView {
override val viewModelClass = ConversationInfoViewModel::class
override val notificationsIntent by lazy { notifications.clicks() }
override val themeIntent by lazy { themePrefs.clicks() }
override val archiveIntent by lazy { archive.clicks() }
override val blockIntent by lazy { block.clicks() }
override val deleteIntent by lazy { delete.clicks() }
override val confirmDeleteIntent: PublishSubject<Unit> = PublishSubject.create()
@Inject lateinit var navigator: Navigator
@Inject lateinit var recipientAdapter: ConversationRecipientAdapter
@Inject lateinit var mediaAdapter: ConversationMediaAdapter
@Inject lateinit var itemDecoration: GridSpacingItemDecoration
init {
appComponent.inject(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.conversation_info_activity)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
viewModel.bindView(this)
setTitle(R.string.info_title)
colors.background
.autoDisposable(scope())
.subscribe { color -> window.decorView.setBackgroundColor(color) }
mediaAdapter.thumbnailClicks
.autoDisposable(scope())
.subscribe { view -> navigator.showImageAnimated(this, view) }
recipients.adapter = recipientAdapter
media.adapter = mediaAdapter
media.addItemDecoration(itemDecoration)
}
override fun render(state: ConversationInfoState) {
if (state.hasError) {
finish()
return
}
recipientAdapter.data = state.recipients
recipientAdapter.threadId = state.threadId
notifications.setVisible(!state.blocked)
archive.setVisible(!state.blocked)
archive.title = getString(when (state.archived) {
true -> R.string.info_unarchive
false -> R.string.info_archive
})
block.title = getString(when (state.blocked) {
true -> R.string.info_unblock
false -> R.string.info_block
})
mediaAdapter.data = state.media
}
override fun showDeleteDialog() {
AlertDialog.Builder(this)
.setTitle(R.string.dialog_delete_title)
.setMessage(R.string.dialog_delete_message)
.setPositiveButton(R.string.button_delete, { _, _ -> confirmDeleteIntent.onNext(Unit) })
.setNegativeButton(R.string.button_cancel, null)
.show()
}
}
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.conversationinfo
import model.MmsPart
import model.Recipient
data class ConversationInfoState(
val recipients: List<Recipient> = listOf(),
val threadId: Long = 0,
val archived: Boolean = false,
val blocked: Boolean = false,
val media: List<MmsPart> = ArrayList(),
val hasError: Boolean = false
)
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.conversationinfo
import io.reactivex.Observable
import presentation.common.base.QkView
interface ConversationInfoView : QkView<ConversationInfoState> {
val notificationsIntent: Observable<Unit>
val themeIntent: Observable<Unit>
val archiveIntent: Observable<Unit>
val blockIntent: Observable<Unit>
val deleteIntent: Observable<Unit>
val confirmDeleteIntent: Observable<Unit>
fun showDeleteDialog()
}
@@ -0,0 +1,153 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.conversationinfo
import android.content.Context
import android.content.Intent
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.kotlin.autoDisposable
import injection.appComponent
import interactor.DeleteConversation
import interactor.MarkArchived
import interactor.MarkBlocked
import interactor.MarkUnarchived
import interactor.MarkUnblocked
import io.reactivex.Observable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.withLatestFrom
import model.Conversation
import presentation.common.Navigator
import presentation.common.base.QkViewModel
import repository.MessageRepository
import util.extensions.asObservable
import util.extensions.isImage
import javax.inject.Inject
class ConversationInfoViewModel(intent: Intent) : QkViewModel<ConversationInfoView,
ConversationInfoState>(ConversationInfoState()) {
@Inject lateinit var context: Context
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var markArchived: MarkArchived
@Inject lateinit var markUnarchived: MarkUnarchived
@Inject lateinit var markBlocked: MarkBlocked
@Inject lateinit var markUnblocked: MarkUnblocked
@Inject lateinit var navigator: Navigator
@Inject lateinit var deleteConversation: DeleteConversation
private val conversation: Observable<Conversation>
init {
appComponent.inject(this)
val threadId = intent.extras?.getLong("threadId") ?: 0L
newState { it.copy(threadId = threadId) }
conversation = messageRepo.getConversationAsync(threadId)
.asObservable<Conversation>()
.filter { conversation -> conversation.isLoaded }
.doOnNext { conversation ->
if (!conversation.isValid) {
newState { it.copy(hasError = true) }
}
}
.filter { conversation -> conversation.isValid }
.filter { conversation -> conversation.id != 0L }
disposables += markArchived
disposables += markUnarchived
disposables += markBlocked
disposables += markUnblocked
disposables += deleteConversation
// Update the recipients whenever they change
disposables += conversation
.map { conversation -> conversation.recipients }
.distinctUntilChanged()
.subscribe { recipients -> newState { it.copy(recipients = recipients) } }
// Update the view's archived state whenever it changes
disposables += conversation
.map { conversation -> conversation.archived }
.distinctUntilChanged()
.subscribe { archived -> newState { it.copy(archived = archived) } }
// Update the view's blocked state whenever it changes
disposables += conversation
.map { conversation -> conversation.blocked }
.distinctUntilChanged()
.subscribe { blocked -> newState { it.copy(blocked = blocked) } }
// Load the attachments from the conversation
disposables += messageRepo.getPartsForConversation(threadId)
.map { parts -> parts.filter { part -> part.isImage() } }
.map { parts -> parts.reversed() }
.subscribe { media -> newState { it.copy(media = media) } }
}
override fun bindView(view: ConversationInfoView) {
super.bindView(view)
// Show the notifications settings for the conversation
view.notificationsIntent
.withLatestFrom(conversation, { _, conversation -> conversation })
.autoDisposable(view.scope())
.subscribe { conversation -> navigator.showNotificationSettings(conversation.id) }
// Show the theme settings for the conversation
view.themeIntent
.withLatestFrom(conversation, { _, conversation -> conversation })
.autoDisposable(view.scope())
.subscribe { conversation -> navigator.showThemePicker(conversation.id) }
// Toggle the archived state of the conversation
view.archiveIntent
.withLatestFrom(conversation, { _, conversation -> conversation })
.autoDisposable(view.scope())
.subscribe { conversation ->
when (conversation.archived) {
true -> markUnarchived.execute(conversation.id)
false -> markArchived.execute(conversation.id)
}
}
// Toggle the blocked state of the conversation
view.blockIntent
.withLatestFrom(conversation, { _, conversation -> conversation })
.autoDisposable(view.scope())
.subscribe { conversation ->
when (conversation.blocked) {
true -> markUnblocked.execute(conversation.id)
false -> markBlocked.execute(conversation.id)
}
}
// Show the delete confirmation dialog
view.deleteIntent
.autoDisposable(view.scope())
.subscribe { view.showDeleteDialog() }
// Delete the conversation
view.confirmDeleteIntent
.withLatestFrom(conversation, { _, conversation -> conversation })
.autoDisposable(view.scope())
.subscribe { conversation -> deleteConversation.execute(conversation.id) }
}
}
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.conversationinfo
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.moez.QKSMS.R
import common.util.Colors
import common.util.GlideApp
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.PublishSubject
import kotlinx.android.synthetic.main.conversation_media_list_item.view.*
import model.MmsPart
import presentation.common.base.QkAdapter
import presentation.common.base.QkViewHolder
import javax.inject.Inject
class ConversationMediaAdapter @Inject constructor(
private val context: Context,
private val colors: Colors
) : QkAdapter<MmsPart>() {
val thumbnailClicks: PublishSubject<View> = PublishSubject.create()
private val disposables = CompositeDisposable()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.conversation_media_list_item, parent, false)
disposables += colors.separator.subscribe { color -> view.setBackgroundColor(color) }
return QkViewHolder(view)
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val part = getItem(position)
val view = holder.itemView
GlideApp.with(context)
.load(part.image)
.fitCenter()
.into(view.thumbnail)
view.thumbnail.transitionName = part.id.toString()
view.thumbnail.setOnClickListener { thumbnailClicks.onNext(it) }
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
disposables.clear()
}
}
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.conversationinfo
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.moez.QKSMS.R
import common.util.Colors
import common.util.extensions.setTint
import common.util.extensions.setVisible
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import kotlinx.android.synthetic.main.conversation_recipient_list_item.view.*
import model.Recipient
import presentation.common.Navigator
import presentation.common.base.QkAdapter
import presentation.common.base.QkViewHolder
import javax.inject.Inject
class ConversationRecipientAdapter @Inject constructor(
private val context: Context,
private val colors: Colors,
private val navigator: Navigator
) : QkAdapter<Recipient>() {
var threadId: Long = 0L
private val disposables = CompositeDisposable()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.conversation_recipient_list_item, parent, false)
disposables += colors.textSecondary
.subscribe { color -> view.add.setTint(color) }
return QkViewHolder(view)
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val recipient = getItem(position)
val view = holder.itemView
view.setOnClickListener {
if (recipient.contact == null) {
navigator.addContact(recipient.address)
} else {
view.avatar.callOnClick()
}
}
view.avatar.threadId = threadId
view.avatar.contact = recipient.contact
view.name.text = recipient.contact?.name ?: recipient.address
view.address.text = recipient.address
view.address.setVisible(recipient.contact != null)
view.add.setVisible(recipient.contact == null)
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
disposables.clear()
}
}
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package presentation.feature.conversationinfo
import android.content.Context
import android.graphics.Rect
import android.support.v7.widget.RecyclerView
import android.view.View
import common.util.extensions.dpToPx
import javax.inject.Inject
class GridSpacingItemDecoration @Inject constructor(context: Context) : RecyclerView.ItemDecoration() {
private val spanCount = 3
private val spacing = 2.dpToPx(context)
override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
val position = parent.getChildAdapterPosition(view)
val column = position % spanCount
outRect.left = column * spacing / spanCount
outRect.right = spacing - (column + 1) * spacing / spanCount
if (position >= spanCount) {
outRect.top = spacing
}
}
}