<commit_before>package net.ketc.numeri.presentation.presenter.fragment.tweet.display

import android.os.Handler
import net.ketc.numeri.R
import net.ketc.numeri.domain.inject
import net.ketc.numeri.domain.model.Tweet
import net.ketc.numeri.domain.service.OAuthService
import net.ketc.numeri.domain.service.TwitterClient
import net.ketc.numeri.presentation.presenter.fragment.AutoDisposableFragmentPresenter
import net.ketc.numeri.presentation.view.component.ReadableMore
import net.ketc.numeri.presentation.view.component.TweetOperatorDialogFactory
import net.ketc.numeri.presentation.view.fragment.TimeLineFragmentInterface
import net.ketc.numeri.util.rx.MySchedulers
import org.jetbrains.anko.toast
import twitter4j.Paging
import javax.inject.Inject

abstract class TimeLinePresenter(timeLineFragment: TimeLineFragmentInterface) : AutoDisposableFragmentPresenter<TimeLineFragmentInterface>(), ReadableMore<MutableList<Tweet>> {
    @Inject
    lateinit var oAuthService: OAuthService

    override val fragment: TimeLineFragmentInterface = timeLineFragment
    private lateinit var mClient: TwitterClient
    protected val client: TwitterClient
        get() = mClient

    private val handler = Handler()
    private val unlockUpdate: () -> Unit = { fragment.isRefreshable = true }

    init {
        inject()
    }

    abstract fun getTweets(paging: Paging): List<Tweet>

    override fun initialize() {
        singleTask(MySchedulers.twitter) {
            oAuthService.clients()
        } success {
            mClient = it.first { it.id == fragment.display.token.id }
            fragment.setClient(client)
            client.stream.onDeletionNoticeFlowable.subscribe {
                fragment.remove(it.statusId)
            }.autoDispose()
            beforeInitializeLoad()
            initializeLoad()
            afterInitializeLoad()
        }
    }

    private fun initializeLoad() {
        fragment.isRefreshing = true
        singleTask(MySchedulers.twitter) {
            getTweets(Paging().apply {
                count = DEFAULT_COUNT
            })
        } error {
            it.printStackTrace()
            fragment.activity.toast("error")
            fragment.isReadMorEnabled = true
            fragment.isRefreshing = false
        } success {
            fragment.addAll(it)
            fragment.isReadMorEnabled = true
            fragment.isRefreshing = false
        }
    }

    /**
     * callback at before initialization load
     */
    abstract fun beforeInitializeLoad()

    /**
     * callback at initialization load completion
     */
    abstract fun afterInitializeLoad()

    fun update() {
        fragment.isRefreshing = true
        singleTask(MySchedulers.twitter) {
            getTweets(Paging().apply {
                count = DEFAULT_COUNT
                fragment.firstTweet?.let {
                    sinceId = it.id
                }
            }).reversed()
        } error {
            it.printStackTrace()
            fragment.isRefreshing = false
            fragment.activity.toast("error")
            lockUpdate()
        } success {
            fragment.isRefreshing = false
            fragment.insertAllToTop(it)<commit_msg>変なことしてた<commit_after>package net.ketc.numeri.presentation.presenter.fragment.tweet.display

import android.os.Handler
import net.ketc.numeri.R
import net.ketc.numeri.domain.inject
import net.ketc.numeri.domain.model.Tweet
import net.ketc.numeri.domain.service.OAuthService
import net.ketc.numeri.domain.service.TwitterClient
import net.ketc.numeri.presentation.presenter.fragment.AutoDisposableFragmentPresenter
import net.ketc.numeri.presentation.view.component.ReadableMore
import net.ketc.numeri.presentation.view.component.TweetOperatorDialogFactory
import net.ketc.numeri.presentation.view.fragment.TimeLineFragmentInterface
import net.ketc.numeri.util.rx.MySchedulers
import org.jetbrains.anko.toast
import twitter4j.Paging
import javax.inject.Inject

abstract class TimeLinePresenter(timeLineFragment: TimeLineFragmentInterface) : AutoDisposableFragmentPresenter<TimeLineFragmentInterface>(), ReadableMore<MutableList<Tweet>> {
    @Inject
    lateinit var oAuthService: OAuthService

    override val fragment: TimeLineFragmentInterface = timeLineFragment
    private lateinit var mClient: TwitterClient
    protected val client: TwitterClient
        get() = mClient

    private val handler = Handler()
    private val unlockUpdate: () -> Unit = { fragment.isRefreshable = true }

    init {
        inject()
    }

    abstract fun getTweets(paging: Paging): List<Tweet>

    override fun initialize() {
        singleTask(MySchedulers.twitter) {
            oAuthService.clients()
        } success {
            mClient = it.first { it.id == fragment.display.token.id }
            fragment.setClient(client)
            client.stream.onDeletionNoticeFlowable.subscribe {
                fragment.remove(it.statusId)
            }.autoDispose()
            beforeInitializeLoad()
            initializeLoad()
            afterInitializeLoad()
        }
    }

    private fun initializeLoad() {
        fragment.isRefreshing = true
        singleTask(MySchedulers.twitter) {
            getTweets(Paging().apply {
                count = DEFAULT_COUNT
            })
        } error {
            it.printStackTrace()
            fragment.activity.toast("error")
            fragment.isReadMorEnabled = true
            fragment.isRefreshing = false
        } success {
            fragment.addAll(it)
            fragment.isReadMorEnabled = true
            fragment.isRefreshing = false
        }
    }

    /**
     * callback at before initialization load
     */
    abstract fun beforeInitializeLoad()

    /**
     * callback at initialization load completion
     */
    abstract fun afterInitializeLoad()

    fun update() {
        fragment.isRefreshing = true
        singleTask(MySchedulers.twitter) {
            getTweets(Paging().apply {
                count = DEFAULT_COUNT
                fragment.firstTweet?.let {
                    sinceId = it.id
                }
            })
        } error {
            it.printStackTrace()
            fragment.isRefreshing = false
            fragment.activity.toast("error")
            lockUpdate()
        } success {
            fragment.isRefreshing = false
            fragment.insertAllToTop(it)
{'fraction_non_alphanumeric': 0.08358162145209345, 'fraction_numerical': 0.00031362709738121374, 'mean_word_length': 2.3961661341853033, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '7595358', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1676, 'n_words': 300}