<commit_before>package org.wikipedia.readinglist

import android.view.Gravity
import android.view.MenuItem
import android.view.View
import androidx.annotation.MenuRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.schedulers.Schedulers
import org.wikipedia.R
import org.wikipedia.history.HistoryEntry
import org.wikipedia.readinglist.database.ReadingList
import org.wikipedia.readinglist.database.ReadingListDbHelper
import org.wikipedia.readinglist.database.ReadingListPage
import org.wikipedia.util.ClipboardUtil.setPlainText
import org.wikipedia.util.FeedbackUtil.showMessage
import org.wikipedia.util.ShareUtil.shareText

class LongPressMenu(private val anchorView: View, private val existsInAnyList: Boolean, private val callback: Callback?) {
    interface Callback {
        fun onOpenLink(entry: HistoryEntry)
        fun onOpenInNewTab(entry: HistoryEntry)
        fun onAddRequest(entry: HistoryEntry, addToDefault: Boolean)
        fun onMoveRequest(page: ReadingListPage?, entry: HistoryEntry)
    }

    @MenuRes
    private val menuRes: Int = if (existsInAnyList) R.menu.menu_long_press else R.menu.menu_reading_list_page_toggle
    private var listsContainingPage: List<ReadingList>? = null
    private var entry: HistoryEntry? = null

    constructor(anchorView: View, callback: Callback?) : this(anchorView, false, callback)

    fun show(entry: HistoryEntry?) {
        entry?.let {
            Completable.fromAction { listsContainingPage = ReadingListDbHelper.instance().let { helper ->
                helper.getListsFromPageOccurrences(helper.getAllPageOccurrences(it.title))
            } }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (!anchorView.isAttachedToWindow) {
                    return@subscribe
                }
                this.entry = it
                showMenu()
            }
        }
    }

    private fun showMenu() {
        if (!existsInAnyList) {
            return
        }
        listsContainingPage?.let {
            PopupMenu(anchorView.context, anchorView).let { menu -><commit_msg>Fix: "save" on PageFragment does not work properly on PageFragment (#2213)<commit_after>package org.wikipedia.readinglist

import android.view.Gravity
import android.view.MenuItem
import android.view.View
import androidx.annotation.MenuRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.schedulers.Schedulers
import org.wikipedia.R
import org.wikipedia.history.HistoryEntry
import org.wikipedia.readinglist.database.ReadingList
import org.wikipedia.readinglist.database.ReadingListDbHelper
import org.wikipedia.readinglist.database.ReadingListPage
import org.wikipedia.util.ClipboardUtil.setPlainText
import org.wikipedia.util.FeedbackUtil.showMessage
import org.wikipedia.util.ShareUtil.shareText

class LongPressMenu(private val anchorView: View, private val existsInAnyList: Boolean, private val callback: Callback?) {
    interface Callback {
        fun onOpenLink(entry: HistoryEntry)
        fun onOpenInNewTab(entry: HistoryEntry)
        fun onAddRequest(entry: HistoryEntry, addToDefault: Boolean)
        fun onMoveRequest(page: ReadingListPage?, entry: HistoryEntry)
    }

    @MenuRes
    private val menuRes: Int = if (existsInAnyList) R.menu.menu_long_press else R.menu.menu_reading_list_page_toggle
    private var listsContainingPage: List<ReadingList>? = null
    private var entry: HistoryEntry? = null

    constructor(anchorView: View, callback: Callback?) : this(anchorView, false, callback)

    fun show(entry: HistoryEntry?) {
        entry?.let {
            Completable.fromAction { listsContainingPage = ReadingListDbHelper.instance().let { helper ->
                helper.getListsFromPageOccurrences(helper.getAllPageOccurrences(it.title))
            } }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (!anchorView.isAttachedToWindow) {
                    return@subscribe
                }
                this.entry = it
                showMenu()
            }
        }
    }

    private fun showMenu() {
        if (!existsInAnyList && listsContainingPage.isNullOrEmpty()) {
            return
        }
        listsContainingPage?.let {
            PopupMenu(anchorView.context, anchorView).let { menu ->
{'fraction_non_alphanumeric': 0.0828764234500211, 'fraction_numerical': 0.002108814846056516, 'mean_word_length': 3.303992740471869, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3954092', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1277, 'n_words': 255}