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
|
package net.lacolaco.smileessence
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import androidx.annotation.StringRes
import android.widget.Toast
import com.squareup.leakcanary.LeakCanary
class Application : android.app.Application() {
private lateinit var dbOpenHelper: DbOpenHelper
val db
get() = dbOpenHelper.writableDatabase!!
override fun onCreate() {
super.onCreate()
if (LeakCanary.isInAnalyzerProcess(this))
return
LeakCanary.install(this)
dbOpenHelper = DbOpenHelper(this)
instance = this
World.load()
}
companion object {
lateinit var instance: Application
private set
fun toast(@StringRes id: Int) {
Toast.makeText(instance, id, Toast.LENGTH_LONG).show()
}
fun toast(text: String) {
Toast.makeText(instance, text, Toast.LENGTH_LONG).show()
}
var currentWorld: World? = null
}
private class DbOpenHelper(context: Context) :
SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
override fun onCreate(db: SQLiteDatabase) {
// Schema v1
db.execSQL("""
CREATE TABLE profiles (
id INTEGER PRIMARY KEY,
oauth_token TEXT NOT NULL,
oauth_token_secret TEXT NOT NULL,
screen_name TEXT NOT NULL,
profile_image_url TEXT NOT NULL,
theme INTEGER NOT NULL,
theme_color INTEGER NOT NULL,
page_infos TEXT NOT NULL
)
""")
onUpgrade(db, 1, DATABASE_VERSION)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
if (oldVersion < 2) {
db.execSQL("""
ALTER TABLE profiles
ADD COLUMN twitter_event_stream_endpoint TEXT
""")
db.execSQL("""
UPDATE profiles
SET twitter_event_stream_endpoint="https://userstream.herokuapp.com/stream"
""")
}
}
companion object {
private const val DATABASE_NAME = "main.db"
private const val DATABASE_VERSION = 2
}
}
}
|