ShareFragment.kt 4.22 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
import android.os.Bundle
9
import android.os.Parcelable
10
11
12
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
13
import androidx.appcompat.app.AppCompatDialogFragment
Tiger Oakes's avatar
Tiger Oakes committed
14
15
16
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory
import androidx.lifecycle.observe
17
import androidx.navigation.fragment.findNavController
18
import androidx.navigation.fragment.navArgs
19
import kotlinx.android.parcel.Parcelize
20
import kotlinx.android.synthetic.main.fragment_share.view.*
21
import mozilla.components.feature.sendtab.SendTabUseCases
22
import org.mozilla.fenix.R
23
24
import org.mozilla.fenix.components.FenixSnackbarPresenter
import org.mozilla.fenix.ext.getRootView
25
import org.mozilla.fenix.ext.requireComponents
26

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

    private val viewModel: ShareViewModel by viewModels {
        AndroidViewModelFactory(requireActivity().application)
    }
32
33
34
35
    private lateinit var shareInteractor: ShareInteractor
    private lateinit var shareCloseView: ShareCloseView
    private lateinit var shareToAccountDevicesView: ShareToAccountDevicesView
    private lateinit var shareToAppsView: ShareToAppsView
36

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

42
43
44
45
46
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.ShareDialogStyle)
    }

47
48
49
50
51
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
52
        val view = inflater.inflate(R.layout.fragment_share, container, false)
53
        val args by navArgs<ShareFragmentArgs>()
54
        check(!(args.url == null && args.tabs.isNullOrEmpty())) { "URL and tabs cannot both be null." }
55

Tiger Oakes's avatar
Tiger Oakes committed
56
        val tabs = args.tabs?.toList() ?: listOf(ShareTab(args.url!!, args.title.orEmpty()))
57
        val accountManager = requireComponents.backgroundServices.accountManager
58

59
60
        shareInteractor = ShareInteractor(
            DefaultShareController(
61
                context = requireContext(),
62
                sharedTabs = tabs,
63
                snackbarPresenter = FenixSnackbarPresenter(activity!!.getRootView()!!),
64
                navController = findNavController(),
65
                sendTabUseCases = SendTabUseCases(accountManager),
66
67
68
                dismiss = ::dismiss
            )
        )
69

70
        view.shareWrapper.setOnClickListener { shareInteractor.onShareClosed() }
71
72
        shareToAccountDevicesView =
            ShareToAccountDevicesView(view.devicesShareLayout, shareInteractor)
73
74
75
76
77
78
79
80
81
82
83
84
85

        if (args.url != null && args.tabs == null) {
            // If sharing one tab from the browser fragment, show it.
            // If URL is set and tabs is null, we assume the browser is visible, since navigation
            // does not tell us the back stack state.
            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)
            shareCloseView.setTabs(tabs)
        }
86
        shareToAppsView = ShareToAppsView(view.appsShareLayout, shareInteractor)
87

88
        return view
89
    }
90
91
92

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
Tiger Oakes's avatar
Tiger Oakes committed
93
        viewModel.devicesList.observe(viewLifecycleOwner) { devicesShareOptions ->
94
            shareToAccountDevicesView.setShareTargets(devicesShareOptions)
95
        }
Tiger Oakes's avatar
Tiger Oakes committed
96
97
        viewModel.appsList.observe(viewLifecycleOwner) { appsToShareTo ->
            shareToAppsView.setShareTargets(appsToShareTo)
98
99
        }
    }
100
101
102
103

    companion object {
        const val SHOW_PAGE_ALPHA = 0.6f
    }
104
}
105
106

@Parcelize
107
data class ShareTab(val url: String, val title: String) : Parcelable