Commit 87e18b75 authored by Valere's avatar Valere

Refactoring / better support for fragment and handleback

parent 8b26ab81
/*
* 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.activity
/**
* A fragment should implement this interface if it wants to intercept backPressed events.
* Any activity extending VectorAppCompatActivity will propagate back pressed event to child
* fragment that implements it.
*/
interface HandleBackParticipant {
/**
* Returns true, if the on back pressed event has been handled by this Fragment.
* Otherwise return false
*/
fun onBackPressed(): Boolean
}
\ No newline at end of file
......@@ -24,9 +24,9 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.annotation.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentManager
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.Unbinder
......@@ -36,6 +36,8 @@ import im.vector.R
import im.vector.VectorApp
import im.vector.activity.interfaces.Restorable
import im.vector.dialogs.ConsentNotGivenHelper
import im.vector.fragments.VectorBaseFragment
import im.vector.fragments.VectorBaseMvRxFragment
import im.vector.receiver.DebugReceiver
import im.vector.ui.themes.ActivityOtherThemes
import im.vector.ui.themes.ThemeUtils
......@@ -152,6 +154,27 @@ abstract class VectorAppCompatActivity : BaseMvRxActivity() {
}
}
override fun onBackPressed() {
val handled = recursivelyDispatchOnBackPressed(supportFragmentManager)
if (!handled) {
super.onBackPressed()
}
}
private fun recursivelyDispatchOnBackPressed(fm: FragmentManager): Boolean {
val reverseOrder = fm.fragments.filter { it is VectorBaseFragment || it is VectorBaseMvRxFragment }.reversed()
for (f in reverseOrder) {
val handledByChildFragments = recursivelyDispatchOnBackPressed(f.childFragmentManager)
if (handledByChildFragments) {
return true
}
if (f is HandleBackParticipant && f.onBackPressed()) {
return true
}
}
return false
}
override fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean, newConfig: Configuration?) {
super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig)
......
......@@ -108,19 +108,6 @@ class WidgetActivity : VectorAppCompatActivity() {
})
}
/* ==========================================================================================
* UI EVENTS
* ========================================================================================== */
override fun onBackPressed() {
//Hugly i know
(supportFragmentManager.findFragmentByTag(FRAGMENT_TAG_WEBVIEW) as? RoomWidgetFragment)?.let { widgetFragment ->
if (widgetFragment.onBackPressed()) return
}
super.onBackPressed()
}
/* ==========================================================================================
* companion
* ========================================================================================== */
......
......@@ -37,6 +37,7 @@ import androidx.lifecycle.Observer
import butterknife.BindView
import com.airbnb.mvrx.*
import im.vector.R
import im.vector.activity.HandleBackParticipant
import im.vector.activity.ReviewTermsActivity
import im.vector.activity.util.TERMS_REQUEST_CODE
import im.vector.fragments.VectorBaseMvRxFragment
......@@ -44,7 +45,7 @@ import im.vector.ui.themes.ThemeUtils
import im.vector.util.openUrlInExternalBrowser
import org.matrix.androidsdk.features.terms.TermsManager
class RoomWidgetFragment : VectorBaseMvRxFragment() {
class RoomWidgetFragment : VectorBaseMvRxFragment(), HandleBackParticipant {
var mWidgetWebView: WebView? = null
......@@ -255,7 +256,7 @@ class RoomWidgetFragment : VectorBaseMvRxFragment() {
super.onPrepareOptionsMenu(menu)
}
fun onBackPressed(): Boolean = withState(viewModel) { state ->
override fun onBackPressed(): Boolean = withState(viewModel) { state ->
if (state.formattedURL.complete) {
if (mWidgetWebView?.canGoBack() == true) {
mWidgetWebView?.goBack()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment