- A
dependency
is a coupling between two classes usually because one of them uses the other to do something. Dependency injection
consists of passing dependencies (inject them) via constructor in order to extract the task of creating modules out from other modules. Objects are instantiated somewhere else and passed as constructor attributes when creating the current object.- A
dependency injector
is another module in our app that is in charge of providing instances of the rest of modules and inject their dependencies. It's responsibility is the creation of modules is localized in a single point in our app, and we have full control over it. Dagger
is a dependency injector designed for low-end devices. Most dependency injectors rely on reflection to create and inject dependencies. Reflection is very time consuming on low-end devices, and specially on old android versions. Dagger, however, uses a pre-compiler that creates all the classes it needs to work. That way, no reflection is needed. Dagger is
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has | |
* already been handled. | |
* | |
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled. | |
*/ | |
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> { | |
override fun onChanged(event: Event<T>?) { | |
event?.getContentIfNotHandled()?.let { value -> | |
onEventUnhandledContent(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Hasher { | |
fun hash(): String { | |
val bytes = this.toString().toByteArray() | |
val md = MessageDigest.getInstance("SHA-256") | |
val digest = md.digest(bytes) | |
return digest.fold("", { str, it -> str + "%02x".format(it) }) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String... args) { | |
AtomicInteger a = new AtomicInteger(); | |
Observable.just(1, 2, 3, 4, 5, 6, 7, 8) | |
.repeatWhen(notification -> { | |
return notification.flatMap(o -> { | |
System.out.println("'repeatWhen'"); | |
if(a.getAndAdd(1) < 10){ | |
return Observable.just(o).delay(1,TimeUnit.SECONDS); |
Centralize the support libraries dependencies in gradle
Working with multi-modules project, it is very useful to centralize the dependencies, especially the support libraries.
A very good way is to separate gradle build files, defining something like:
root
--gradleScript
----dependencies.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileDescriptor; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.io.PrintStream; | |
public class HelloWorld{ | |
private static HelloWorld instance; | |
public static void main(String[] args){ | |
instantiateHelloWorldMainClassAndRun(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mt.bnpp.com.bnpp_mt_android_bleu.utils; | |
import android.text.InputFilter; | |
import android.text.Spanned; | |
/** | |
* Created by A591108 on 05/11/2014. | |
*/ | |
public class DecimalFilter implements InputFilter { |