<commit_before>package com.obaied.dingerquotes.ui.start

import com.obaied.dingerquotes.data.DataManager
import com.obaied.dingerquotes.data.model.Quote
//import com.obaied.dingerquotes.injection.ConfigPersistent
import com.obaied.dingerquotes.ui.base.BasePresenter
import com.obaied.dingerquotes.util.Schedulers.SchedulerProvider
import com.obaied.dingerquotes.util.d
import com.obaied.dingerquotes.util.e
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.Consumer
import io.reactivex.rxkotlin.subscribeBy
import java.net.SocketTimeoutException
import java.util.*
import javax.inject.Inject

/**
 * Created by ab on 19/03/2017.
 */

class StartPresenter
@Inject constructor(dataManager: DataManager,
                    compositeDisposable: CompositeDisposable,
                    schedulerProvider: SchedulerProvider)
    : BasePresenter<StartMvpView>(dataManager, compositeDisposable, schedulerProvider) {
    fun subscribeToDbToFetchQuotes() {
        d { "subscribeToDbToFetchQuotes(): " }

        checkViewAttached()
        mvpView?.showProgress()

        mCompositeDisposable.add(mDataManager.fetchQuotesFromDb()
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(mSchedulerProvider.ui())
                .subscribe(Consumer<List<Quote>> {
                    d { "subscribeToDbToFetchQuotes(): Received quotes: ${it.size}" }
                    mvpView?.hideProgress()

                    if (it.isEmpty()) {
                        mvpView?.showEmpty()
                        return@Consumer
                    }

                    mvpView?.showQuotes(it)

                }, Consumer<Throwable> {
                    e(it, { "subscribeToDbToFetchQuotes(): Received error" })

                    var error: String = "error"
                    if (it is SocketTimeoutException) {
                        error = " | Timed out | "
                    }

                    error += it.message

                    mvpView?.hideProgress()
                    mvpView?.showError(error)
                }
                ))

//        mCompositeDisposable.add(mDataManager.fetchRandomImagesFromApi(limit)
//                .subscribeOn(mSchedulerProvider.io())
//                .subscribeBy(
//                        onNext = { d { "received new quote: $it" } },
//                        onError = { d { "Error occurred: ${it.printStackTrace()}" } },
//                        onComplete = { d { "done server update" } }
//                ))

//                .subscribe(Consumer<List<RandomImage>> { images ->
//                    d { "subscribeToDbToFetchQuotes(): Received images: ${images.size}" }
//
//                    if (images.isEmpty()) {
//                        return@Consumer
//                    }
//
//                    for (image in images) {
//                        d { "quote's image url: ${image.url}" }
//                    }
//
//                    //TODO: Assing an image_url to each quote
//                    //TODO: then, simply use the image_url from the quote to the RecyclerView and the
//                    // QuoteActivity.
//
//                    (0 until quotes.size)
//                            .filter { quotes[it].imageTag == null }
//                            .filter { it < images.size }
//                            .forEach { quotes[it].imageTag = images[it].url }
//
////                    mvpView?.showQuotes(quotes)
//
//                }, Consumer<Throwable> {
//                    e(it, { "subscribeToDbToFetchQuotes(): Received error" })
//
//                    var error: String = "error"
//                    if (it is SocketTimeoutException) {
//                        error = " | Timed out | "
//                    }
//
//                    error += it.message
//
//                    mvpView?.hideProgress()
//                    mvpView?.showError(error)
//                }
//                ))

    }

    fun fetchQuotesFromApi(limit: Int) {
        //since subscribeToDbToFetchQuotes is subscribed to SqlBrite's SELECT statement,
        // whatever I push here would be updated there
        mDataManager.fetchQuotesFromApi(limit)
                .subscribeOn(mSchedulerProvider.io())
                .subscribeBy(
                        onNext = { },
                        onError = { d { "Error occurred: ${it.printStackTrace()}" } },
                        onComplete = { d { "done server fetch" } }
                )
    }
}<commit_msg>Remove fetching random images<commit_after>package com.obaied.dingerquotes.ui.start

import com.obaied.dingerquotes.data.DataManager
import com.obaied.dingerquotes.data.model.Quote
//import com.obaied.dingerquotes.injection.ConfigPersistent
import com.obaied.dingerquotes.ui.base.BasePresenter
import com.obaied.dingerquotes.util.Schedulers.SchedulerProvider
import com.obaied.dingerquotes.util.d
import com.obaied.dingerquotes.util.e
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.subjects.PublishSubject
import java.net.SocketTimeoutException
import java.util.*
import javax.inject.Inject

/**
 * Created by ab on 19/03/2017.
 */

class StartPresenter
@Inject constructor(dataManager: DataManager,
                    compositeDisposable: CompositeDisposable,
                    schedulerProvider: SchedulerProvider)
    : BasePresenter<StartMvpView>(dataManager, compositeDisposable, schedulerProvider) {
    val retrySubject = PublishSubject.create<Any>()

    fun subscribeToDbToFetchQuotes() {
        d { "subscribeToDbToFetchQuotes(): " }

        checkViewAttached()
        mvpView?.showProgress()

        mCompositeDisposable.add(mDataManager.fetchQuotesFromDb()
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(mSchedulerProvider.ui())
//                .retryWhen { it.flatMap { retrySubject } }
                .subscribe(Consumer<List<Quote>> {
                    d { "subscribeToDbToFetchQuotes(): Received quotes: ${it.size}" }
                    mvpView?.hideProgress()

                    if (it.isEmpty()) {
                        mvpView?.showEmpty()
                        return@Consumer
                    }

                    mvpView?.showQuotes(it)

                }, Consumer<Throwable> {
                    e(it, { "subscribeToDbToFetchQuotes(): Received error" })

                    var error: String = "error"
                    if (it is SocketTimeoutException) {
                        error = " | Timed out | "
                    }

                    error += it.message

                    mvpView?.hideProgress()
                    mvpView?.showError(error)
                }
                )
        )
    }

    fun fetchQuotesFromDb() {
        retrySubject.onNext(1)
    }

    fun fetchQuotesFromApi(limit: Int) {
        //since subscribeToDbToFetchQuotes is subscribed to SqlBrite's SELECT statement,
        // whatever I push here would be updated there
        mDataManager.fetchQuotesFromApi(limit)
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(mSchedulerProvider.ui())
                .subscribeBy(
                        onNext = {
                            mDataManager.setQuotesToDb(it)
                                    .subscribe({}, {}, { d { "Done server set" } })
                        },
                        onError = { mvpView?.showError("No internet connection") },
                        onComplete = { d { "onComplete(): done with fetching quotes from api" } }
                )
//                .subscribe(Consumer<Quote> {
//                }, Consumer<Throwable> {
//                    e(it, { "fetchQuotesFromApi(): PINEAPPLE Received error" })
////
////                    var error: String = "error"
////                    if (it is SocketTimeoutException) {
////                        error = " | Timed out | "
////                    }
////
////                    error += it.message
////
////                    mvpView?.hideProgress()
////                    mvpView?.showError(error)
//                }, Action { d { "done server fetch" } }
//                )
    }
}
{'fraction_non_alphanumeric': 0.10877656381318286, 'fraction_numerical': 0.0021611237843678715, 'mean_word_length': 1.5135787567893784, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1464024', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2114, 'n_words': 386}