Improve RiotFragment

This commit is contained in:
Benoit Marty 2019-04-02 12:14:16 +02:00
parent 10520fb1bd
commit b9b8527b38
7 changed files with 110 additions and 28 deletions

View File

@ -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<Restorable>()

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)
}
}

}

View File

@ -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
}
}

View File

@ -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)

View File

@ -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<GroupSummaryController>()

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)

View File

@ -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)

View File

@ -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)

View File

@ -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<HomeNavigator>()
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)