App.kt 2.35 KB
Newer Older
1 2 3
package ru.terrakok.gitlabclient

import android.app.Application
4
import android.graphics.Color
5
import com.jakewharton.threetenabp.AndroidThreeTen
6 7
import ru.noties.markwon.SpannableConfiguration
import ru.noties.markwon.spans.SpannableTheme
8
import ru.terrakok.gitlabclient.model.data.auth.AuthHolder
9
import ru.terrakok.gitlabclient.toothpick.DI
10
import ru.terrakok.gitlabclient.toothpick.module.AppModule
11
import ru.terrakok.gitlabclient.toothpick.module.ServerModule
12
import timber.log.Timber
13
import toothpick.Toothpick
14 15 16
import toothpick.configuration.Configuration
import toothpick.registries.FactoryRegistryLocator
import toothpick.registries.MemberInjectorRegistryLocator
17

18 19 20 21 22 23 24 25 26
/**
 * @author Konstantin Tskhovrebov (aka terrakok) on 26.03.17.
 */
class App : Application() {

    override fun onCreate() {
        super.onCreate()

        initLogger()
27
        initToothpick()
28
        initAppScope()
29
        initMarkwon()
30
        initThreetenABP()
31 32 33 34 35 36 37 38
    }

    private fun initLogger() {
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }
    }

39 40 41 42 43 44 45 46 47 48
    private fun initToothpick() {
        if (BuildConfig.DEBUG) {
            Toothpick.setConfiguration(Configuration.forDevelopment().preventMultipleRootScopes())
        } else {
            Toothpick.setConfiguration(Configuration.forProduction().disableReflection())
            FactoryRegistryLocator.setRootRegistry(ru.terrakok.gitlabclient.FactoryRegistry())
            MemberInjectorRegistryLocator.setRootRegistry(ru.terrakok.gitlabclient.MemberInjectorRegistry())
        }
    }

49
    private fun initAppScope() {
50 51 52 53 54 55 56
        val appScope = Toothpick.openScope(DI.APP_SCOPE)
        appScope.installModules(AppModule(this))

        //By default we need init ServerScope for launch app
        val authHolder = appScope.getInstance(AuthHolder::class.java)
        val serverScope = Toothpick.openScopes(DI.APP_SCOPE, DI.SERVER_SCOPE)
        serverScope.installModules(ServerModule(authHolder.serverPath))
57
    }
58

59 60
    private fun initMarkwon() {
        val theme = SpannableTheme.builderWithDefaults(this)
Konstantin Tskhovrebov's avatar
Konstantin Tskhovrebov committed
61 62 63
            .codeTextColor(Color.parseColor("#C0341D"))
            .codeBackgroundColor(Color.parseColor("#FCEDEA"))
            .build()
64
        SpannableConfiguration.builder(this)
Konstantin Tskhovrebov's avatar
Konstantin Tskhovrebov committed
65 66
            .theme(theme)
            .build()
67
    }
68 69 70 71

    private fun initThreetenABP() {
        AndroidThreeTen.init(this)
    }
72
}