<commit_before>package io.taig.android.content.contract

import java.lang.reflect.InvocationTargetException

import android.os.Bundle
import android.view.View
import io.taig.android.content.fragment.Fragment

import scala.language.postfixOps

/**
 * A Fragment may be a Creditor, loosely forcing the hosting Activity to implement its contract
 */
trait Creditor[+C <: Contract] extends Fragment {
    private val name = getClass.getSimpleName

    private var target: Any = null

    override def onAttach( activity: android.app.Activity ) = {
        super.onAttach( activity )

        val path = {
            val debtor = activity.getClass.getName
            val base = debtor.substring( 0, debtor.lastIndexOf( "." ) )
            val creditor = getClass.getName
            val nested = creditor.substring( 0, creditor.lastIndexOf( "." ) )

            val path = if ( nested.startsWith( base ) && base != nested ) {
                nested.substring( base.length + 1 ).split( "\\." )
            } else {
                Array.empty[String]
            }

            "Contract" +: path :+ getClass.getSimpleName
        }

        try {
            target = path.foldLeft[Any]( activity ) {
                case ( obj, name ) ⇒
                    val method = obj.getClass.getDeclaredMethod( name )
                    method.setAccessible( true )
                    method.invoke( obj )
            }.asInstanceOf[C]
        } catch {
            case _: NoSuchMethodException |
                _: IllegalAccessException |
                _: IllegalArgumentException |
                _: InvocationTargetException |
                _: ClassCastException ⇒
                throw new IllegalStateException(
                    s"Activity ${activity.getClass.getName} did not properly implement contract ${path.mkString( "." )}"
                )
        }
    }

    override def onViewCreated( view: View, state: Option[Bundle] ) = {
        super.onViewCreated( view, state )

        ->> onViewCreated
    }

    override def onStart() = {
        super.onStart()

        ->> onStart
    }

    override def onResume() = {
        super.onResume()

        ->> onResume
    }

    override def onStop() = {
        super.onStop()

        ->> onStop<commit_msg>Allow to override the contract name<commit_after>package io.taig.android.content.contract

import java.lang.reflect.InvocationTargetException

import android.os.Bundle
import android.view.View
import io.taig.android.content.fragment.Fragment

import scala.language.postfixOps

/**
 * A Fragment may be a Creditor, loosely forcing the hosting Activity to implement its contract
 */
trait Creditor[+C <: Contract] extends Fragment {
    private var target: Any = null

    /**
     * Identifier that is expected to host the contract implementation in the debtor Activity
     * 
     * E.g. <code>"fragment.problem.Request"</code> forces the Activity to implement:
     * {{{
     * object Contract {
     *     object fragment {
     *         object problem {
     *             object Request extends C { ... }
     *         }
     *     }
     * }
     * }}}
     */
    def contract: String = {
        val debtor = context.getClass.getName
        val base = debtor.substring( 0, debtor.lastIndexOf( "." ) )
        val creditor = getClass.getName
        val nested = creditor.substring( 0, creditor.lastIndexOf( "." ) )

        val path: Option[String] = if ( nested.startsWith( base ) && base != nested ) {
            Some( nested.substring( base.length + 1 ) )
        } else {
            None
        }

        s"Contract.${path.map( _ + "." ).getOrElse( "" )}${getClass.getSimpleName}"
    }

    override def onAttach( activity: android.app.Activity ) = {
        super.onAttach( activity )

        try {
            target = contract.split( "\\." ).foldLeft[Any]( activity ) {
                case ( obj, name ) ⇒
                    val method = obj.getClass.getDeclaredMethod( name )
                    method.setAccessible( true )
                    method.invoke( obj )
            }.asInstanceOf[C]
        } catch {
            case _: NoSuchMethodException |
                _: IllegalAccessException |
                _: IllegalArgumentException |
                _: InvocationTargetException |
                _: ClassCastException ⇒
                throw new IllegalStateException(
                    s"Activity ${activity.getClass.getName} did not properly implement contract $contract"
                )
        }
    }

    override def onViewCreated( view: View, state: Option[Bundle] ) = {
        super.onViewCreated( view, state )

        ->> onViewCreated
    }

    override def onStart() = {
        super.onStart()

        ->> onStart
    }

    override def onResume() = {
        super.onResume()

        ->> onResume
    }

    override def onStop() = {
        super.onStop()

        ->> onStop
{'fraction_non_alphanumeric': 0.1016880211511084, 'fraction_numerical': 0.0012202562538133007, 'mean_word_length': 1.799089356858281, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '137149', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1269, 'n_words': 321}