ShareFragment.kt 5.62 KB
Newer Older
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
3
4
5
 * 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.share
6

7
import android.content.Context
8
9
10
11
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
12
import androidx.appcompat.app.AppCompatDialogFragment
Tiger Oakes's avatar
Tiger Oakes committed
13
14
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory
Mihai Branescu's avatar
Mihai Branescu committed
15
import androidx.lifecycle.lifecycleScope
Tiger Oakes's avatar
Tiger Oakes committed
16
import androidx.lifecycle.observe
17
import androidx.navigation.fragment.findNavController
18
import androidx.navigation.fragment.navArgs
19
import kotlinx.android.synthetic.main.fragment_share.view.*
20
21
22
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.selector.findTabOrCustomTab
import mozilla.components.concept.engine.prompt.PromptRequest
23
import mozilla.components.feature.accounts.push.SendTabUseCases
Mihai Branescu's avatar
Mihai Branescu committed
24
import mozilla.components.feature.share.RecentAppsStorage
25
import org.mozilla.fenix.R
26
import org.mozilla.fenix.components.FenixSnackbar
27
import org.mozilla.fenix.ext.getRootView
28
import org.mozilla.fenix.ext.requireComponents
29

30
class ShareFragment : AppCompatDialogFragment() {
Tiger Oakes's avatar
Tiger Oakes committed
31

32
    private val args by navArgs<ShareFragmentArgs>()
Tiger Oakes's avatar
Tiger Oakes committed
33
34
35
    private val viewModel: ShareViewModel by viewModels {
        AndroidViewModelFactory(requireActivity().application)
    }
36
37
38
39
    private lateinit var shareInteractor: ShareInteractor
    private lateinit var shareCloseView: ShareCloseView
    private lateinit var shareToAccountDevicesView: ShareToAccountDevicesView
    private lateinit var shareToAppsView: ShareToAppsView
40

41
42
    override fun onAttach(context: Context) {
        super.onAttach(context)
Tiger Oakes's avatar
Tiger Oakes committed
43
        viewModel.loadDevicesAndApps()
44
45
    }

46
47
48
49
50
    override fun dismiss() {
        consumePrompt { onDismiss() }
        super.dismiss()
    }

51
52
53
54
55
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.ShareDialogStyle)
    }

56
57
58
59
60
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
61
        val view = inflater.inflate(R.layout.fragment_share, container, false)
62
        val shareData = args.data.toList()
63

64
        val accountManager = requireComponents.backgroundServices.accountManager
65

66
67
        shareInteractor = ShareInteractor(
            DefaultShareController(
68
                context = requireContext(),
69
                shareData = shareData,
70
71
72
73
                snackbar = FenixSnackbar.make(
                    view = requireActivity().getRootView()!!,
                    isDisplayedOnBrowserFragment = true
                ),
74
                navController = findNavController(),
Mihai Branescu's avatar
Mihai Branescu committed
75
76
77
                sendTabUseCases = SendTabUseCases(accountManager),
                recentAppsStorage = RecentAppsStorage(requireContext()),
                lifecycleScope = lifecycleScope
78
79
80
81
82
83
84
85
86
87
            ) { result ->
                consumePrompt {
                    when (result) {
                        ShareController.Result.DISMISSED -> onDismiss()
                        ShareController.Result.SHARE_ERROR -> onFailure()
                        ShareController.Result.SUCCESS -> onSuccess()
                    }
                }
                super.dismiss()
            }
88
        )
89

90
        view.shareWrapper.setOnClickListener { shareInteractor.onShareClosed() }
91
92
        shareToAccountDevicesView =
            ShareToAccountDevicesView(view.devicesShareLayout, shareInteractor)
93

94
95
96
        if (args.showPage) {
            // Show the previous fragment underneath the share background scrim
            // by making it translucent.
97
98
99
100
101
102
            view.closeSharingScrim.alpha = SHOW_PAGE_ALPHA
            view.shareWrapper.setOnClickListener { shareInteractor.onShareClosed() }
        } else {
            // Otherwise, show a list of tabs to share.
            view.closeSharingScrim.alpha = 1.0f
            shareCloseView = ShareCloseView(view.closeSharingContent, shareInteractor)
103
            shareCloseView.setTabs(shareData)
104
        }
105
        shareToAppsView = ShareToAppsView(view.appsShareLayout, shareInteractor)
106

107
        return view
108
    }
109
110
111

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
Tiger Oakes's avatar
Tiger Oakes committed
112
        viewModel.devicesList.observe(viewLifecycleOwner) { devicesShareOptions ->
113
            shareToAccountDevicesView.setShareTargets(devicesShareOptions)
114
        }
Tiger Oakes's avatar
Tiger Oakes committed
115
116
        viewModel.appsList.observe(viewLifecycleOwner) { appsToShareTo ->
            shareToAppsView.setShareTargets(appsToShareTo)
117
        }
Mihai Branescu's avatar
Mihai Branescu committed
118
119
120
        viewModel.recentAppsList.observe(viewLifecycleOwner) { appsToShareTo ->
            shareToAppsView.setRecentShareTargets(appsToShareTo)
        }
121
    }
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    /**
     * If [ShareFragmentArgs.sessionId] is set and the session has a pending Web Share
     * prompt request, call [consume] then clean up the prompt.
     */
    private fun consumePrompt(
        consume: PromptRequest.Share.() -> Unit
    ) {
        val browserStore = requireComponents.core.store
        args.sessionId
            ?.let { sessionId -> browserStore.state.findTabOrCustomTab(sessionId) }
            ?.let { tab ->
                val promptRequest = tab.content.promptRequest
                if (promptRequest is PromptRequest.Share) {
                    consume(promptRequest)
                    browserStore.dispatch(ContentAction.ConsumePromptRequestAction(tab.id))
                }
            }
    }

142
143
144
    companion object {
        const val SHOW_PAGE_ALPHA = 0.6f
    }
145
}