This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #45: Use fragments and add tabs tray component.
- Loading branch information
Showing
12 changed files
with
293 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
97 changes: 97 additions & 0 deletions
97
app/src/main/java/mozilla/fenix/fragment/BrowserFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
app/src/main/java/mozilla/fenix/fragment/TabsTrayFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Oops, something went wrong.