Issue #45: Use fragments and add tabs tray component.
parent
e3da924944
commit
b6ce863e6f
@ -0,0 +1,14 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package mozilla.fenix.ext
|
||||
|
||||
import android.support.v4.app.Fragment
|
||||
import mozilla.fenix.components.Components
|
||||
|
||||
/**
|
||||
* Get the components of this application.
|
||||
*/
|
||||
val Fragment.components: Components
|
||||
get() = activity!!.components
|
@ -0,0 +1,12 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package mozilla.fenix.fragment
|
||||
|
||||
/**
|
||||
* Interface for fragments that want to handle 'back' button presses.
|
||||
*/
|
||||
interface BackHandler {
|
||||
fun onBackPressed(): Boolean
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package mozilla.fenix.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import kotlinx.android.synthetic.main.fragment_browser.*
|
||||
import mozilla.components.feature.session.SessionFeature
|
||||
import mozilla.components.feature.tabs.toolbar.TabsToolbarFeature
|
||||
import mozilla.components.feature.toolbar.ToolbarFeature
|
||||
import mozilla.fenix.ext.components
|
||||
import mozilla.fenix.R
|
||||
|
||||
class BrowserFragment : Fragment(), BackHandler {
|
||||
private lateinit var sessionFeature: SessionFeature
|
||||
private lateinit var toolbarFeature: ToolbarFeature
|
||||
private lateinit var tabsToolbarFeature: TabsToolbarFeature
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_browser, container, false)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
toolbar.setMenuBuilder(components.menuBuilder)
|
||||
|
||||
val sessionId = arguments?.getString(SESSION_ID)
|
||||
|
||||
sessionFeature = SessionFeature(
|
||||
components.sessionManager,
|
||||
components.sessionUseCases,
|
||||
engineView,
|
||||
components.sessionStorage,
|
||||
sessionId)
|
||||
|
||||
toolbarFeature = ToolbarFeature(
|
||||
toolbar,
|
||||
components.sessionManager,
|
||||
components.sessionUseCases.loadUrl,
|
||||
components.defaultSearchUseCase,
|
||||
sessionId)
|
||||
|
||||
tabsToolbarFeature = TabsToolbarFeature(context!!, toolbar, ::showTabs)
|
||||
}
|
||||
|
||||
private fun showTabs() {
|
||||
// For now we are performing manual fragment transactions here. Once we can use the new
|
||||
// navigation support library we may want to pass navigation graphs around.
|
||||
activity?.supportFragmentManager?.beginTransaction()?.apply {
|
||||
replace(R.id.container, TabsTrayFragment())
|
||||
commit()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
sessionFeature.start()
|
||||
toolbarFeature.start()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
|
||||
sessionFeature.stop()
|
||||
toolbarFeature.stop()
|
||||
}
|
||||
|
||||
@Suppress("ReturnCount")
|
||||
override fun onBackPressed(): Boolean {
|
||||
if (toolbarFeature.handleBackPressed()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (sessionFeature.handleBackPressed()) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val SESSION_ID = "session_id"
|
||||
|
||||
fun create(sessionId: String? = null): BrowserFragment = BrowserFragment().apply {
|
||||
arguments = Bundle().apply {
|
||||
putString(SESSION_ID, sessionId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package mozilla.fenix.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import kotlinx.android.synthetic.main.fragment_tabstray.*
|
||||
import mozilla.components.feature.tabs.tabstray.TabsFeature
|
||||
import mozilla.fenix.R
|
||||
import mozilla.fenix.ext.components
|
||||
|
||||
/**
|
||||
* A fragment for displaying the tabs tray.
|
||||
*/
|
||||
class TabsTrayFragment : Fragment(), BackHandler {
|
||||
private var tabsFeature: TabsFeature? = null
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
|
||||
inflater.inflate(R.layout.fragment_tabstray, container, false)
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
toolbar.setNavigationIcon(R.drawable.mozac_ic_back)
|
||||
toolbar.setNavigationOnClickListener {
|
||||
closeTabsTray()
|
||||
}
|
||||
|
||||
toolbar.inflateMenu(R.menu.menu_tabstray)
|
||||
toolbar.setOnMenuItemClickListener {
|
||||
when (it.itemId) {
|
||||
R.id.newTab -> {
|
||||
components.tabsUseCases.addSession.invoke("about:blank", selectTab = true)
|
||||
closeTabsTray()
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
tabsFeature = TabsFeature(tabsTray, components.sessionManager, components.tabsUseCases, ::closeTabsTray)
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
tabsFeature?.start()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
|
||||
tabsFeature?.stop()
|
||||
}
|
||||
|
||||
override fun onBackPressed(): Boolean {
|
||||
closeTabsTray()
|
||||
return true
|
||||
}
|
||||
|
||||
private fun closeTabsTray() {
|
||||
activity?.supportFragmentManager?.beginTransaction()?.apply {
|
||||
replace(R.id.container, BrowserFragment.create())
|
||||
commit()
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<mozilla.components.browser.toolbar.BrowserToolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="56dp"
|
||||
android:background="@color/colorPrimary" />
|
||||
|
||||
<mozilla.components.concept.engine.EngineView
|
||||
android:id="@+id/engineView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1" />
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
android:background="@color/colorPrimary" />
|
||||
|
||||
<mozilla.components.concept.tabstray.TabsTray
|
||||
android:id="@+id/tabsTray"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/toolbar">
|
||||
|
||||
</mozilla.components.concept.tabstray.TabsTray>
|
||||
</android.support.constraint.ConstraintLayout>
|
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item
|
||||
android:id="@+id/newTab"
|
||||
android:icon="@drawable/mozac_ic_tab_new"
|
||||
android:title="@string/menu_action_add_tab"
|
||||
app:showAsAction="ifRoom" />
|
||||
</menu>
|
Loading…
Reference in New Issue