<commit_before>/*
 * Copyright (c) 2017 <NAME>
 *
 * 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 io.sweers.catchup.ui.about

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.support.design.widget.Snackbar
import android.support.v7.graphics.Palette
import android.support.v7.graphics.Palette.Swatch
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.ViewHolder
import android.support.v7.widget.RxViewHolder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.OvershootInterpolator
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import butterknife.BindDimen
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.Unbinder
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.cache.http.HttpCachePolicy
import com.apollographql.apollo.rx2.Rx2Apollo
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.DrawableImageViewTarget
import com.bumptech.glide.request.transition.Transition
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.uber.autodispose.kotlin.autoDisposeWith
import dagger.Provides
import dagger.Subcomponent
import dagger.android.AndroidInjector
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers
import io.sweers.catchup.R
import io.sweers.catchup.R.layout
import io.sweers.catchup.data.LinkManager
import io.sweers.catchup.data.github.ProjectOwnersByIdsQuery
import io.sweers.catchup.data.github.RepositoriesByIdsQuery
import io.sweers.catchup.data.github.RepositoriesByIdsQuery.AsRepository
import io.sweers.catchup.data.github.RepositoryByNameAndOwnerQuery
import io.sweers.catchup.injection.ConductorInjection
import io.sweers.catchup.injection.scopes.PerController
import io.sweers.catchup.service.api.UrlMeta
import io.sweers.catchup.ui.Scrollable
import io.sweers.catchup.ui.StickyHeaders
import io.sweers.catchup.ui.StickyHeadersLinearLayoutManager
import io.sweers.catchup.ui.about.LicensesModule.ForLicenses
import io.sweers.catchup.ui.base.ButterKnifeController
import io.sweers.catchup.ui.base.CatchUpItemViewHolder
import io.sweers.catchup.util.UiUtil
import io.sweers.catchup.util.dp2px
import io.sweers.catchup.util.e
import io.sweers.catchup.util.findSwatch
import io.sweers.catchup.util.hide
import io.sweers.catchup.util.isInNightMode
import io.sweers.catchup.util.luminosity
import jp.wasabeef.recyclerview.animators.FadeInUpAnimator
import okio.Okio
import javax.inject.Inject
import javax.inject.Qualifier

/**
 * A controller that displays oss licenses.
 */
class LicensesController : ButterKnifeController(), Scrollable {

  @Inject lateinit var apolloClient: ApolloClient

  @Inject
  @field:ForLicenses
  lateinit var moshi: Moshi

  @Inject internal lateinit var linkManager: LinkManager

  @BindDimen(R.dimen.avatar)
  @JvmField
  var dimenSize: Int = 0
  @BindView(R.id.progress) lateinit var progressBar: ProgressBar
  @BindView(R.id.list) lateinit var recyclerView: RecyclerView

  private val adapter = Adapter()
  private lateinit var layoutManager: StickyHeadersLinearLayoutManager<Adapter>

  override fun onContextAvailable(context: Context) {
    ConductorInjection.inject(this)
    super.onContextAvailable(context)
  }

  override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View {
    return inflater.inflate(R.layout.controller_licenses, container, false)
  }

  override fun bind(view: View) = ButterKnife.bind(this, view)

  override fun onViewBound(view: View) {
    super.onViewBound(view)
    recyclerView.adapter = adapter
    layoutManager = StickyHeadersLinearLayoutManager(view.context)
    recyclerView.layoutManager = layoutManager
    recyclerView.itemAnimator = FadeInUpAnimator(OvershootInterpolator(1f)).apply {
      addDuration = 300
      removeDuration = 300
    }
  }

  override fun onAttach(view: View) {
    super.onAttach(view)
    if (adapter.itemCount == 0) {
      // Weird hack to avoid adding more unnecessarily. I'm not sure how to leave transient state
      // during onPause in Conductor
      requestItems()
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .doFinally {
            progressBar.hide()
          }
          .subscribe { data, error ->
            if (data != null) {
              adapter.setItems(data)
            } else {
              // TODO Show a better error
              e(error) { "Could not load open source licenses." }
              Snackbar.make(recyclerView, "Could not load open source licenses.",
                  Snackbar.LENGTH_SHORT).show()
            }
          }
    }
  }

  /**
   * I give you: the most over-engineered OSS licenses section ever.
   */
  private fun requestItems(): Single<List<OssBaseItem>> {<commit_msg>Warn for now if licenses don't load<commit_after>/*
 * Copyright (c) 2017 <NAME>
 *
 * 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 io.sweers.catchup.ui.about

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.support.design.widget.Snackbar
import android.support.v7.graphics.Palette
import android.support.v7.graphics.Palette.Swatch
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.ViewHolder
import android.support.v7.widget.RxViewHolder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.OvershootInterpolator
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import butterknife.BindDimen
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.Unbinder
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.cache.http.HttpCachePolicy
import com.apollographql.apollo.rx2.Rx2Apollo
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.DrawableImageViewTarget
import com.bumptech.glide.request.transition.Transition
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.uber.autodispose.kotlin.autoDisposeWith
import dagger.Provides
import dagger.Subcomponent
import dagger.android.AndroidInjector
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers
import io.sweers.catchup.R
import io.sweers.catchup.R.layout
import io.sweers.catchup.data.LinkManager
import io.sweers.catchup.data.github.ProjectOwnersByIdsQuery
import io.sweers.catchup.data.github.RepositoriesByIdsQuery
import io.sweers.catchup.data.github.RepositoriesByIdsQuery.AsRepository
import io.sweers.catchup.data.github.RepositoryByNameAndOwnerQuery
import io.sweers.catchup.injection.ConductorInjection
import io.sweers.catchup.injection.scopes.PerController
import io.sweers.catchup.service.api.UrlMeta
import io.sweers.catchup.ui.Scrollable
import io.sweers.catchup.ui.StickyHeaders
import io.sweers.catchup.ui.StickyHeadersLinearLayoutManager
import io.sweers.catchup.ui.about.LicensesModule.ForLicenses
import io.sweers.catchup.ui.base.ButterKnifeController
import io.sweers.catchup.ui.base.CatchUpItemViewHolder
import io.sweers.catchup.util.UiUtil
import io.sweers.catchup.util.dp2px
import io.sweers.catchup.util.findSwatch
import io.sweers.catchup.util.hide
import io.sweers.catchup.util.isInNightMode
import io.sweers.catchup.util.luminosity
import io.sweers.catchup.util.w
import jp.wasabeef.recyclerview.animators.FadeInUpAnimator
import okio.Okio
import javax.inject.Inject
import javax.inject.Qualifier

/**
 * A controller that displays oss licenses.
 */
class LicensesController : ButterKnifeController(), Scrollable {

  @Inject lateinit var apolloClient: ApolloClient

  @Inject
  @field:ForLicenses
  lateinit var moshi: Moshi

  @Inject internal lateinit var linkManager: LinkManager

  @BindDimen(R.dimen.avatar)
  @JvmField
  var dimenSize: Int = 0
  @BindView(R.id.progress) lateinit var progressBar: ProgressBar
  @BindView(R.id.list) lateinit var recyclerView: RecyclerView

  private val adapter = Adapter()
  private lateinit var layoutManager: StickyHeadersLinearLayoutManager<Adapter>

  override fun onContextAvailable(context: Context) {
    ConductorInjection.inject(this)
    super.onContextAvailable(context)
  }

  override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View {
    return inflater.inflate(R.layout.controller_licenses, container, false)
  }

  override fun bind(view: View) = ButterKnife.bind(this, view)

  override fun onViewBound(view: View) {
    super.onViewBound(view)
    recyclerView.adapter = adapter
    layoutManager = StickyHeadersLinearLayoutManager(view.context)
    recyclerView.layoutManager = layoutManager
    recyclerView.itemAnimator = FadeInUpAnimator(OvershootInterpolator(1f)).apply {
      addDuration = 300
      removeDuration = 300
    }
  }

  override fun onAttach(view: View) {
    super.onAttach(view)
    if (adapter.itemCount == 0) {
      // Weird hack to avoid adding more unnecessarily. I'm not sure how to leave transient state
      // during onPause in Conductor
      requestItems()
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .doFinally {
            progressBar.hide()
          }
          .subscribe { data, error ->
            if (data != null) {
              adapter.setItems(data)
            } else {
              // TODO Show a better error
              w(error) { "Could not load open source licenses." }
              Snackbar.make(recyclerView, "Could not load open source licenses.",
                  Snackbar.LENGTH_SHORT).show()
            }
          }
    }
  }

  /**
   * I give you: the most over-engineered OSS licenses section ever.
   */
  private fun requestItems(): Single<List<OssBaseItem>> {
{'fraction_non_alphanumeric': 0.08367037411526795, 'fraction_numerical': 0.0042130097741826765, 'mean_word_length': 4.3681592039801, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '6744324', 'n_tokens_mistral': 3699, 'n_tokens_neox': 3524, 'n_words': 788}