SessionControlController.kt 14.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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 org.mozilla.fenix.home.sessioncontrol

import android.view.View
import androidx.navigation.NavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.browser.session.SessionManager
13
import mozilla.components.browser.state.store.BrowserStore
14
15
16
17
import mozilla.components.concept.engine.prompt.ShareData
import mozilla.components.feature.media.ext.pauseIfPlaying
import mozilla.components.feature.media.ext.playIfPaused
import mozilla.components.feature.tab.collections.TabCollection
18
import mozilla.components.feature.tab.collections.ext.restore
19
import mozilla.components.feature.top.sites.TopSite
20
21
22
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
23
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
24
25
26
import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager
import org.mozilla.fenix.collections.SaveCollectionStep
import org.mozilla.fenix.components.TabCollectionStorage
27
import org.mozilla.fenix.components.TopSiteStorage
28
29
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
30
import org.mozilla.fenix.components.tips.Tip
ekager's avatar
ekager committed
31
import org.mozilla.fenix.ext.components
32
33
34
35
36
37
38
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.sessionsOfType
import org.mozilla.fenix.home.HomeFragment
import org.mozilla.fenix.home.HomeFragmentAction
import org.mozilla.fenix.home.HomeFragmentDirections
import org.mozilla.fenix.home.HomeFragmentStore
import org.mozilla.fenix.home.Tab
39
import org.mozilla.fenix.ext.settings
40
import org.mozilla.fenix.settings.SupportUtils
ekager's avatar
ekager committed
41
import mozilla.components.feature.tab.collections.Tab as ComponentTab
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

/**
 * [HomeFragment] controller. An interface that handles the view manipulation of the Tabs triggered
 * by the Interactor.
 */
@SuppressWarnings("TooManyFunctions")
interface SessionControlController {
    /**
     * @see [TabSessionInteractor.onCloseTab]
     */
    fun handleCloseTab(sessionId: String)

    /**
     * @see [TabSessionInteractor.onCloseAllTabs]
     */
    fun handleCloseAllTabs(isPrivateMode: Boolean)

    /**
     * @see [CollectionInteractor.onCollectionAddTabTapped]
     */
    fun handleCollectionAddTabTapped(collection: TabCollection)

    /**
     * @see [CollectionInteractor.onCollectionOpenTabClicked]
     */
    fun handleCollectionOpenTabClicked(tab: ComponentTab)

    /**
     * @see [CollectionInteractor.onCollectionOpenTabsTapped]
     */
    fun handleCollectionOpenTabsTapped(collection: TabCollection)

    /**
     * @see [CollectionInteractor.onCollectionRemoveTab]
     */
    fun handleCollectionRemoveTab(collection: TabCollection, tab: ComponentTab)

    /**
     * @see [CollectionInteractor.onCollectionShareTabsClicked]
     */
    fun handleCollectionShareTabsClicked(collection: TabCollection)

    /**
     * @see [CollectionInteractor.onDeleteCollectionTapped]
     */
    fun handleDeleteCollectionTapped(collection: TabCollection)

89
90
91
92
93
    /**
     * @see [TopSiteInteractor.onOpenInPrivateTabClicked]
     */
    fun handleOpenInPrivateTabClicked(topSite: TopSite)

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    /**
     * @see [TabSessionInteractor.onPauseMediaClicked]
     */
    fun handlePauseMediaClicked()

    /**
     * @see [TabSessionInteractor.onPlayMediaClicked]
     */
    fun handlePlayMediaClicked()

    /**
     * @see [TabSessionInteractor.onPrivateBrowsingLearnMoreClicked]
     */
    fun handlePrivateBrowsingLearnMoreClicked()

109
110
111
112
113
    /**
     * @see [TopSiteInteractor.onRemoveTopSiteClicked]
     */
    fun handleRemoveTopSiteClicked(topSite: TopSite)

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    /**
     * @see [CollectionInteractor.onRenameCollectionTapped]
     */
    fun handleRenameCollectionTapped(collection: TabCollection)

    /**
     * @see [TabSessionInteractor.onSaveToCollection]
     */
    fun handleSaveTabToCollection(selectedTabId: String?)

    /**
     * @see [TabSessionInteractor.onSelectTab]
     */
    fun handleSelectTab(tabView: View, sessionId: String)

129
130
131
    /**
     * @see [TopSiteInteractor.onSelectTopSite]
     */
132
    fun handleSelectTopSite(url: String, isDefault: Boolean)
133

134
135
136
137
138
139
140
141
142
143
    /**
     * @see [TabSessionInteractor.onShareTabs]
     */
    fun handleShareTabs()

    /**
     * @see [OnboardingInteractor.onStartBrowsingClicked]
     */
    fun handleStartBrowsingClicked()

144
145
146
147
148
    /**
     * @see [OnboardingInteractor.onOpenSettingsClicked]
     */
    fun handleOpenSettingsClicked()

149
150
151
152
153
154
155
156
157
158
    /**
     * @see [OnboardingInteractor.onWhatsNewGetAnswersClicked]
     */
    fun handleWhatsNewGetAnswersClicked()

    /**
     * @see [OnboardingInteractor.onReadPrivacyNoticeClicked]
     */
    fun handleReadPrivacyNoticeClicked()

159
160
161
162
    /**
     * @see [CollectionInteractor.onToggleCollectionExpanded]
     */
    fun handleToggleCollectionExpanded(collection: TabCollection, expand: Boolean)
163
164
165
166
167

    /**
     * @see [TabSessionInteractor.onOpenNewTabClicked]
     */
    fun handleonOpenNewTabClicked()
168
169

    fun handleCloseTip(tip: Tip)
170
171
}

172
@SuppressWarnings("TooManyFunctions", "LargeClass")
173
class DefaultSessionControlController(
174
    private val store: BrowserStore,
175
    private val activity: HomeActivity,
176
    private val fragmentStore: HomeFragmentStore,
177
178
    private val navController: NavController,
    private val browsingModeManager: BrowsingModeManager,
179
    private val viewLifecycleScope: CoroutineScope,
180
181
182
183
184
185
186
    private val closeTab: (sessionId: String) -> Unit,
    private val closeAllTabs: (isPrivateMode: Boolean) -> Unit,
    private val getListOfTabs: () -> List<Tab>,
    private val hideOnboarding: () -> Unit,
    private val invokePendingDeleteJobs: () -> Unit,
    private val registerCollectionStorageObserver: () -> Unit,
    private val scrollToTheTop: () -> Unit,
187
    private val showDeleteCollectionPrompt: (tabCollection: TabCollection) -> Unit,
188
    private val openSettingsScreen: () -> Unit,
189
190
    private val openSearchScreen: () -> Unit,
    private val openWhatsNewLink: () -> Unit,
191
192
    private val openPrivacyNotice: () -> Unit,
    private val showTabTray: () -> Unit
193
194
195
196
197
198
199
) : SessionControlController {
    private val metrics: MetricController
        get() = activity.components.analytics.metrics
    private val sessionManager: SessionManager
        get() = activity.components.core.sessionManager
    private val tabCollectionStorage: TabCollectionStorage
        get() = activity.components.core.tabCollectionStorage
200
201
    private val topSiteStorage: TopSiteStorage
        get() = activity.components.core.topSiteStorage
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    override fun handleCloseTab(sessionId: String) {
        closeTab.invoke(sessionId)
    }

    override fun handleCloseAllTabs(isPrivateMode: Boolean) {
        closeAllTabs.invoke(isPrivateMode)
    }

    override fun handleCollectionAddTabTapped(collection: TabCollection) {
        metrics.track(Event.CollectionAddTabPressed)
        showCollectionCreationFragment(
            step = SaveCollectionStep.SelectTabs,
            selectedTabCollectionId = collection.id
        )
    }

    override fun handleCollectionOpenTabClicked(tab: ComponentTab) {
        invokePendingDeleteJobs()

222
223
224
225
226
227
228
229
230
231
232
233
234
235
        sessionManager.restore(
            activity,
            activity.components.core.engine,
            tab,
            onTabRestored = {
                activity.openToBrowser(BrowserDirection.FromHome)
            },
            onFailure = {
                activity.openToBrowserAndLoad(
                    searchTermOrURL = tab.url,
                    newTab = true,
                    from = BrowserDirection.FromHome
                )
            }
236
237
238
239
240
241
242
243
        )

        metrics.track(Event.CollectionTabRestored)
    }

    override fun handleCollectionOpenTabsTapped(collection: TabCollection) {
        invokePendingDeleteJobs()

244
245
246
247
248
249
        sessionManager.restore(
            activity,
            activity.components.core.engine,
            collection,
            onFailure = { url ->
                activity.components.useCases.tabsUseCases.addTab.invoke(url)
250
            }
251
        )
252

253
254
255
256
257
258
        if (activity.settings().useNewTabTray) {
            showTabTray()
        } else {
            scrollToTheTop()
        }

259
260
261
262
263
264
        metrics.track(Event.CollectionAllTabsRestored)
    }

    override fun handleCollectionRemoveTab(collection: TabCollection, tab: ComponentTab) {
        metrics.track(Event.CollectionTabRemoved)

265
        viewLifecycleScope.launch(Dispatchers.IO) {
266
267
268
269
270
271
272
273
274
275
276
277
278
            tabCollectionStorage.removeTabFromCollection(collection, tab)
        }
    }

    override fun handleCollectionShareTabsClicked(collection: TabCollection) {
        showShareFragment(collection.tabs.map { ShareData(url = it.url, title = it.title) })
        metrics.track(Event.CollectionShared)
    }

    override fun handleDeleteCollectionTapped(collection: TabCollection) {
        showDeleteCollectionPrompt(collection)
    }

279
    override fun handleOpenInPrivateTabClicked(topSite: TopSite) {
280
        metrics.track(Event.TopSiteOpenInPrivateTab)
281
282
283
284
285
286
287
288
289
290
        with(activity) {
            browsingModeManager.mode = BrowsingMode.Private
            openToBrowserAndLoad(
                searchTermOrURL = topSite.url,
                newTab = true,
                from = BrowserDirection.FromHome
            )
        }
    }

291
    override fun handlePauseMediaClicked() {
292
        store.state.media.pauseIfPlaying()
293
294
295
    }

    override fun handlePlayMediaClicked() {
296
        store.state.media.playIfPaused()
297
298
299
300
301
302
303
304
305
306
307
    }

    override fun handlePrivateBrowsingLearnMoreClicked() {
        activity.openToBrowserAndLoad(
            searchTermOrURL = SupportUtils.getGenericSumoURLForTopic
                (SupportUtils.SumoTopic.PRIVATE_BROWSING_MYTHS),
            newTab = true,
            from = BrowserDirection.FromHome
        )
    }

308
    override fun handleRemoveTopSiteClicked(topSite: TopSite) {
309
        metrics.track(Event.TopSiteRemoved)
ekager's avatar
ekager committed
310
311
312
        if (topSite.url == SupportUtils.POCKET_TRENDING_URL) {
            metrics.track(Event.PocketTopSiteRemoved)
        }
313

314
        viewLifecycleScope.launch(Dispatchers.IO) {
315
316
317
318
            topSiteStorage.removeTopSite(topSite)
        }
    }

319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
    override fun handleRenameCollectionTapped(collection: TabCollection) {
        showCollectionCreationFragment(
            step = SaveCollectionStep.RenameCollection,
            selectedTabCollectionId = collection.id
        )
        metrics.track(Event.CollectionRenamePressed)
    }

    override fun handleSaveTabToCollection(selectedTabId: String?) {
        if (browsingModeManager.mode.isPrivate) return

        invokePendingDeleteJobs()

        val tabs = getListOfTabs()
        val step = when {
            // Show the SelectTabs fragment if there are multiple opened tabs to select which tabs
            // you want to save to a collection.
            tabs.size > 1 -> SaveCollectionStep.SelectTabs
            // If there is an existing tab collection, show the SelectCollection fragment to save
            // the selected tab to a collection of your choice.
            tabCollectionStorage.cachedTabCollections.isNotEmpty() -> SaveCollectionStep.SelectCollection
            // Show the NameCollection fragment to create a new collection for the selected tab.
            else -> SaveCollectionStep.NameCollection
        }

        showCollectionCreationFragment(step, selectedTabId?.let { arrayOf(it) })
    }

    override fun handleSelectTab(tabView: View, sessionId: String) {
        invokePendingDeleteJobs()
        val session = sessionManager.findSessionById(sessionId)
        sessionManager.select(session!!)
351
        activity.openToBrowser(BrowserDirection.FromHome)
352
353
    }

354
    override fun handleSelectTopSite(url: String, isDefault: Boolean) {
355
        invokePendingDeleteJobs()
356
        metrics.track(Event.TopSiteOpenInNewTab)
357
        if (isDefault) { metrics.track(Event.TopSiteOpenDefault) }
358
359
360
361
362
        if (url == SupportUtils.POCKET_TRENDING_URL) { metrics.track(Event.PocketTopSiteClicked) }
        activity.components.useCases.tabsUseCases.addTab.invoke(
            url = url,
            selectTab = true,
            startLoading = true
363
        )
364
        activity.openToBrowser(BrowserDirection.FromHome)
365
366
    }

367
368
369
370
371
372
373
374
375
376
377
378
379
    override fun handleShareTabs() {
        invokePendingDeleteJobs()
        val shareData = sessionManager
            .sessionsOfType(private = browsingModeManager.mode.isPrivate)
            .map { ShareData(url = it.url, title = it.title) }
            .toList()
        showShareFragment(shareData)
    }

    override fun handleStartBrowsingClicked() {
        hideOnboarding()
    }

380
381
382
383
    override fun handleOpenSettingsClicked() {
        openSettingsScreen()
    }

384
385
386
387
388
389
390
391
    override fun handleWhatsNewGetAnswersClicked() {
        openWhatsNewLink()
    }

    override fun handleReadPrivacyNoticeClicked() {
        openPrivacyNotice()
    }

392
    override fun handleToggleCollectionExpanded(collection: TabCollection, expand: Boolean) {
393
        fragmentStore.dispatch(HomeFragmentAction.CollectionExpanded(collection, expand))
394
395
    }

396
397
398
399
    override fun handleonOpenNewTabClicked() {
        openSearchScreen()
    }

400
401
402
403
    override fun handleCloseTip(tip: Tip) {
        fragmentStore.dispatch(HomeFragmentAction.RemoveTip(tip))
    }

404
405
406
407
408
409
410
411
412
413
414
    private fun showCollectionCreationFragment(
        step: SaveCollectionStep,
        selectedTabIds: Array<String>? = null,
        selectedTabCollectionId: Long? = null
    ) {
        if (navController.currentDestination?.id == R.id.collectionCreationFragment) return

        // Only register the observer right before moving to collection creation
        registerCollectionStorageObserver()

        val tabIds = getListOfTabs().map { it.sessionId }.toTypedArray()
415
        val directions = HomeFragmentDirections.actionGlobalCollectionCreationFragment(
416
417
418
419
420
421
422
423
424
            tabIds = tabIds,
            saveCollectionStep = step,
            selectedTabIds = selectedTabIds,
            selectedTabCollectionId = selectedTabCollectionId ?: -1
        )
        navController.nav(R.id.homeFragment, directions)
    }

    private fun showShareFragment(data: List<ShareData>) {
Jeff Boek's avatar
Jeff Boek committed
425
        val directions = HomeFragmentDirections.actionGlobalShareFragment(
426
427
428
429
430
431
432
433
434
            data = data.toTypedArray()
        )
        navController.nav(R.id.homeFragment, directions)
    }

    companion object {
        private const val TAB_ITEM_TRANSITION_NAME = "tab_item"
    }
}