-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathApp.kt
More file actions
134 lines (109 loc) · 3.76 KB
/
App.kt
File metadata and controls
134 lines (109 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.commit451.gitlab
import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex
import com.commit451.gitlab.api.GitLab
import com.commit451.gitlab.api.GitLabFactory
import com.commit451.gitlab.api.OkHttpClientFactory
import com.commit451.gitlab.api.PicassoFactory
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.model.Account
import com.commit451.gitlab.util.FabricUtil
import com.commit451.lift.Lift
import com.jakewharton.threetenabp.AndroidThreeTen
import com.novoda.simplechromecustomtabs.SimpleChromeCustomTabs
import com.squareup.leakcanary.LeakCanary
import com.squareup.picasso.Picasso
import io.reactivex.plugins.RxJavaPlugins
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.greenrobot.eventbus.EventBus
import timber.log.Timber
/**
* App for one time init things and to house singletons
*/
class App : Application() {
companion object {
var bus: EventBus = EventBus.getDefault()
var authenticated : Boolean = false
private lateinit var instance: App
fun bus(): EventBus {
return bus
}
fun get(): App {
return instance
}
}
lateinit var gitLab: GitLab
lateinit var currentAccount: Account
lateinit var picasso: Picasso
override fun onCreate() {
super.onCreate()
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return
}
setupLeakCanary()
instance = this
RxJavaPlugins.setErrorHandler { error ->
//In case an error cannot be thrown properly anywhere else in the app
Timber.e(error)
}
setupThreeTen()
Prefs.init(this)
setupCrashReporting()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
SimpleChromeCustomTabs.initialize(this)
val accounts = Prefs.getAccounts()
if (!accounts.isEmpty()) {
setAccount(accounts[0])
}
authenticated = false
Lift.track(this)
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
setupMultidex()
}
fun setAccount(account: Account) {
currentAccount = account
//This is kinda weird, but basically, I don't want to see all the annoying logs from bitmap
//decoding since the OkHttpClient is going to log everything, but it does not matter in release
//builds, and will actually speed up the init time to share the same client between all these
val clientBuilder = OkHttpClientFactory.create(account)
if (BuildConfig.DEBUG) {
clientBuilder.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
}
val client = clientBuilder.build()
initGitLab(account, clientBuilder)
if (BuildConfig.DEBUG) {
initPicasso(OkHttpClientFactory.create(account).build())
} else {
initPicasso(client)
}
}
fun getAccount(): Account {
return currentAccount
}
private fun setupMultidex() {
MultiDex.install(this)
}
private fun setupCrashReporting() {
FabricUtil.init(this)
}
private fun setupLeakCanary() {
LeakCanary.install(this)
}
private fun setupThreeTen() {
AndroidThreeTen.init(this)
}
private fun initGitLab(account: Account, clientBuilder: OkHttpClient.Builder) {
gitLab = GitLabFactory.createGitLab(account, clientBuilder)
}
private fun initPicasso(client: OkHttpClient) {
picasso = PicassoFactory.createPicasso(client)
}
}