<commit_before>package com.isapp.kontroller

import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.support.annotation.CallSuper
import android.view.View

interface Controller {
  fun initialize() {}
  fun start() {}
  fun stop() {}
  fun destroy() {}
}

interface ManagingController : Controller {
  val managedControllers: List<Controller>

  @CallSuper
  override fun initialize() {
    managedControllers.forEach { it.initialize() }
  }

  @CallSuper
  override fun start() {
    managedControllers.forEach { it.start() }
  }

  @CallSuper
  override fun stop() {
    managedControllers.forEach { it.stop() }
  }

  @CallSuper
  override fun destroy() {
    managedControllers.forEach { it.destroy() }
  }
}

interface UIController : Controller {
  fun createView(): View?
  fun uiReady()
  fun navigateBack(): Boolean
}

abstract class ContextController<T : UI<*>> protected constructor(
    protected val context: Context,
    private val createUI: () -> T,
    private val navigateBackAction: () -> Boolean = { false }
) : UIController {
  constructor(fragment: Fragment, createUI: () -> T) : this(fragment.context, createUI, {
    fragment.fragmentManager.popBackStack()
    true
  })
  constructor(activity: Activity, createUI: () -> T) : this(activity as Context, createUI)

  protected var ui: T? = null
  override fun uiReady() {}
  override fun navigateBack() = navigateBackAction()

  @CallSuper
  override fun initialize() {
    ui = createUI()
  }

  override fun createView(): View? {
    return coerceCreateView<UIController, UI<UIController>>()
  }

  @CallSuper
  override fun destroy() {
    ui = null
  }

  private inline fun <reified THIS : UIController, reified UIType : UI<THIS>> coerceCreateView() = (ui as? UIType)?.createView(this as THIS)
}

abstract class ManagingContextController<T : UI<*>> private constructor(
    context: Context,
    createUI: () -> T,
    navigateBackAction: () -> Boolean = { false }
) : ContextController<T>(context, createUI, navigateBackAction), ManagingController  {
  constructor(fragment: Fragment, createUI: () -> T) : this(fragment.context, createUI, {
    fragment.fragmentManager.popBackStack()
    true
  })
  constructor(activity: Activity, createUI: () -> T) : this(activity as Context, createUI)

  @CallSuper
  override fun initialize() {
    super<ManagingController>.initialize()
    super<ContextController>.initialize()
  }

  @CallSuper
  override fun start() {
    super<ManagingController>.start()<commit_msg>Use a Fragment API that is available on all supported versions<commit_after>package com.isapp.kontroller

import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.support.annotation.CallSuper
import android.view.View

interface Controller {
  fun initialize() {}
  fun start() {}
  fun stop() {}
  fun destroy() {}
}

interface ManagingController : Controller {
  val managedControllers: List<Controller>

  @CallSuper
  override fun initialize() {
    managedControllers.forEach { it.initialize() }
  }

  @CallSuper
  override fun start() {
    managedControllers.forEach { it.start() }
  }

  @CallSuper
  override fun stop() {
    managedControllers.forEach { it.stop() }
  }

  @CallSuper
  override fun destroy() {
    managedControllers.forEach { it.destroy() }
  }
}

interface UIController : Controller {
  fun createView(): View?
  fun uiReady()
  fun navigateBack(): Boolean
}

abstract class ContextController<T : UI<*>> protected constructor(
    protected val context: Context,
    private val createUI: () -> T,
    private val navigateBackAction: () -> Boolean = { false }
) : UIController {
  constructor(fragment: Fragment, createUI: () -> T) : this(fragment.context, createUI, {
    fragment.fragmentManager.popBackStack()
    true
  })
  constructor(activity: Activity, createUI: () -> T) : this(activity as Context, createUI)

  protected var ui: T? = null
  override fun uiReady() {}
  override fun navigateBack() = navigateBackAction()

  @CallSuper
  override fun initialize() {
    ui = createUI()
  }

  override fun createView(): View? {
    return coerceCreateView<UIController, UI<UIController>>()
  }

  @CallSuper
  override fun destroy() {
    ui = null
  }

  private inline fun <reified THIS : UIController, reified UIType : UI<THIS>> coerceCreateView() = (ui as? UIType)?.createView(this as THIS)
}

abstract class ManagingContextController<T : UI<*>> private constructor(
    context: Context,
    createUI: () -> T,
    navigateBackAction: () -> Boolean = { false }
) : ContextController<T>(context, createUI, navigateBackAction), ManagingController  {
  constructor(fragment: Fragment, createUI: () -> T) : this(fragment.activity, createUI, {
    fragment.fragmentManager.popBackStack()
    true
  })
  constructor(activity: Activity, createUI: () -> T) : this(activity as Context, createUI)

  @CallSuper
  override fun initialize() {
    super<ManagingController>.initialize()
    super<ContextController>.initialize()
  }

  @CallSuper
  override fun start() {
    super<ManagingController>.start()
{'fraction_non_alphanumeric': 0.11879640484564283, 'fraction_numerical': 0.0, 'mean_word_length': 4.113886113886114, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 45, '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': '3835733', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1462, 'n_words': 387}