ShareFragment.kt 3.81 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
import mozilla.components.feature.sendtab.SendTabUseCases
20
import org.mozilla.fenix.R
21
22
import org.mozilla.fenix.components.FenixSnackbarPresenter
import org.mozilla.fenix.ext.getRootView
23
import org.mozilla.fenix.ext.requireComponents
24

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

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

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

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

45
46
47
48
49
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
50
        val view = inflater.inflate(R.layout.fragment_share, container, false)
51
        val args by navArgs<ShareFragmentArgs>()
52
        val shareData = args.data.toList()
53

54
        val accountManager = requireComponents.backgroundServices.accountManager
55

56
57
        shareInteractor = ShareInteractor(
            DefaultShareController(
58
                context = requireContext(),
59
                shareData = shareData,
60
                snackbarPresenter = FenixSnackbarPresenter(activity!!.getRootView()!!),
61
                navController = findNavController(),
62
                sendTabUseCases = SendTabUseCases(accountManager),
63
64
65
                dismiss = ::dismiss
            )
        )
66

67
        view.shareWrapper.setOnClickListener { shareInteractor.onShareClosed() }
68
69
        shareToAccountDevicesView =
            ShareToAccountDevicesView(view.devicesShareLayout, shareInteractor)
70

71
72
73
        if (args.showPage) {
            // Show the previous fragment underneath the share background scrim
            // by making it translucent.
74
75
76
77
78
79
            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)
80
            shareCloseView.setTabs(shareData)
81
        }
82
        shareToAppsView = ShareToAppsView(view.appsShareLayout, shareInteractor)
83

84
        return view
85
    }
86
87
88

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

    companion object {
        const val SHOW_PAGE_ALPHA = 0.6f
    }
100
}