<commit_before>/*
 * Copyright (c) 2016-2018 ayatk.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ayatk.biblio.ui.search

import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.OnLifecycleEvent
import android.arch.lifecycle.ViewModel
import android.databinding.ObservableArrayList
import android.util.Log
import android.view.View
import com.ayatk.biblio.data.narou.NarouClient
import com.ayatk.biblio.data.narou.util.QueryBuilder
import com.ayatk.biblio.domain.repository.LibraryRepository
import com.ayatk.biblio.model.Library
import com.ayatk.biblio.model.Novel
import com.ayatk.biblio.util.rx.SchedulerProvider
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import javax.inject.Inject

class SearchViewModel @Inject constructor(
    private val narouClient: NarouClient,
    private val libraryRepository: LibraryRepository,
    private val schedulerProvider: SchedulerProvider
) : ViewModel(), LifecycleObserver {

  private val compositeDisposable = CompositeDisposable()

  val searchResult = ObservableArrayList<SearchResultItemViewModel>()

  val searchResultVisibility = MutableLiveData<Int>()

  var libraries = listOf<Library>()

  @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
  fun onCreate() {
    libraryRepository.findAll()
        .observeOn(schedulerProvider.ui())
        .subscribe(
            { libraries -> this.libraries = libraries },
            { t -> Log.e("SearchViewModel", t.toString()) }
        )
        .addTo(compositeDisposable)
  }

  override fun onCleared() {
    super.onCleared()
    compositeDisposable.clear()
  }

  fun search(query: String) {
    val formattedQuery = query.trim { it <= ' ' }
    if (formattedQuery.isNotBlank()) {
      searchResultVisibility.postValue(View.VISIBLE)
      val builtQuery = QueryBuilder().searchWords(query).size(100).build()
      compositeDisposable.clear()
      narouClient.getNovel(builtQuery)
          .map({ novels -> convertToViewModel(novels) })
          .observeOn(schedulerProvider.ui())
          .subscribe(
              { viewModels ->
                if (viewModels.isNotEmpty()) {
                  searchResult.clear()
                  searchResult.addAll(viewModels)
                }
              },
              { _ -> /* TODO: あとで頑張る */ }
          )
          .addTo(compositeDisposable)
    } else {
      searchResultVisibility.postValue(View.GONE)
    }
  }<commit_msg>あとで頑張った<commit_after>/*
 * Copyright (c) 2016-2018 ayatk.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ayatk.biblio.ui.search

import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.OnLifecycleEvent
import android.arch.lifecycle.ViewModel
import android.databinding.ObservableArrayList
import android.util.Log
import android.view.View
import com.ayatk.biblio.data.narou.NarouClient
import com.ayatk.biblio.data.narou.util.QueryBuilder
import com.ayatk.biblio.domain.repository.LibraryRepository
import com.ayatk.biblio.model.Library
import com.ayatk.biblio.model.Novel
import com.ayatk.biblio.util.rx.SchedulerProvider
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import timber.log.Timber
import javax.inject.Inject

class SearchViewModel @Inject constructor(
    private val narouClient: NarouClient,
    private val libraryRepository: LibraryRepository,
    private val schedulerProvider: SchedulerProvider
) : ViewModel(), LifecycleObserver {

  private val compositeDisposable = CompositeDisposable()

  val searchResult = ObservableArrayList<SearchResultItemViewModel>()

  val searchResultVisibility = MutableLiveData<Int>()

  var libraries = listOf<Library>()

  @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
  fun onCreate() {
    libraryRepository.findAll()
        .observeOn(schedulerProvider.ui())
        .subscribe(
            { libraries -> this.libraries = libraries },
            { t -> Log.e("SearchViewModel", t.toString()) }
        )
        .addTo(compositeDisposable)
  }

  override fun onCleared() {
    super.onCleared()
    compositeDisposable.clear()
  }

  fun search(query: String) {
    val formattedQuery = query.trim { it <= ' ' }
    if (formattedQuery.isNotBlank()) {
      searchResultVisibility.postValue(View.VISIBLE)
      val builtQuery = QueryBuilder().searchWords(query).size(100).build()
      compositeDisposable.clear()
      narouClient.getNovel(builtQuery)
          .map({ novels -> convertToViewModel(novels) })
          .observeOn(schedulerProvider.ui())
          .subscribe(
              { viewModels ->
                if (viewModels.isNotEmpty()) {
                  searchResult.clear()
                  searchResult.addAll(viewModels)
                }
              },
              Timber::e
          )
          .addTo(compositeDisposable)
    } else {
      searchResultVisibility.postValue(View.GONE)
    }
  }
{'fraction_non_alphanumeric': 0.09450621321124918, 'fraction_numerical': 0.004905166775670372, 'mean_word_length': 3.329087048832272, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3109030', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1701, 'n_words': 397}