<commit_before>package jp.toastkid.yobidashi.tab

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.text.TextUtils
import androidx.lifecycle.ViewModelProviders
import io.reactivex.Completable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.browser.BrowserFragment
import jp.toastkid.yobidashi.browser.BrowserHeaderViewModel
import jp.toastkid.yobidashi.browser.archive.IdGenerator
import jp.toastkid.yobidashi.browser.archive.auto.AutoArchive
import jp.toastkid.yobidashi.libs.BitmapCompressor
import jp.toastkid.yobidashi.libs.preference.ColorPair
import jp.toastkid.yobidashi.libs.preference.PreferenceApplier
import jp.toastkid.yobidashi.main.HeaderViewModel
import jp.toastkid.yobidashi.main.MainActivity
import jp.toastkid.yobidashi.tab.model.EditorTab
import jp.toastkid.yobidashi.tab.model.PdfTab
import jp.toastkid.yobidashi.tab.model.Tab
import jp.toastkid.yobidashi.tab.model.WebTab
import jp.toastkid.yobidashi.tab.tab_list.TabListViewModel
import timber.log.Timber
import java.io.File

/**
 * ModuleAdapter of [Tab].
 *
 * @author toastkidjp
 */
class TabAdapter(
        private val contextSupplier: () -> Context,
        private val tabEmptyCallback: () -> Unit
) {

    private val tabList: TabList = TabList.loadOrInit(contextSupplier())

    private val colorPair: ColorPair

    private val tabThumbnails: TabThumbnails

    private val preferenceApplier: PreferenceApplier

    private val autoArchive = AutoArchive.make(contextSupplier())

    private val disposables: CompositeDisposable = CompositeDisposable()

    private var browserHeaderViewModel: BrowserHeaderViewModel? = null

    private var headerViewModel: HeaderViewModel? = null

    private val bitmapCompressor = BitmapCompressor()

    private var tabListViewModel: TabListViewModel? = null

    init {
        val viewContext = contextSupplier()
        tabThumbnails = TabThumbnails(contextSupplier)
        preferenceApplier = PreferenceApplier(viewContext)
        colorPair = preferenceApplier.colorPair()
        if (viewContext is MainActivity) {
            val viewModelProvider = ViewModelProviders.of(viewContext)
            browserHeaderViewModel = viewModelProvider.get(BrowserHeaderViewModel::class.java)
            headerViewModel = viewModelProvider.get(HeaderViewModel::class.java)
            tabListViewModel = viewModelProvider.get(TabListViewModel::class.java)
        }
    }
    /**
     * Save new thumbnail asynchronously.
     */
    fun saveNewThumbnailAsync(makeDrawingCache: () -> Bitmap?) {
        val currentTab = tabList.currentTab() ?: return
        makeDrawingCache()?.let {
            Completable.fromAction {
                val file = tabThumbnails.assignNewFile(currentTab.thumbnailPath())
                bitmapCompressor(it, file)
            }.subscribeOn(Schedulers.io())
                    .subscribe({}, Timber::e)
                    .addTo(disposables)
        }
    }

    /**
     * Delete thumbnail file.
     *
     * @param thumbnailPath file path.
     */
    fun deleteThumbnail(thumbnailPath: String?) {
        if (TextUtils.isEmpty(thumbnailPath)) {
            return
        }

        val lastScreenshot = File(thumbnailPath)
        if (lastScreenshot.exists()) {
            lastScreenshot.delete()
        }
    }

    /**
     * Open new tab with URL string.
     * TODO remove withLoad.
     */
    fun openNewWebTab(url: String = preferenceApplier.homeUrl, withLoad: Boolean = true) {
        val newTab = WebTab.make("New tab: $url", url)
        tabList.add(newTab)
        setCount()
        setIndexByTab(newTab)
    }

    /**
     * Open background tab with URL string.
     *
     * @param title Tab's title
     * @param url Tab's URL
     */
    fun openBackgroundTab(title: String, url: String) {
        val tabTitle =
                if (title.isNotBlank()) title
                else contextSupplier().getString(R.string.new_tab)

        tabList.add(WebTab.makeBackground(tabTitle, url))
        tabList.save()
        setCount()
    }

    /**
     * This method allow calling from only [BrowserFragment].
     */
    internal fun openNewEditorTab(path: String? = null) {<commit_msg>refactor #523 Delete unused argument.<commit_after>package jp.toastkid.yobidashi.tab

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.text.TextUtils
import androidx.lifecycle.ViewModelProviders
import io.reactivex.Completable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.browser.BrowserFragment
import jp.toastkid.yobidashi.browser.BrowserHeaderViewModel
import jp.toastkid.yobidashi.browser.archive.IdGenerator
import jp.toastkid.yobidashi.browser.archive.auto.AutoArchive
import jp.toastkid.yobidashi.libs.BitmapCompressor
import jp.toastkid.yobidashi.libs.preference.ColorPair
import jp.toastkid.yobidashi.libs.preference.PreferenceApplier
import jp.toastkid.yobidashi.main.HeaderViewModel
import jp.toastkid.yobidashi.main.MainActivity
import jp.toastkid.yobidashi.tab.model.EditorTab
import jp.toastkid.yobidashi.tab.model.PdfTab
import jp.toastkid.yobidashi.tab.model.Tab
import jp.toastkid.yobidashi.tab.model.WebTab
import jp.toastkid.yobidashi.tab.tab_list.TabListViewModel
import timber.log.Timber
import java.io.File

/**
 * ModuleAdapter of [Tab].
 *
 * @author toastkidjp
 */
class TabAdapter(
        private val contextSupplier: () -> Context,
        private val tabEmptyCallback: () -> Unit
) {

    private val tabList: TabList = TabList.loadOrInit(contextSupplier())

    private val colorPair: ColorPair

    private val tabThumbnails: TabThumbnails

    private val preferenceApplier: PreferenceApplier

    private val autoArchive = AutoArchive.make(contextSupplier())

    private val disposables: CompositeDisposable = CompositeDisposable()

    private var browserHeaderViewModel: BrowserHeaderViewModel? = null

    private var headerViewModel: HeaderViewModel? = null

    private val bitmapCompressor = BitmapCompressor()

    private var tabListViewModel: TabListViewModel? = null

    init {
        val viewContext = contextSupplier()
        tabThumbnails = TabThumbnails(contextSupplier)
        preferenceApplier = PreferenceApplier(viewContext)
        colorPair = preferenceApplier.colorPair()
        if (viewContext is MainActivity) {
            val viewModelProvider = ViewModelProviders.of(viewContext)
            browserHeaderViewModel = viewModelProvider.get(BrowserHeaderViewModel::class.java)
            headerViewModel = viewModelProvider.get(HeaderViewModel::class.java)
            tabListViewModel = viewModelProvider.get(TabListViewModel::class.java)
        }
    }
    /**
     * Save new thumbnail asynchronously.
     */
    fun saveNewThumbnailAsync(makeDrawingCache: () -> Bitmap?) {
        val currentTab = tabList.currentTab() ?: return
        makeDrawingCache()?.let {
            Completable.fromAction {
                val file = tabThumbnails.assignNewFile(currentTab.thumbnailPath())
                bitmapCompressor(it, file)
            }.subscribeOn(Schedulers.io())
                    .subscribe({}, Timber::e)
                    .addTo(disposables)
        }
    }

    /**
     * Delete thumbnail file.
     *
     * @param thumbnailPath file path.
     */
    fun deleteThumbnail(thumbnailPath: String?) {
        if (TextUtils.isEmpty(thumbnailPath)) {
            return
        }

        val lastScreenshot = File(thumbnailPath)
        if (lastScreenshot.exists()) {
            lastScreenshot.delete()
        }
    }

    /**
     * Open new tab with URL string.
     *
     * @param url URL (default = homeUrl)
     */
    fun openNewWebTab(url: String = preferenceApplier.homeUrl) {
        val newTab = WebTab.make("New tab: $url", url)
        tabList.add(newTab)
        setCount()
        setIndexByTab(newTab)
    }

    /**
     * Open background tab with URL string.
     *
     * @param title Tab's title
     * @param url Tab's URL
     */
    fun openBackgroundTab(title: String, url: String) {
        val tabTitle =
                if (title.isNotBlank()) title
                else contextSupplier().getString(R.string.new_tab)

        tabList.add(WebTab.makeBackground(tabTitle, url))
        tabList.save()
        setCount()
    }

    /**
     * This method allow calling from only [BrowserFragment].
     */
    internal fun openNewEditorTab(path: String? = null) {
{'fraction_non_alphanumeric': 0.09077346798255681, 'fraction_numerical': 0.00034427358274041774, 'mean_word_length': 3.042207792207792, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3224228', 'n_tokens_mistral': 2720, 'n_tokens_neox': 2549, 'n_words': 493}