ShareFragment.kt 4.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.mozilla.fenix.share

/* This Source Code Form is subject to the terms of the Mozilla Public
   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/. */

import android.content.Intent
import android.content.Intent.ACTION_SEND
import android.content.Intent.EXTRA_TEXT
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
15
16
17
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.navigation.fragment.NavHostFragment.findNavController
import kotlinx.android.synthetic.main.component_share.*
18
19
20
21
import kotlinx.android.synthetic.main.fragment_share.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
22
import mozilla.components.concept.sync.DeviceEventOutgoing
23
24
25
26
27
28
29
import org.mozilla.fenix.FenixViewModelProvider
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.getAutoDisposeObservable
import kotlin.coroutines.CoroutineContext

30
class ShareFragment : AppCompatDialogFragment(), CoroutineScope {
31
32
33
34
35
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job
    private lateinit var job: Job
    private lateinit var component: ShareComponent
    private lateinit var url: String
36
    private lateinit var title: String
37
38
39
40
41
42
43
44
45
46
47
48

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.CreateCollectionDialogStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_share, container, false)
        val args = ShareFragmentArgs.fromBundle(arguments!!)

        job = Job()
        url = args.url
49
        title = args.title ?: ""
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        component = ShareComponent(
            view.share_wrapper,
            ActionBusFactory.get(this),
            FenixViewModelProvider.create(
                this,
                ShareUIViewModel::class.java
            ) {
                ShareUIViewModel(ShareState)
            }
        )

        return view
    }

    override fun onResume() {
        super.onResume()
        subscribeToActions()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        job.cancel()
    }

74
    @SuppressWarnings("ComplexMethod")
75
76
77
78
79
80
    private fun subscribeToActions() {
        getAutoDisposeObservable<ShareAction>().subscribe {
            when (it) {
                ShareAction.Close -> {
                    dismiss()
                }
81
82
83
84
                ShareAction.SignInClicked -> {
                    val directions = ShareFragmentDirections.actionShareFragmentToTurnOnSyncFragment()
                    findNavController(this@ShareFragment).navigate(directions)
                }
85
86
87
                ShareAction.AddNewDeviceClicked -> {
                    requireComponents.useCases.tabsUseCases.addTab.invoke(ADD_NEW_DEVICES_URL, true)
                }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
                ShareAction.HideSendTab -> {
                    send_tab_group.visibility = View.GONE
                }
                is ShareAction.ShareDeviceClicked -> {
                    val authAccount = requireComponents.backgroundServices.accountManager.authenticatedAccount()
                    authAccount?.run {
                        deviceConstellation().sendEventToDeviceAsync(
                            it.device.id,
                            DeviceEventOutgoing.SendTab(title, url)
                        )
                    }
                }
                is ShareAction.SendAllClicked -> {
                    val authAccount = requireComponents.backgroundServices.accountManager.authenticatedAccount()
                    authAccount?.run {
                        it.devices.forEach { device ->
                            deviceConstellation().sendEventToDeviceAsync(
                                device.id,
                                DeviceEventOutgoing.SendTab(title, url)
                            )
                        }
                    }
                }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
                is ShareAction.ShareAppClicked -> {
                    val intent = Intent(ACTION_SEND).apply {
                        putExtra(EXTRA_TEXT, url)
                        type = "text/plain"
                        flags = FLAG_ACTIVITY_NEW_TASK
                        `package` = it.packageName
                    }
                    startActivity(intent)
                }
            }
            dismiss()
        }
    }

    companion object {
126
        // TODO Replace this link with the correct one when provided.
127
128
129
        const val ADD_NEW_DEVICES_URL = "https://accounts.firefox.com/connect_another_device"
    }
}