A simple Kotlin wrapper around Anvil.

Related tags

Kotlin AnvilKotlin
Overview

AnvilKotlin

A simple Kotlin wrapper around Anvil.

The only purpose of this library is to provide type safety to Anvil through Kotlin. Nothing more, nothing less. Enjoy! :)

Get it

Add the JitPack repo to your root build.gradle file:

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

The possible library strings are:

dependencies {
	// Remember, you still need to set up the equivalent Anvil ones!
	
	// SDK libraries
	compile 'com.github.graknol.AnvilKotlin:anvil:0.4.0:sdk15Release@aar'
	compile 'com.github.graknol.AnvilKotlin:anvil:0.4.0:sdk19Release@aar'
	compile 'com.github.graknol.AnvilKotlin:anvil:0.4.0:sdk21Release@aar'
	
	// Support libraries
	compile 'com.github.graknol.AnvilKotlin:cardview:0.4.0@aar'
	compile 'com.github.graknol.AnvilKotlin:gridlayout:0.4.0@aar'
	compile 'com.github.graknol.AnvilKotlin:recyclerview:0.4.0@aar'
	compile 'com.github.graknol.AnvilKotlin:support:0.4.0@aar'
	compile 'com.github.graknol.AnvilKotlin:design:0.4.0@aar'
	compile 'com.github.graknol.AnvilKotlin:appcompat:0.4.0@aar'
}

How to use it

Use the functions from each library (these are found in graknol.anvil.kotlin) to create views and then use the functions on this, for instance:

val fooContent: Anvil.Renderable.() -> Unit = {
	coordinatorLayout {
		appBarLayout {
			size(MATCH, WRAP)
			toolbar {
				size(MATCH, dip(48))
			}
		}
	}
}

// Just to demonstrate that you can indeed style views by a lambda (think, theme classes with functions like this in it).
val styleNavDrawer: AppCompatDSLListViewCompat.() -> Unit = {
	choiceMode(ListView.CHOICE_MODE_SINGLE)
	divider(resources.getDrawable(android.R.color.transparent))
	dividerHeight(0)
	backgroundColor(hex("#111111"))
}

class ExampleView(c: Context) : RenderableView(c) {
	fun view() {
		linearLayout {
			backgroundColor(someIntColorValue)
			appCompatButton {
				text("click me!")
				onClick(View.OnClickListener {
					doSomethingCool()
				})
			}
			
			drawerLayout {
				// Look at the top of this snippet
				fooContent()
				
				listViewCompat {
					size(dip(240), MATCH)
					// Look at the top of this snippet
					styleNavDrawer() 
				}.lparams {
					gravity = START
					weight = 1f
				}
			}
			
			// If there are functions missing in the DSL, do this little trick:
			with(Anvil.currentView<LinearLayout>()) {
				// Magic!
				whicheverFunctionIsMissing()
				andAllOthers()
			}
		}
	}
}

The reason for the "seemingly redundant" typing of OnClickListener above, is simply that Kotlin does not automatically convert Kotlin parameters to SAMs (Single abstract method), and there are too many functions in the different library for me to patch every single one of them. PS: Some listeners have multiple functions, so you'll end up writing those like this anyway ;)

Splitting the code is ~~ugly not that ugly anymore, YAY :)

Most issues are not present anymore. If there's something bothering you on this area, DO NOT hesitate to open an issue (we really need all the input we can get).

License

Code is distributed under the MIT license, feel free to use it in your proprietary projects as well.

You might also like...
🎲 A powerful and simple-to-use guilded wrapper made in Kotlin.

🎲 deck [WIP] Deck is a powerful yet simple-to-use guilded wrapper made entirely in Kotlin with support to multiplatform. Implementating In case you'r

This library is a set of simple wrapper classes that are aimed to help you easily access android device information.
This library is a set of simple wrapper classes that are aimed to help you easily access android device information.

SysInfo Simple, single class wrapper to get device information from an android device. This library provides an easy way to access all the device info

πŸ”“ Kotlin version of the popular google/easypermissions wrapper library to simplify basic system permissions logic on Android M or higher.
πŸ”“ Kotlin version of the popular google/easypermissions wrapper library to simplify basic system permissions logic on Android M or higher.

EasyPermissions-ktx Kotlin version of the popular googlesample/easypermissions wrapper library to simplify basic system permissions logic on Android M

Android AsyncTask wrapper library, written in Kotlin

KillerTask This is a Kotlin Android library to create async background tasks. Inspired by TinyTask, but more beautiful and easy to use for Kotlin Andr

Netflix inspired OTT Home Screen, Contains implementation in Reactjs, Kotlin React Wrapper, Jetpack Compose Web

Netflix-Clone-React Practising React by building Netflix Clone Requirements TMDB api key : Add TMDB API key to AppApi.kt Learning Resourcce Build Netf

A Kotlin coroutines wrapper for IndexedDB.

Kotlin IndexedDB A wrapper around IndexedDB which allows for access from Kotlin/JS code using suspend blocks and linear, non-callback based control fl

Yaspeller-kt - Asynchronous Yandex.Speller API wrapper for Kotlin/JVM.

yaspeller-kt Asynchronous Yandex.Speller API wrapper for Kotlin/JVM. Installation repositories { maven { url 'https://jitpack.io' }

Permissionmanager is a small wrapper for handling permission requests.

Permissionmanager Permissionmanager is a small wrapper for handling permission requests. Installation Add jitpack to your repositories in Project buil

Dependency Injection library for Compose Multiplatform, Koin wrapper.

πŸ₯₯ Cokoin Injection library for Compose (Multiplatform and Jetpack), Koin wrapper. It uses @Composable functions to configure KoinContext and Scopes.

Owner
Andre Artus
Andre Artus
A lightweight Kotlin friendly wrapper around Couchbase lite for Android.

CouchBaseKtx ?? Work In-Progress ?? A lightweight Kotlin friendly wrapper around Couchbase-lite for Android Read up a little bit of documentation abou

Jaya Surya Thotapalli 5 Feb 15, 2022
SavedStateFlow - A Kotlin StateFlow wrapper around SavedStateHandle

SavedStateFlow SavedStateFlow is a Kotlin StateFlow wrapper around SavedStateHan

Andrew Steinmetz 13 Aug 4, 2022
A simple Kotlin multi-platform abstraction around the javax.inject annotations.

Inject A simple Kotlin multi-platform abstraction around the javax.inject annotations. This allows using the annotations in Kotlin common code so that

Christopher 43 Aug 17, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Practical Kotlin Multiplatform on the Web λ³Έ μ €μž₯μ†ŒλŠ” μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌ 기반 μ›Ή ν”„λ‘œκ·Έλž˜λ° μ›Œν¬μˆ(κ°•μ’Œ)을 μœ„ν•΄ μž‘μ„±λœ ν…œν”Œλ¦Ώ ν”„λ‘œμ νŠΈκ°€ μžˆλŠ” κ³³μž…λ‹ˆλ‹€. μ›Œν¬μˆ κ³Όμ •μ—μ„œ μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌμ„ 기반으둜 ν”„λ‘ νŠΈμ—”λ“œ(front-end)λŠ” Ko

SpringRunner 14 Nov 5, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Building a Full Stack Web App with Kotlin Multiplatform λ³Έ μ €μž₯μ†ŒλŠ” INFCON 2022μ—μ„œ μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌ 기반 μ›Ή ν”„λ‘œκ·Έλž˜λ° ν•Έμ¦ˆμ˜¨λž©μ„ μœ„ν•΄ μž‘μ„±λœ ν…œν”Œλ¦Ώ ν”„λ‘œμ νŠΈκ°€ μžˆλŠ” κ³³μž…λ‹ˆλ‹€. ν•Έμ¦ˆμ˜¨ κ³Όμ •μ—μ„œ μ½”ν‹€λ¦° λ©€ν‹°ν”Œλž«νΌμ„

Arawn Park 19 Sep 8, 2022
A Lightweight PDF Viewer Android library which only occupies around 125kb while most of the Pdf viewer occupies up to 16MB space.

Pdf Viewer For Android A Simple PDF Viewer library which only occupies around 125kb while most of the Pdf viewer occupies upto 16MB space. How to inte

Rajat 362 Dec 29, 2022
Small Kafka Playground to play around with Test Containers, and KotlinX Coroutines bindings while reading Kafka Definite Guide V2

KafkaPlayground Small playground where I'm playing around with Kafka in Kotlin and the Kafka SDK whilst reading the Kafka book Definite Guide from Con

Simon Vergauwen 34 Dec 30, 2022
Develop an API that moves a rover around on a grid

Mars Rover Kata Develop an API that moves a rover around on a grid. Rules: You are given the initial starting 2D point (x,y) of a rover and the direct

Alvaro Martin Rodriguez 1 Dec 17, 2021
DocuBox is a cloud based file storing app where you can securely store and access your documents from anywhere around the world

DocuBox is an android app ??in which you can securely upload your files on the cloud– from family pictures and audio recordings to spreadsheets, presentations and other confidential documents.

Vaibhav Jaiswal 26 Jan 3, 2023
⏰ A powerful and simple-to-use guilded wrapper made in Kotlin.

⏰ guilded-kt [WIP] A powerful yet simple-to-use guilded wrapper made entirely in Kotlin with supporting multiplatform. Take a look at an example of th

Gabriel 13 Jul 30, 2022