ShareFragment.kt 5.35 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
    private lateinit var shareInteractor: ShareInteractor
    private lateinit var shareCloseView: ShareCloseView
    private lateinit var shareToAppsView: ShareToAppsView
39

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

45
46
47
48
49
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.ShareDialogStyle)
    }

50
51
    override fun onPause() {
        super.onPause()
52
        consumePrompt { onDismiss() }
53
54
55
        dismiss()
    }

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
                shareSubject = args.shareSubject,
70
                shareData = shareData,
71
72
                snackbar = FenixSnackbar.make(
                    view = requireActivity().getRootView()!!,
73
                    isDisplayedWithBrowserToolbar = true
74
                ),
75
                navController = findNavController(),
Mihai Branescu's avatar
Mihai Branescu committed
76
77
                sendTabUseCases = SendTabUseCases(accountManager),
                recentAppsStorage = RecentAppsStorage(requireContext()),
78
                viewLifecycleScope = viewLifecycleOwner.lifecycleScope
79
80
81
82
83
84
85
86
87
88
            ) { result ->
                consumePrompt {
                    when (result) {
                        ShareController.Result.DISMISSED -> onDismiss()
                        ShareController.Result.SHARE_ERROR -> onFailure()
                        ShareController.Result.SUCCESS -> onSuccess()
                    }
                }
                super.dismiss()
            }
89
        )
90

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

105
        return view
106
    }
107
108
109

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

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    /**
     * 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))
                }
            }
    }

137
138
139
    companion object {
        const val SHOW_PAGE_ALPHA = 0.6f
    }
140
}