Skip to content

Instantly share code, notes, and snippets.

View code-twister's full-sized avatar

Mihaly Nagy code-twister

View GitHub Profile
@code-twister
code-twister / Injection.kt
Last active March 18, 2022 09:06
Simple Kotlin Dependency Injection
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
/*
* Using the Injection framework:
*
* Create bindings somewhere in the application before the injections would occur.
*
* factory<MyInterface>(named = "specialName") { SomeImplementation() }
@code-twister
code-twister / tp.kt
Created April 10, 2019 14:32
TP Kotlin Injection by delegate idea
package delegation
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class Foo: Injectable by ScopeWrapper() {
val bar: Bar by inject()
}
@code-twister
code-twister / dragons.kt
Last active April 12, 2019 14:14
Kotlin DSL example
class Village(val name: String, val huts: MutableList<Hut>)
class Hut(val address: String,
val vikings: MutableList<Viking>,
val dragons: MutableList<Dragon>)
class Viking(val name: String)
class Dragon(val name: String)
@code-twister
code-twister / dslexample.kt
Created February 21, 2019 23:10
Kotlin DSL Example
data class AddresBook(val contactGroups: List<ContactGroup>, val contacts: List<Contact>)
data class Contact(val name: String, val phones: List<String>)
data class ContactGroup(val name: String, val contacts: List<Contact>)
fun addressBook(lambda: AddressBookBuilder.()->Unit) = AddressBookBuilder().apply(lambda).build()
@DslMarker
annotation class AdressBookDslMarker
@code-twister
code-twister / ProxyExample.java
Created February 2, 2018 15:22
An example showing how to create dynamic proxies for interfaces.
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class ProxyExample {
public static void main(String[] args) {
// Setup... this is where the magic happens...
ApiInterface service = (ApiInterface) Proxy.newProxyInstance(
ApiInterface.class.getClassLoader(),
new Class[]{ ApiInterface.class },
@code-twister
code-twister / build.gradle
Created January 9, 2018 15:18
Gradle build file for Android Studio plugin with custom ide sandbox
...
intellij {
version '2017.1'
pluginName 'YOUR PLUGIN NAME HERE'
plugins 'android'
updateSinceUntilBuild false
alternativeIdePath "/Applications/Android Studio 3.0 Preview.app/Contents/"
}
@code-twister
code-twister / TestProjectComponent.java
Created January 8, 2018 10:34
Test project component for Android Studio plugin
package com.example.plugin;
import com.intellij.openapi.components.ProjectComponent;
import org.jetbrains.annotations.NotNull;
public class TestProjectComponent implements ProjectComponent {
private static final String TEST_PROJECT_COMPONENT_NAME = "TestProjectComponent";
@Override
@code-twister
code-twister / plugin.xml
Created January 8, 2018 10:07
plugin xml for Android Studio
<idea-plugin>
<id>com.example.plugin</id>
<name>ExamplePlugin</name>
<vendor email="test@example.com" url="http://www.example.com">Example</vendor>
<description><![CDATA[
Some description of the plugin, what it does, so people can have a better idea if they want to use it
]]></description>
<change-notes><![CDATA[
@code-twister
code-twister / build.gradle
Created January 8, 2018 09:49
Gradle build file for Android Studio plugin
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
maven {
url 'http://dl.bintray.com/jetbrains/intellij-plugin-service'
}
}
@code-twister
code-twister / ComposableBuilder.java
Created November 17, 2017 10:51
Experiment trying to create a builder pattern which enforces certain params and others being optional, that's also inheritable.
public class Activity1$$IntentBuilder<NEXT> {
protected Map<String, String> params = new HashMap<>();
public Activity1FirstParam createActivity1() {
return new Activity1FirstParam();
}
protected class Activity1FirstParam {
public AfterExtra1 extra1(String extra1) {