Skip to content

Commit

Permalink
Refactoring core web classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Slion committed Oct 2, 2023
1 parent 09fb94a commit b8c9b98
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BrowserPresenter @Inject constructor(
private val bookmarkPageFactory: BookmarkPageFactory
): fulguris.Component() {

private var currentTab: LightningView? = null
private var currentTab: WebPageTab? = null
private var shouldClose: Boolean = false

lateinit var iBrowserView: BrowserView
Expand Down Expand Up @@ -107,7 +107,7 @@ class BrowserPresenter @Inject constructor(
*
* @param tab the tab that changed, may be null.
*/
fun tabChangeOccurred(tab: LightningView?) = tab?.let {
fun tabChangeOccurred(tab: WebPageTab?) = tab?.let {
iBrowserView.notifyTabViewChanged(tabsModel.indexOfTab(it))
}

Expand All @@ -118,7 +118,7 @@ class BrowserPresenter @Inject constructor(
* [aWasTabAdded] True if [aTab] was just created.
* [aGoingBack] Tells in which direction we are going, this can help determine what kind of tab animation will be used.
*/
private fun onTabChanged(aTab: LightningView, aWasTabAdded: Boolean, aPreviousTabClosed: Boolean, aGoingBack: Boolean) {
private fun onTabChanged(aTab: WebPageTab, aWasTabAdded: Boolean, aPreviousTabClosed: Boolean, aGoingBack: Boolean) {
Timber.d("On tab changed")

currentTab?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

package acr.browser.lightning.browser
import acr.browser.lightning.extensions.createDefaultFavicon
import acr.browser.lightning.view.LightningViewTitle
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
Expand Down
40 changes: 20 additions & 20 deletions app/src/main/java/acr/browser/lightning/browser/TabsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import javax.inject.Singleton
import kotlin.collections.ArrayList

/**
* A manager singleton that holds all the [LightningView] and tracks the current tab. It handles
* A manager singleton that holds all the [WebPageTab] and tracks the current tab. It handles
* creation, deletion, restoration, state saving, and switching of tabs and sessions.
*/
//@HiltViewModel
Expand All @@ -41,8 +41,8 @@ class TabsManager @Inject constructor(
private val userPreferences: UserPreferences
): fulguris.Component() {

private val tabList = arrayListOf<LightningView>()
var iRecentTabs = mutableSetOf<LightningView>()
private val tabList = arrayListOf<WebPageTab>()
var iRecentTabs = mutableSetOf<WebPageTab>()
// This is just used when loading and saving sessions.
// TODO: Ideally it should not be a data member.
val savedRecentTabsIndices = mutableSetOf<Int>()
Expand All @@ -60,11 +60,11 @@ class TabsManager @Inject constructor(
}

/**
* Return the current [LightningView] or null if no current tab has been set.
* Return the current [WebPageTab] or null if no current tab has been set.
*
* @return a [LightningView] or null if there is no current tab.
* @return a [WebPageTab] or null if there is no current tab.
*/
var currentTab: LightningView? = null
var currentTab: WebPageTab? = null
private set

private var tabNumberListeners = emptySet<(Int) -> Unit>()
Expand Down Expand Up @@ -251,13 +251,13 @@ class TabsManager @Inject constructor(
*
* TODO: See how you can offload IO to a background thread
*/
fun initializeTabs(activity: Activity, incognito: Boolean) : MutableList<LightningView> {
fun initializeTabs(activity: Activity, incognito: Boolean) : MutableList<WebPageTab> {
Timber.d("initializeTabs")
iIsIncognito = incognito

shutdown()

val list = mutableListOf<LightningView>()
val list = mutableListOf<WebPageTab>()

if (incognito) {
list.add(newTab(activity, incognitoPageInitializer, incognito, NewTabPosition.END_OF_TAB_LIST))
Expand Down Expand Up @@ -498,24 +498,24 @@ class TabsManager @Inject constructor(
*/
fun pauseAll() {
currentTab?.pauseTimers()
tabList.forEach(LightningView::onPause)
tabList.forEach(WebPageTab::onPause)
}

/**
* Return the tab at the given position in tabs list, or null if position is not in tabs list
* range.
*
* @param position the index in tabs list
* @return the corespondent [LightningView], or null if the index is invalid
* @return the corespondent [WebPageTab], or null if the index is invalid
*/
fun getTabAtPosition(position: Int): LightningView? =
fun getTabAtPosition(position: Int): WebPageTab? =
if (position < 0 || position >= tabList.size) {
null
} else {
tabList[position]
}

val allTabs: List<LightningView>
val allTabs: List<WebPageTab>
get() = tabList

/**
Expand Down Expand Up @@ -550,7 +550,7 @@ class TabsManager @Inject constructor(
*
* @return the last tab, or null if there are no tabs.
*/
fun lastTab(): LightningView? = tabList.lastOrNull()
fun lastTab(): WebPageTab? = tabList.lastOrNull()

/**
* Create and return a new tab. The tab is automatically added to the tabs list.
Expand All @@ -565,9 +565,9 @@ class TabsManager @Inject constructor(
tabInitializer: TabInitializer,
isIncognito: Boolean,
newTabPosition: NewTabPosition
): LightningView {
): WebPageTab {
Timber.i("New tab")
val tab = LightningView(
val tab = WebPageTab(
activity,
tabInitializer,
isIncognito,
Expand Down Expand Up @@ -640,7 +640,7 @@ class TabsManager @Inject constructor(
* @param tab the tab to look for.
* @return the position of the tab or -1 if the tab is not in the list.
*/
fun positionOf(tab: LightningView?): Int = tabList.indexOf(tab)
fun positionOf(tab: WebPageTab?): Int = tabList.indexOf(tab)


/**
Expand Down Expand Up @@ -863,15 +863,15 @@ class TabsManager @Inject constructor(
*
* @return Return the index of the tab, or -1 if the tab isn't in the list.
*/
fun indexOfTab(tab: LightningView): Int = tabList.indexOf(tab)
fun indexOfTab(tab: WebPageTab): Int = tabList.indexOf(tab)

/**
* Returns the [LightningView] with the provided hash, or null if there is no tab with the hash.
* Returns the [WebPageTab] with the provided hash, or null if there is no tab with the hash.
*
* @param hashCode the hashcode.
* @return the tab with an identical hash, or null.
*/
fun getTabForHashCode(hashCode: Int): LightningView? =
fun getTabForHashCode(hashCode: Int): WebPageTab? =
tabList.firstOrNull { lightningView -> lightningView.webView?.let { it.hashCode() == hashCode } == true }

/**
Expand All @@ -881,7 +881,7 @@ class TabsManager @Inject constructor(
* @exception IndexOutOfBoundsException if the provided index is out of range.
* @return The selected tab we just switched to.
*/
fun switchToTab(aPosition: Int): LightningView {
fun switchToTab(aPosition: Int): WebPageTab {
Timber.i("switch to tab: $aPosition")
return tabList[aPosition].also {
currentTab = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl

// For CTRL+TAB implementation
var iRecentTabIndex = -1;
var iCapturedRecentTabsIndices : Set<LightningView>? = null
var iCapturedRecentTabsIndices : Set<WebPageTab>? = null


private fun copyRecentTabsList()
Expand Down Expand Up @@ -2314,7 +2314,7 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
/**
*
*/
override fun onTabChanged(aTab: LightningView) {
override fun onTabChanged(aTab: WebPageTab) {
if (tabsManager.currentTab==aTab) {
setTaskDescription()
}
Expand All @@ -2330,7 +2330,7 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
/**
*
*/
override fun onTabChangedIcon(aTab: LightningView) {
override fun onTabChangedIcon(aTab: WebPageTab) {
if (tabsManager.currentTab==aTab) {
setTaskDescription()
}
Expand All @@ -2343,7 +2343,7 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
/**
*
*/
override fun onTabChangedTitle(aTab: LightningView) {
override fun onTabChangedTitle(aTab: WebPageTab) {
if (tabsManager.currentTab==aTab) {
setTaskDescription()
}
Expand Down Expand Up @@ -3985,14 +3985,14 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
}

/**
* Closes the specified [LightningView]. This implements
* Closes the specified [WebPageTab]. This implements
* the JavaScript callback that asks the tab to close itself and
* is especially helpful when a page creates a redirect and does
* not need the tab to stay open any longer.
*
* @param tab the LightningView to close, delete it.
*/
override fun onCloseWindow(tab: LightningView) {
override fun onCloseWindow(tab: WebPageTab) {
presenter.deleteTab(tabsManager.positionOf(tab))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package acr.browser.lightning.browser.tabs

import acr.browser.lightning.view.LightningView
import acr.browser.lightning.view.WebPageTab
import android.graphics.Bitmap
import android.graphics.Color
import timber.log.Timber
Expand Down Expand Up @@ -36,9 +36,9 @@ private fun createDefaultBitmap() : Bitmap {


/**
* Converts a [LightningView] to a [TabViewState].
* Converts a [WebPageTab] to a [TabViewState].
*/
fun LightningView.asTabViewState() = TabViewState(
fun WebPageTab.asTabViewState() = TabViewState(
id = id,
title = title,
favicon = favicon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import acr.browser.lightning.controller.UIController
import acr.browser.lightning.databinding.TabDesktopViewBinding
import acr.browser.lightning.extensions.inflater
import acr.browser.lightning.utils.ItemDragDropSwipeHelper
import acr.browser.lightning.view.LightningView
import acr.browser.lightning.view.WebPageTab
import android.content.Context
import android.util.AttributeSet
import android.view.View
Expand Down Expand Up @@ -101,7 +101,7 @@ class TabsDesktopView @JvmOverloads constructor(
}

private fun displayTabs() {
tabsAdapter.showTabs(uiController.getTabModel().allTabs.map(LightningView::asTabViewState))
tabsAdapter.showTabs(uiController.getTabModel().allTabs.map(WebPageTab::asTabViewState))
}

override fun tabsInitialized() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import acr.browser.lightning.controller.UIController
import acr.browser.lightning.databinding.TabDrawerViewBinding
import acr.browser.lightning.di.configPrefs
import acr.browser.lightning.extensions.inflater
import acr.browser.lightning.settings.preferences.UserPreferences
import acr.browser.lightning.utils.ItemDragDropSwipeHelper
import acr.browser.lightning.utils.fixScrollBug
import acr.browser.lightning.view.LightningView
import acr.browser.lightning.view.WebPageTab
import android.content.Context
import android.util.AttributeSet
import android.view.View
Expand All @@ -18,7 +17,6 @@ import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import javax.inject.Inject


/**
Expand Down Expand Up @@ -114,7 +112,7 @@ class TabsDrawerView @JvmOverloads constructor(
* TODO: this is called way to often for my taste and should be optimized somehow.
*/
private fun displayTabs() {
tabsAdapter.showTabs(uiController.getTabModel().allTabs.map(LightningView::asTabViewState))
tabsAdapter.showTabs(uiController.getTabModel().allTabs.map(WebPageTab::asTabViewState))

if (fixScrollBug(iBinding.tabsList)) {
// Scroll bug was fixed trigger a scroll to current item then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import acr.browser.lightning.browser.TabsManager
import acr.browser.lightning.database.Bookmark
import acr.browser.lightning.dialog.LightningDialogBuilder
import acr.browser.lightning.ssl.SslState
import acr.browser.lightning.view.LightningView
import acr.browser.lightning.view.WebPageTab
import android.content.pm.ActivityInfo
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
Expand Down Expand Up @@ -110,7 +110,7 @@ interface UIController {
* Notify the browser that the website currently being displayed by the [tab] wants to be
* closed.
*/
fun onCloseWindow(tab: LightningView)
fun onCloseWindow(tab: WebPageTab)

/**
* Hide the search bar from view via animation.
Expand Down Expand Up @@ -170,17 +170,17 @@ interface UIController {
/**
* Notify the UI that the [aTab] should be displayed.
*/
fun onTabChanged(aTab: LightningView)
fun onTabChanged(aTab: WebPageTab)

/**
*
*/
fun onTabChangedIcon(aTab: LightningView)
fun onTabChangedIcon(aTab: WebPageTab)

/**
*
*/
fun onTabChangedTitle(aTab: LightningView)
fun onTabChangedTitle(aTab: WebPageTab)

/**
* Notify the browser that the user pressed the back button.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import acr.browser.lightning.settings.preferences.UserPreferences;
import acr.browser.lightning.utils.FileUtils;
import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.view.LightningView;
import acr.browser.lightning.view.WebPageTab;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -305,7 +305,7 @@ private void onDownloadStartNoStream(@NonNull final Activity context, @NonNull U

// save download in database
UIController browserActivity = (UIController) context;
LightningView view = browserActivity.getTabModel().getCurrentTab();
WebPageTab view = browserActivity.getTabModel().getCurrentTab();

if (view != null && !view.isIncognito()) {
downloadsRepository.addDownloadIfNotExists(new DownloadEntry(url, iFilename, contentSize))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package acr.browser.lightning.settings.fragment

import acr.browser.lightning.BuildConfig
import acr.browser.lightning.Capabilities
import acr.browser.lightning.R
import acr.browser.lightning.database.history.HistoryRepository
Expand All @@ -12,7 +11,7 @@ import acr.browser.lightning.extensions.snackbar
import acr.browser.lightning.isSupported
import acr.browser.lightning.settings.preferences.UserPreferences
import acr.browser.lightning.utils.WebUtils
import acr.browser.lightning.view.LightningView
import acr.browser.lightning.view.WebPageTab
import android.app.Activity
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -123,7 +122,7 @@ class PrivacySettingsFragment : AbstractSettingsFragment() {
switchPreference(
preference = SETTINGS_IDENTIFYINGHEADERS,
isChecked = userPreferences.removeIdentifyingHeadersEnabled,
summary = "${LightningView.HEADER_REQUESTED_WITH}, ${LightningView.HEADER_WAP_PROFILE}",
summary = "${WebPageTab.HEADER_REQUESTED_WITH}, ${WebPageTab.HEADER_WAP_PROFILE}",
onCheckChange = { userPreferences.removeIdentifyingHeadersEnabled = it }
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import io.reactivex.rxkotlin.subscribeBy
import javax.inject.Inject

/**
* An initializer that is run on a [LightningView] after it is created.
* An initializer that is run on a [WebPageTab] after it is created.
*/
interface TabInitializer {

/**
* Initialize the [WebView] instance held by the [LightningView]. If a url is loaded, the
* Initialize the [WebView] instance held by the [WebPageTab]. If a url is loaded, the
* provided [headers] should be used to load the url.
*/
fun initialize(webView: WebView, headers: Map<String, String>)
Expand Down Expand Up @@ -213,7 +213,7 @@ class ResultMessageInitializer(private val resultMessage: Message) : TabInitiali

override fun url(): String {
/** We don't know our URL at this stage, it will only be loaded in the WebView by whatever is handling the message sent above.
* That's ok though as we implemented a special case to handle this situation in [LightningView.initializeContent]
* That's ok though as we implemented a special case to handle this situation in [WebPageTab.initializeContent]
*/
return ""
}
Expand Down
Loading

0 comments on commit b8c9b98

Please sign in to comment.