ShareFragment.kt 5.17 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
15
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory
import androidx.lifecycle.observe
16
import androidx.navigation.fragment.findNavController
17
import androidx.navigation.fragment.navArgs
18
import kotlinx.android.synthetic.main.fragment_share.view.*
19
20
21
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.selector.findTabOrCustomTab
import mozilla.components.concept.engine.prompt.PromptRequest
22
import mozilla.components.feature.accounts.push.SendTabUseCases
23
import org.mozilla.fenix.R
24
import org.mozilla.fenix.components.FenixSnackbar
25
import org.mozilla.fenix.ext.getRootView
26
import org.mozilla.fenix.ext.requireComponents
27

28
class ShareFragment : AppCompatDialogFragment() {
Tiger Oakes's avatar
Tiger Oakes committed
29

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

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

44
45
46
47
48
    override fun dismiss() {
        consumePrompt { onDismiss() }
        super.dismiss()
    }

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

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

62
        val accountManager = requireComponents.backgroundServices.accountManager
63

64
65
        shareInteractor = ShareInteractor(
            DefaultShareController(
66
                context = requireContext(),
67
                shareData = shareData,
68
                snackbar = FenixSnackbar.makeWithToolbarPadding(requireActivity().getRootView()!!),
69
                navController = findNavController(),
70
71
72
73
74
75
76
77
78
79
80
                sendTabUseCases = SendTabUseCases(accountManager)
            ) { result ->
                consumePrompt {
                    when (result) {
                        ShareController.Result.DISMISSED -> onDismiss()
                        ShareController.Result.SHARE_ERROR -> onFailure()
                        ShareController.Result.SUCCESS -> onSuccess()
                    }
                }
                super.dismiss()
            }
81
        )
82

83
        view.shareWrapper.setOnClickListener { shareInteractor.onShareClosed() }
84
85
        shareToAccountDevicesView =
            ShareToAccountDevicesView(view.devicesShareLayout, shareInteractor)
86

87
88
89
        if (args.showPage) {
            // Show the previous fragment underneath the share background scrim
            // by making it translucent.
90
91
92
93
94
95
            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)
96
            shareCloseView.setTabs(shareData)
97
        }
98
        shareToAppsView = ShareToAppsView(view.appsShareLayout, shareInteractor)
99

100
        return view
101
    }
102
103
104

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
Tiger Oakes's avatar
Tiger Oakes committed
105
        viewModel.devicesList.observe(viewLifecycleOwner) { devicesShareOptions ->
106
            shareToAccountDevicesView.setShareTargets(devicesShareOptions)
107
        }
Tiger Oakes's avatar
Tiger Oakes committed
108
109
        viewModel.appsList.observe(viewLifecycleOwner) { appsToShareTo ->
            shareToAppsView.setShareTargets(appsToShareTo)
110
111
        }
    }
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    /**
     * 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))
                }
            }
    }

132
133
134
    companion object {
        const val SHOW_PAGE_ALPHA = 0.6f
    }
135
}