From d42bb431456bb232121b41a941b8113e664a2d8c Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Wed, 28 Feb 2018 21:44:27 -0500 Subject: [PATCH] Show media history of conversation in info activity --- .../java/data/repository/MessageRepository.kt | 17 +++- .../common/widget/SquareImageView.kt | 31 +++++++ .../ConversationInfoActivity.kt | 9 +- .../conversationinfo/ConversationInfoState.kt | 2 + .../ConversationInfoViewModel.kt | 6 ++ .../ConversationMediaAdapter.kt | 71 +++++++++++++++ .../GridSpacingItemDecoration.kt | 47 ++++++++++ .../res/layout/conversation_info_activity.xml | 89 ++++++++++++------- .../layout/conversation_media_list_item.xml | 26 ++++++ 9 files changed, 258 insertions(+), 40 deletions(-) create mode 100644 qksms/src/main/java/presentation/common/widget/SquareImageView.kt create mode 100644 qksms/src/main/java/presentation/feature/conversationinfo/ConversationMediaAdapter.kt create mode 100644 qksms/src/main/java/presentation/feature/conversationinfo/GridSpacingItemDecoration.kt create mode 100644 qksms/src/main/res/layout/conversation_media_list_item.xml diff --git a/qksms/src/main/java/data/repository/MessageRepository.kt b/qksms/src/main/java/data/repository/MessageRepository.kt index 409f65eb..f98c9e02 100644 --- a/qksms/src/main/java/data/repository/MessageRepository.kt +++ b/qksms/src/main/java/data/repository/MessageRepository.kt @@ -33,15 +33,13 @@ import com.klinker.android.send_message.StripAccents import common.util.Keys import common.util.MessageUtils import common.util.Preferences -import common.util.extensions.asFlowable -import common.util.extensions.asMaybe -import common.util.extensions.insertOrUpdate -import common.util.extensions.map +import common.util.extensions.* import data.mapper.CursorToConversation import data.mapper.CursorToRecipient import data.model.* import io.reactivex.Flowable import io.reactivex.Maybe +import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.rxkotlin.Flowables import io.reactivex.schedulers.Schedulers @@ -185,6 +183,17 @@ class MessageRepository @Inject constructor( .findFirst() } + fun getPartsForConversation(threadId: Long): Observable> { + return Realm.getDefaultInstance() + .where(Message::class.java) + .equalTo("threadId", threadId) + .isNotEmpty("parts") + .sort("date") + .findAll() + .asObservable() + .map { messages -> messages.flatMap { it.parts } } + } + /** * Retrieves the list of messages which should be shown in the notification * for a given conversation diff --git a/qksms/src/main/java/presentation/common/widget/SquareImageView.kt b/qksms/src/main/java/presentation/common/widget/SquareImageView.kt new file mode 100644 index 00000000..c050bab9 --- /dev/null +++ b/qksms/src/main/java/presentation/common/widget/SquareImageView.kt @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +package presentation.common.widget + +import android.content.Context +import android.util.AttributeSet +import android.widget.ImageView + +class SquareImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ImageView(context, attrs) { + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(widthMeasureSpec, widthMeasureSpec) + } + +} \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt index 6dee4c5f..2b66b4d9 100644 --- a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoActivity.kt @@ -20,7 +20,6 @@ package presentation.feature.conversationinfo import android.os.Bundle import android.support.v7.app.AlertDialog -import android.support.v7.widget.LinearLayoutManager import com.jakewharton.rxbinding2.view.clicks import com.moez.QKSMS.R import com.uber.autodispose.android.lifecycle.scope @@ -40,6 +39,8 @@ class ConversationInfoActivity : QkThemedActivity(), override val confirmDeleteIntent: PublishSubject = PublishSubject.create() @Inject lateinit var recipientAdapter: ConversationRecipientAdapter + @Inject lateinit var mediaAdapter: ConversationMediaAdapter + @Inject lateinit var itemDecoration: GridSpacingItemDecoration init { appComponent.inject(this) @@ -56,8 +57,10 @@ class ConversationInfoActivity : QkThemedActivity(), .autoDisposable(scope()) .subscribe { color -> window.decorView.setBackgroundColor(color) } - recipients.layoutManager = LinearLayoutManager(this) recipients.adapter = recipientAdapter + + media.adapter = mediaAdapter + media.addItemDecoration(itemDecoration) } override fun render(state: ConversationInfoState) { @@ -72,6 +75,8 @@ class ConversationInfoActivity : QkThemedActivity(), true -> R.string.info_unarchive false -> R.string.info_archive }) + + mediaAdapter.data = state.media } override fun showDeleteDialog() { diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt index c45eae6f..4f6f4739 100644 --- a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoState.kt @@ -18,10 +18,12 @@ */ package presentation.feature.conversationinfo +import data.model.MmsPart import data.model.Recipient data class ConversationInfoState( val recipients: List = listOf(), val archived: Boolean = false, + val media: List = ArrayList(), val hasError: Boolean = false ) \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt index 75b53da8..d71f908c 100644 --- a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationInfoViewModel.kt @@ -80,6 +80,12 @@ class ConversationInfoViewModel(intent: Intent) : QkViewModel conversation.archived } .distinctUntilChanged() .subscribe { archived -> newState { it.copy(archived = archived) } } + + // 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) { diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/ConversationMediaAdapter.kt b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationMediaAdapter.kt new file mode 100644 index 00000000..8af977c4 --- /dev/null +++ b/qksms/src/main/java/presentation/feature/conversationinfo/ConversationMediaAdapter.kt @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +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.GlideApp +import data.model.MmsPart +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.rxkotlin.plusAssign +import kotlinx.android.synthetic.main.conversation_media_list_item.view.* +import presentation.common.Navigator +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, + private val navigator: Navigator +) : QkAdapter() { + + 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.setOnClickListener { + navigator.showImage(part.id) + } + } + + override fun onDetachedFromRecyclerView(recyclerView: RecyclerView?) { + disposables.clear() + } + +} \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/conversationinfo/GridSpacingItemDecoration.kt b/qksms/src/main/java/presentation/feature/conversationinfo/GridSpacingItemDecoration.kt new file mode 100644 index 00000000..325260b2 --- /dev/null +++ b/qksms/src/main/java/presentation/feature/conversationinfo/GridSpacingItemDecoration.kt @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +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 + } + } + +} \ No newline at end of file diff --git a/qksms/src/main/res/layout/conversation_info_activity.xml b/qksms/src/main/res/layout/conversation_info_activity.xml index e0f0fef5..1fa72b32 100644 --- a/qksms/src/main/res/layout/conversation_info_activity.xml +++ b/qksms/src/main/res/layout/conversation_info_activity.xml @@ -31,48 +31,69 @@ android:layout_height="0dp" android:layout_weight="1"> - + android:layout_height="match_parent"> - + android:layout_height="match_parent" + android:orientation="vertical"> - + - + - + - + - + + + + + + + + + + + \ No newline at end of file