<commit_before>package jp.toastkid.yobidashi.libs.clip

import android.content.Context
import android.net.Uri
import android.view.View
import androidx.core.net.toUri
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.Urls
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.libs.network.NetworkChecker

/**
 * Clipping URL opener, this class invoked containing URL in clipboard.
 *
 * @author toastkidjp
 */
class ClippingUrlOpener {

    /**
     * Invoke action.
     *
     * @param view [View](Nullable)
     * @param onClick callback
     */
    operator fun invoke(context: Context?, onClick: (Uri) -> Unit) {
        if (context == null || NetworkChecker.isNotAvailable(context)) {
            return
        }

        val activityContext = context
        val clipboardContent = Clipboard.getPrimary(activityContext)?.toString() ?: return
        val preferenceApplier = PreferenceApplier(activityContext)
        val lastClipped = preferenceApplier.lastClippedWord()

        if (shouldNotFeedback(clipboardContent, lastClipped)) {
            return
        }

        preferenceApplier.setLastClippedWord(clipboardContent)

        feedbackToUser(context, clipboardContent, onClick)
    }

    private fun shouldNotFeedback(clipboardContent: String, lastClippedWord: String) =
            Urls.isInvalidUrl(clipboardContent) || clipboardContent == lastClippedWord

    private fun feedbackToUser(
            context: Context,
            clipboardContent: String,
            onClick: (Uri) -> Unit
    ) {
        val contentViewModel = (context as? ViewModelStoreOwner)?.let {
            ViewModelProvider(it).get(ContentViewModel::class.java)
        }

        contentViewModel?.snackWithAction(
            context.getString(R.string.message_clipping_url_open, clipboardContent),
            context.getString(R.string.open),
            { onClick(clipboardContent.toUri()) }
        )
    }

}<commit_msg>bugfix #853 Fix bug.<commit_after>package jp.toastkid.yobidashi.libs.clip

import android.content.Context
import android.net.Uri
import android.view.View
import androidx.core.net.toUri
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.Urls
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.libs.network.NetworkChecker

/**
 * Clipping URL opener, this class invoked containing URL in clipboard.
 *
 * @author toastkidjp
 */
class ClippingUrlOpener {

    /**
     * Invoke action.
     *
     * @param view [View](Nullable)
     * @param onClick callback
     */
    operator fun invoke(context: Context?, onClick: (Uri) -> Unit) {
        if (context == null || NetworkChecker.isNotAvailable(context)) {
            return
        }

        val activityContext = context
        val clipboardContent = Clipboard.getPrimary(activityContext)?.toString() ?: return
        val preferenceApplier = PreferenceApplier(activityContext)
        val lastClipped = preferenceApplier.lastClippedWord()

        if (shouldNotFeedback(clipboardContent, lastClipped)) {
            return
        }

        preferenceApplier.setLastClippedWord(clipboardContent)

        feedbackToUser(context, clipboardContent, onClick)
    }

    private fun shouldNotFeedback(clipboardContent: String, lastClippedWord: String) =
            Urls.isInvalidUrl(clipboardContent)
                    || clipboardContent == lastClippedWord
                    || clipboardContent.length > 100

    private fun feedbackToUser(
            context: Context,
            clipboardContent: String,
            onClick: (Uri) -> Unit
    ) {
        val contentViewModel = (context as? ViewModelStoreOwner)?.let {
            ViewModelProvider(it).get(ContentViewModel::class.java)
        }

        contentViewModel?.snackWithAction(
            context.getString(R.string.message_clipping_url_open, clipboardContent),
            context.getString(R.string.open),
            { onClick(clipboardContent.toUri()) }
        )
    }

}
{'fraction_non_alphanumeric': 0.08979783732957217, 'fraction_numerical': 0.0014104372355430183, 'mean_word_length': 2.9915572232645404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '6203754', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1172, 'n_words': 223}