From b9b8527b38d4d7dd795fae85aa1d992f6f83b791 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 2 Apr 2019 12:14:16 +0200 Subject: [PATCH] Improve RiotFragment --- .../core/platform/RiotFragment.kt | 68 +++++++++++++++++++ .../core/platform/SimpleTextWatcher.kt | 37 ++++++++++ .../features/home/HomeDrawerFragment.kt | 7 +- .../features/home/group/GroupListFragment.kt | 7 +- .../room/detail/LoadingRoomDetailFragment.kt | 6 +- .../home/room/detail/RoomDetailFragment.kt | 6 +- .../home/room/list/RoomListFragment.kt | 7 +- 7 files changed, 110 insertions(+), 28 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotredesign/core/platform/SimpleTextWatcher.kt diff --git a/vector/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt b/vector/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt index 0b7996fa..076d88b2 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/platform/RiotFragment.kt @@ -18,17 +18,70 @@ package im.vector.riotredesign.core.platform import android.os.Bundle import android.os.Parcelable +import android.view.* +import androidx.annotation.CallSuper +import androidx.annotation.LayoutRes import androidx.annotation.MainThread +import butterknife.ButterKnife +import butterknife.Unbinder import com.airbnb.mvrx.BaseMvRxFragment import com.airbnb.mvrx.MvRx import com.bumptech.glide.util.Util.assertMainThread +import timber.log.Timber abstract class RiotFragment : BaseMvRxFragment(), OnBackPressed { + // Butterknife unbinder + private var mUnBinder: Unbinder? = null + val riotActivity: RiotActivity by lazy { activity as RiotActivity } + /* ========================================================================================== + * Life cycle + * ========================================================================================== */ + + @CallSuper + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + if (getMenuRes() != -1) { + setHasOptionsMenu(true) + } + } + + final override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + return inflater.inflate(getLayoutResId(), container, false) + } + + @LayoutRes + abstract fun getLayoutResId(): Int + + @CallSuper + override fun onResume() { + super.onResume() + + Timber.d("onResume Fragment ${this.javaClass.simpleName}") + } + + @CallSuper + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + mUnBinder = ButterKnife.bind(this, view) + } + + @CallSuper + override fun onDestroyView() { + super.onDestroyView() + mUnBinder?.unbind() + mUnBinder = null + } + + /* ========================================================================================== + * Restorable + * ========================================================================================== */ + private val restorables = ArrayList() override fun onSaveInstanceState(outState: Bundle) { @@ -60,4 +113,19 @@ abstract class RiotFragment : BaseMvRxFragment(), OnBackPressed { return this } + + /* ========================================================================================== + * MENU MANAGEMENT + * ========================================================================================== */ + + open fun getMenuRes() = -1 + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + val menuRes = getMenuRes() + + if (menuRes != -1) { + inflater.inflate(menuRes, menu) + } + } + } \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/core/platform/SimpleTextWatcher.kt b/vector/src/main/java/im/vector/riotredesign/core/platform/SimpleTextWatcher.kt new file mode 100644 index 00000000..d2229542 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/core/platform/SimpleTextWatcher.kt @@ -0,0 +1,37 @@ +/* + * 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.core.platform + +import android.text.Editable +import android.text.TextWatcher + +/** + * TextWatcher with default no op implementation + */ +open class SimpleTextWatcher : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { + // No op + } + + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { + // No op + } + + override fun afterTextChanged(s: Editable) { + // No op + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt index 981f786b..dafdc990 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/HomeDrawerFragment.kt @@ -17,9 +17,6 @@ package im.vector.riotredesign.features.home import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import im.vector.riotredesign.R import im.vector.riotredesign.core.extensions.replaceChildFragment import im.vector.riotredesign.core.platform.RiotFragment @@ -35,9 +32,7 @@ class HomeDrawerFragment : RiotFragment() { } } - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.fragment_home_drawer, container, false) - } + override fun getLayoutResId() = R.layout.fragment_home_drawer override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt index fb99e839..f8290688 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/group/GroupListFragment.kt @@ -17,9 +17,6 @@ package im.vector.riotredesign.features.home.group import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import com.airbnb.mvrx.Incomplete import com.airbnb.mvrx.Success import com.airbnb.mvrx.fragmentViewModel @@ -44,9 +41,7 @@ class GroupListFragment : RiotFragment(), GroupSummaryController.Callback { private val viewModel: GroupListViewModel by fragmentViewModel() private val groupController by inject() - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.fragment_group_list, container, false) - } + override fun getLayoutResId() = R.layout.fragment_group_list override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/LoadingRoomDetailFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/LoadingRoomDetailFragment.kt index 16f2e9a1..6eb4c35a 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/LoadingRoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/LoadingRoomDetailFragment.kt @@ -17,9 +17,7 @@ package im.vector.riotredesign.features.home.room.detail import android.os.Bundle -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import com.bumptech.glide.Glide import im.vector.riotredesign.R import im.vector.riotredesign.core.platform.RiotFragment @@ -34,9 +32,7 @@ class LoadingRoomDetailFragment : RiotFragment() { } } - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.fragment_loading_room_detail, container, false) - } + override fun getLayoutResId() = R.layout.fragment_loading_room_detail override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt index 30a262c4..a66362ec 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/detail/RoomDetailFragment.kt @@ -18,9 +18,7 @@ package im.vector.riotredesign.features.home.room.detail import android.os.Bundle import android.os.Parcelable -import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.airbnb.epoxy.EpoxyVisibilityTracker @@ -69,9 +67,7 @@ class RoomDetailFragment : RiotFragment(), TimelineEventController.Callback { private lateinit var scrollOnNewMessageCallback: ScrollOnNewMessageCallback - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.fragment_room_detail, container, false) - } + override fun getLayoutResId() = R.layout.fragment_room_detail override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt index a1e98068..ece0da61 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListFragment.kt @@ -19,9 +19,6 @@ package im.vector.riotredesign.features.home.room.list import android.os.Bundle import android.text.Editable import android.text.TextWatcher -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup import androidx.recyclerview.widget.LinearLayoutManager import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Incomplete @@ -54,9 +51,7 @@ class RoomListFragment : RiotFragment(), RoomSummaryController.Callback { private val homeNavigator by inject() private val roomListViewModel: RoomListViewModel by fragmentViewModel() - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.fragment_room_list, container, false) - } + override fun getLayoutResId() = R.layout.fragment_room_list override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState)