Skip to content

Commit

Permalink
Version 1.1.0. Refactored kite classes and names, fixed build failure…
Browse files Browse the repository at this point in the history
…, moved extensions to member functions on KiteFetcher.kt to dynamically switch context (closes #1), made global Kite context public, included testing dependencies
  • Loading branch information
cioccarellia committed Dec 12, 2020
1 parent bf320a4 commit e8275c1
Show file tree
Hide file tree
Showing 34 changed files with 197 additions and 202 deletions.
10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kite/.gitignore
@@ -1 +1 @@
/build
/build
6 changes: 4 additions & 2 deletions kite/build.gradle
Expand Up @@ -22,9 +22,11 @@ dependencies {
implementation deps.kotlin.stdlib8
implementation deps.androidx.core

testImplementation deps.test.junit
testImplementation deps.test.androidx_test_core
testImplementation deps.test.androidx_test_runner_espresso
testImplementation deps.kotlin.test.mockito
testImplementation deps.test.robolectric
testImplementation deps.test.junit
testImplementation deps.test.mockito_core
testImplementation deps.test.truth
}
Expand All @@ -36,4 +38,4 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
'-Xexplicit-api=strict',
]
}
}
}
2 changes: 1 addition & 1 deletion kite/src/main/AndroidManifest.xml
@@ -1 +1 @@
<manifest package="com.cioccarellia.kite" />
<manifest package="com.cioccarellia.kite" />
70 changes: 36 additions & 34 deletions kite/src/main/kotlin/com/cioccarellia/kite/Kite.kt
Expand Up @@ -24,153 +24,155 @@ import android.graphics.drawable.Drawable
import android.util.TypedValue
import android.view.animation.Animation
import android.view.animation.Interpolator
import androidx.annotation.RestrictTo
import com.cioccarellia.kite.resparser.KiteCustomResParser
import com.cioccarellia.kite.resparser.KiteResParser
import com.cioccarellia.kite.resparser.compat.KiteColorStateLists
import com.cioccarellia.kite.resparser.compat.KiteColors
import com.cioccarellia.kite.resparser.compat.KiteDrawables
import com.cioccarellia.kite.resparser.context.KiteStrings
import com.cioccarellia.kite.resparser.context.KiteTexts
import com.cioccarellia.kite.resparser.custom.KiteAnimations
import com.cioccarellia.kite.resparser.custom.KiteInterpolators
import com.cioccarellia.kite.resparser.resources.*
import com.cioccarellia.kite.fetchers.CustomKiteFetcher
import com.cioccarellia.kite.fetchers.StandardKiteFetcher
import com.cioccarellia.kite.fetchers.compat.KiteColorStateLists
import com.cioccarellia.kite.fetchers.compat.KiteColors
import com.cioccarellia.kite.fetchers.compat.KiteDrawables
import com.cioccarellia.kite.fetchers.context.KiteStrings
import com.cioccarellia.kite.fetchers.context.KiteTexts
import com.cioccarellia.kite.fetchers.custom.KiteAnimations
import com.cioccarellia.kite.fetchers.custom.KiteInterpolators
import com.cioccarellia.kite.fetchers.resources.*
import java.io.InputStream

public object Kite {
/**
* Initialized Kite
* Initializes Kite and sets the current Kite context
* */
public fun fly(context: Context) {
this.context = context
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
internal lateinit var context: Context
/**
* [Context] used by default to fetch resources
* */
public lateinit var context: Context


/**
* Fetches [String]s from resources.
* [Context.getString()] is used to resolve the id.
* There is also a vararg variant which accepts format arguments, and maps to the appropriate [Context.getString()] function.
* */
public val string: KiteResParser<Int, String> by lazy { KiteStrings() }
public val string: StandardKiteFetcher<Int, String> by lazy { KiteStrings() }

/**
* Fetches [String]s Plurals from resources, given the [String] id and the [Int] quantity.
* [Resources.getQuantityString()] is used to resolve the id.
* There is also a vararg variant which accepts format arguments, and maps to the appropriate [Resources.getQuantityString()] function.
* */
public val plural: KiteCustomResParser<Int, String> by lazy { KitePlurals() }
public val plural: CustomKiteFetcher<Int, String> by lazy { KitePlurals() }

/**
* Fetches [CharSequence] Texts from resources.
* [Context.getText()] is used to resolve the id.
* */
public val text: KiteResParser<Int, CharSequence> by lazy { KiteTexts() }
public val text: StandardKiteFetcher<Int, CharSequence> by lazy { KiteTexts() }

/**
* Fetches color [Int]s from resources.
* [ContextCompat.getColor()] is used to resolve the id.
* */
public val color: KiteResParser<Int, Int> by lazy { KiteColors() }
public val color: StandardKiteFetcher<Int, Int> by lazy { KiteColors() }


/**
* Fetches [ColorStateList]s from resources.
* [ContextCompat.getColorStateList()] is used to resolve the id.
* */
public val colorStateList: KiteResParser<Int, ColorStateList> by lazy { KiteColorStateLists() }
public val colorStateList: StandardKiteFetcher<Int, ColorStateList> by lazy { KiteColorStateLists() }

/**
* Fetches [Boolean]s from resources.
* [Resources.getBoolean()] is used to resolve the id.
* */
public val bools: KiteResParser<Int, Boolean> by lazy { KiteBools() }
public val bools: StandardKiteFetcher<Int, Boolean> by lazy { KiteBools() }

/**
* Fetches ID [Int]s from resources, given the definition type and package.
* [Resources.getIdentifier()] is used to resolve the id.
* */
public val identifier: KiteCustomResParser<String, Int> by lazy { KiteIdentifier() }
public val identifier: CustomKiteFetcher<String, Int> by lazy { KiteIdentifier() }

/**
* Fetches [Drawable]s from resources.
* [ContextCompat.getDrawable()] is used to resolve the id.
* There is also a variant which accepts a [Resources.Theme?] arguments, and maps to the [Resources.getDrawable()] function.
* */
public val drawable: KiteResParser<Int, Drawable> by lazy { KiteDrawables() }
public val drawable: StandardKiteFetcher<Int, Drawable> by lazy { KiteDrawables() }

/**
* Fetches [Animation]s from resources.
* [AnimationUtils.loadAnimation()] is used to resolve the id.
* */
public val animation: KiteResParser<Int, Animation> by lazy { KiteAnimations() }
public val animation: StandardKiteFetcher<Int, Animation> by lazy { KiteAnimations() }

/**
* Fetches [Interpolator]s from resources.
* [AnimationUtils.loadInterpolator()] is used to resolve the id.
* */
public val interpolator: KiteResParser<Int, Interpolator> by lazy { KiteInterpolators() }
public val interpolator: StandardKiteFetcher<Int, Interpolator> by lazy { KiteInterpolators() }

/**
* Fetches [IntArray]s from resources.
* [Resources.getIntArray()] is used to resolve the id.
* */
public val intArray: KiteResParser<Int, IntArray> by lazy { KiteIntArrays() }
public val intArray: StandardKiteFetcher<Int, IntArray> by lazy { KiteIntArrays() }

/**
* Fetches String Arrays ([Array<String>]) from resources.
* [Resources.getStringArray()] is used to resolve the id.
* */
public val stringArray: KiteResParser<Int, Array<out String>> by lazy { KiteStringArrays() }
public val stringArray: StandardKiteFetcher<Int, Array<out String>> by lazy { KiteStringArrays() }

/**
* Fetches [TypedArray]s from resources.
* [Resources.obtainTypedArray()] is used to resolve the id.
* */
public val typedArray: KiteResParser<Int, TypedArray> by lazy { KiteTypedArrays() }
public val typedArray: StandardKiteFetcher<Int, TypedArray> by lazy { KiteTypedArrays() }

/**
* Fetches Dimension [Float]s from resources.
* [Resources.getDimension()] is used to resolve the id.
* */
public val dimension: KiteResParser<Int, Float> by lazy { KiteDimensions() }
public val dimension: StandardKiteFetcher<Int, Float> by lazy { KiteDimensions() }

/**
* Fetches [Int]s from resources.
* [Resources.getInteger()] is used to resolve the id.
* */
public val integer: KiteResParser<Int, Int> by lazy { KiteIntegers() }
public val integer: StandardKiteFetcher<Int, Int> by lazy { KiteIntegers() }

/**
* Fetches fractions [Float]s from resources, given the value base and the parent value base.
* [Resources.getFraction()] is used to resolve the id.
* */
public val fraction: KiteCustomResParser<Int, Float> by lazy { KiteFraction() }
public val fraction: CustomKiteFetcher<Int, Float> by lazy { KiteFraction() }

/**
* Fetches Layout [XmlResourceParser]s from resources.
* [Resources.getLayout()] is used to resolve the id.
* */
public val layout: KiteResParser<Int, XmlResourceParser> by lazy { KiteLayouts() }
public val layout: StandardKiteFetcher<Int, XmlResourceParser> by lazy { KiteLayouts() }

/**
* Fetches [InputStream]s from resources.
* [Resources.openRawResource()] is used to resolve the id.
* There is also a parameterized variant which accepts a [TypedValue], which maps to the appropriate [Resources.openRawResource()] function.
* */
public val raw: KiteResParser<Int, InputStream> by lazy { KiteRaws() }
public val raw: StandardKiteFetcher<Int, InputStream> by lazy { KiteRaws() }

/**
* Fetches Layout [XmlResourceParser]s from resources.
* [Resources.getXml()] is used to resolve the id.
* */
public val xml: KiteResParser<Int, XmlResourceParser> by lazy { KiteXmls() }
public val xml: StandardKiteFetcher<Int, XmlResourceParser> by lazy { KiteXmls() }

/**
* Requires API 26 (O)
* Fetches Layout [Typeface]s from resources.
* [Resources.getFont()] is used to resolve the id.
* */
public val font: KiteResParser<Int, Typeface> by lazy { KiteFonts() }
public val font: StandardKiteFetcher<Int, Typeface> by lazy { KiteFonts() }
}

This file was deleted.

Expand Up @@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cioccarellia.kite.resparser
package com.cioccarellia.kite.fetchers

/**
* [KiteParser] which has precisely 1 input and 1 output
* [KiteFetcher] which is defined to have precisely 1 input type [R] (Resource) and 1 output type [O] (Output)
* */
public abstract class KiteCustomResParser<in R, out O> : KiteParser()
public abstract class CustomKiteFetcher<in R, out O> : KiteFetcher()
41 changes: 41 additions & 0 deletions kite/src/main/kotlin/com/cioccarellia/kite/fetchers/KiteFetcher.kt
@@ -0,0 +1,41 @@
/**
* Designed and developed by Andrea Cioccarelli (@cioccarellia)
*
* 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.cioccarellia.kite.fetchers

import android.content.Context
import androidx.annotation.RestrictTo
import com.cioccarellia.kite.Kite
import com.cioccarellia.kite.internal.switchContext

/**
* Represents a kite fetcher, which is responsible for fetching resources using the Android framework.
* It provides [Context] access to the kite context
* */
public abstract class KiteFetcher {
internal val kiteContext: Context
@RestrictTo(RestrictTo.Scope.LIBRARY)
get() {
return Kite.context
}

/**
* Temporarily switches context to execute the given lambda
*
* @param [context] The context to be temporarily set as the new kite context while the lambda runs
* @param [block] The lambda to be executed
* */
public fun runWith(context: Context, block: (KiteFetcher) -> Unit): KiteFetcher = switchContext(context, block)
}
Expand Up @@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cioccarellia.kite.resparser
package com.cioccarellia.kite.fetchers

/**
* [KiteParser] which has precisely 1 input and 1 output
* [KiteFetcher] which is defined to have precisely 1 input type [R] (Resource) and 1 output type [O] (Output).
* It compatible and backed by the index operator with a canonical signature.
* */
public abstract class KiteResParser<in R, out O> : KiteParser() {
public abstract class StandardKiteFetcher<in R, out O> : KiteFetcher() {
public abstract operator fun get(resource: R): O
}
}
Expand Up @@ -15,18 +15,18 @@
*/
@file:Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")

package com.cioccarellia.kite.resparser.compat
package com.cioccarellia.kite.fetchers.compat

import android.content.res.ColorStateList
import androidx.annotation.ColorRes
import androidx.annotation.IntRange
import androidx.core.content.ContextCompat
import com.cioccarellia.kite.resparser.KiteResParser
import com.cioccarellia.kite.fetchers.StandardKiteFetcher

/**
* KiteColorStateLists Implementation
* [KiteColorStateLists] Implementation
* */
internal class KiteColorStateLists : KiteResParser<@ColorRes Int, ColorStateList>() {
internal class KiteColorStateLists : StandardKiteFetcher<@ColorRes Int, ColorStateList>() {
override operator fun get(
@ColorRes @IntRange(from = 1) colorStateList: Int
): ColorStateList = ContextCompat.getColorStateList(kiteContext, colorStateList)!!
Expand Down

0 comments on commit e8275c1

Please sign in to comment.