summaryrefslogtreecommitdiffstats
path: root/mobile/android/fenix/app/src/main/java/org/mozilla/fenix/home/topsites/TopSiteItemMenu.kt
blob: 3826da5c613df4e3025225cbc2f6b3eb381a090f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* 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/. */

package org.mozilla.fenix.home.topsites

import android.content.Context
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.R

/**
 * Helper class for building a context menu for a top site item.
 *
 * @param context An Android context.
 * @param topSite The [TopSite] to show the context menu for.
 * @param onItemTapped Callback invoked when the user taps on a menu item.
 */
class TopSiteItemMenu(
    private val context: Context,
    private val topSite: TopSite,
    private val onItemTapped: (Item) -> Unit = {},
) {
    sealed class Item {
        object OpenInPrivateTab : Item()
        object RenameTopSite : Item()
        object RemoveTopSite : Item()
        object Settings : Item()
        object SponsorPrivacy : Item()
    }

    val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }

    private val menuItems by lazy {
        val isPinnedSite = topSite is TopSite.Pinned || topSite is TopSite.Default
        val isProvidedSite = topSite is TopSite.Provided

        listOfNotNull(
            SimpleBrowserMenuItem(
                context.getString(R.string.bookmark_menu_open_in_private_tab_button),
            ) {
                onItemTapped.invoke(Item.OpenInPrivateTab)
            },
            if (isPinnedSite) {
                SimpleBrowserMenuItem(
                    context.getString(R.string.rename_top_site),
                ) {
                    onItemTapped.invoke(Item.RenameTopSite)
                }
            } else {
                null
            },
            if (!isProvidedSite) {
                SimpleBrowserMenuItem(
                    if (isPinnedSite) {
                        context.getString(R.string.remove_top_site)
                    } else {
                        context.getString(R.string.delete_from_history)
                    },
                ) {
                    onItemTapped.invoke(Item.RemoveTopSite)
                }
            } else {
                null
            },
            if (isProvidedSite) {
                SimpleBrowserMenuItem(
                    context.getString(R.string.top_sites_menu_settings),
                ) {
                    onItemTapped.invoke(Item.Settings)
                }
            } else {
                null
            },
            if (isProvidedSite) {
                SimpleBrowserMenuItem(
                    context.getString(R.string.top_sites_menu_sponsor_privacy),
                ) {
                    onItemTapped.invoke(Item.SponsorPrivacy)
                }
            } else {
                null
            },
        )
    }
}