From ae38917a33308cfe0c324511f78d24e18d9b1df7 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 29 May 2019 17:19:06 +0200 Subject: [PATCH] Home badge --- .../features/home/HomeDetailFragment.kt | 19 ++++- .../features/home/HomeDetailViewModel.kt | 83 +++++++++++++++++++ .../features/home/HomeDetailViewState.kt | 28 +++++++ ...ml => vector_home_badge_unread_layout.xml} | 0 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewModel.kt create mode 100644 vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewState.kt rename vector/src/main/res/layout/{vector_unread_layout.xml => vector_home_badge_unread_layout.xml} (100%) diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt index dea2bcef..ef6543b1 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailFragment.kt @@ -21,6 +21,8 @@ import android.os.Parcelable import android.view.LayoutInflater import androidx.core.view.forEachIndexed import com.airbnb.mvrx.args +import com.airbnb.mvrx.fragmentViewModel +import com.airbnb.mvrx.withState import com.google.android.material.bottomnavigation.BottomNavigationItemView import com.google.android.material.bottomnavigation.BottomNavigationMenuView import im.vector.riotredesign.R @@ -43,12 +45,18 @@ data class HomeDetailParams( private const val CURRENT_DISPLAY_MODE = "CURRENT_DISPLAY_MODE" +private const val INDEX_CATCHUP = 0 +private const val INDEX_PEOPLE = 1 +private const val INDEX_ROOMS = 2 + class HomeDetailFragment : VectorBaseFragment() { private val params: HomeDetailParams by args() private val unreadCounterBadgeViews = arrayListOf() private lateinit var currentDisplayMode: RoomListFragment.DisplayMode + private val viewModel: HomeDetailViewModel by fragmentViewModel() + override fun getLayoutResId(): Int { return R.layout.fragment_home_detail } @@ -81,7 +89,7 @@ class HomeDetailFragment : VectorBaseFragment() { groupToolbarAvatarImageView ) groupToolbarAvatarImageView.setOnClickListener { - + vectorBaseActivity.notImplemented("Group click in toolbar") } } @@ -103,7 +111,7 @@ class HomeDetailFragment : VectorBaseFragment() { val menuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView menuView.forEachIndexed { index, view -> val itemView = view as BottomNavigationItemView - val badgeLayout = LayoutInflater.from(requireContext()).inflate(R.layout.vector_unread_layout, menuView, false) + val badgeLayout = LayoutInflater.from(requireContext()).inflate(R.layout.vector_home_badge_unread_layout, menuView, false) val unreadCounterBadgeView: UnreadCounterBadgeView = badgeLayout.findViewById(R.id.actionUnreadCounterBadgeView) itemView.addView(badgeLayout) unreadCounterBadgeViews.add(index, unreadCounterBadgeView) @@ -127,6 +135,13 @@ class HomeDetailFragment : VectorBaseFragment() { .commit() } + + override fun invalidate() = withState(viewModel) { + unreadCounterBadgeViews[INDEX_CATCHUP].render(UnreadCounterBadgeView.State(it.notificationCountCatchup, it.notificationHighlightCatchup)) + unreadCounterBadgeViews[INDEX_PEOPLE].render(UnreadCounterBadgeView.State(it.notificationCountPeople, it.notificationHighlightPeople)) + unreadCounterBadgeViews[INDEX_ROOMS].render(UnreadCounterBadgeView.State(it.notificationCountRooms, it.notificationHighlightRooms)) + } + companion object { fun newInstance(args: HomeDetailParams): HomeDetailFragment { diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewModel.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewModel.kt new file mode 100644 index 00000000..5d246922 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewModel.kt @@ -0,0 +1,83 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.home + +import com.airbnb.mvrx.MvRxViewModelFactory +import com.airbnb.mvrx.ViewModelContext +import im.vector.matrix.android.api.session.Session +import im.vector.matrix.rx.rx +import im.vector.riotredesign.core.platform.VectorViewModel +import org.koin.android.ext.android.get + +class HomeDetailViewModel(initialState: HomeDetailViewState, + private val session: Session) + : VectorViewModel(initialState) { + + companion object : MvRxViewModelFactory { + + @JvmStatic + override fun create(viewModelContext: ViewModelContext, state: HomeDetailViewState): HomeDetailViewModel? { + val currentSession = viewModelContext.activity.get() + return HomeDetailViewModel(state, currentSession) + } + } + + init { + observeRoomSummaries() + } + + // PRIVATE METHODS ***************************************************************************** + + // TODO Filter with selected group + private fun observeRoomSummaries() { + session + .rx() + .liveRoomSummaries() + .execute { state -> + state.invoke()?.let { summaries -> + val peopleNotifications = summaries + .filter { it.isDirect } + .map { it.notificationCount } + .reduce { acc, i -> acc + i } + val peopleHasHighlight = summaries + .filter { it.isDirect } + .any { it.highlightCount > 0 } + + val roomsNotifications = summaries + .filter { !it.isDirect } + .map { it.notificationCount } + .reduce { acc, i -> acc + i } + val roomsHasHighlight = summaries + .filter { !it.isDirect } + .any { it.highlightCount > 0 } + + copy( + notificationCountCatchup = peopleNotifications + roomsNotifications, + notificationHighlightCatchup = peopleHasHighlight || roomsHasHighlight, + notificationCountPeople = peopleNotifications, + notificationHighlightPeople = peopleHasHighlight, + notificationCountRooms = roomsNotifications, + notificationHighlightRooms = roomsHasHighlight + ) + } ?: run { + // No change + copy() + } + } + } + +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewState.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewState.kt new file mode 100644 index 00000000..b85b7990 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDetailViewState.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.home + +import com.airbnb.mvrx.MvRxState + +data class HomeDetailViewState( + val notificationCountCatchup: Int = 0, + val notificationHighlightCatchup: Boolean = false, + val notificationCountPeople: Int = 0, + val notificationHighlightPeople: Boolean = false, + val notificationCountRooms: Int = 0, + val notificationHighlightRooms: Boolean = false +) : MvRxState \ No newline at end of file diff --git a/vector/src/main/res/layout/vector_unread_layout.xml b/vector/src/main/res/layout/vector_home_badge_unread_layout.xml similarity index 100% rename from vector/src/main/res/layout/vector_unread_layout.xml rename to vector/src/main/res/layout/vector_home_badge_unread_layout.xml