@@ -2,15 +2,30 @@ package de.koenidv.sph
2
2
3
3
import android.app.Application
4
4
import android.content.Context
5
+ import android.content.SharedPreferences
6
+ import android.widget.Toast
5
7
import com.androidnetworking.AndroidNetworking
6
8
import com.facebook.stetho.Stetho
7
9
import com.facebook.stetho.okhttp3.StethoInterceptor
8
10
import com.google.firebase.analytics.FirebaseAnalytics
9
11
import com.google.firebase.ktx.Firebase
10
12
import com.google.firebase.remoteconfig.ktx.remoteConfig
13
+ import com.google.gson.Gson
14
+ import com.google.gson.reflect.TypeToken
11
15
import de.koenidv.sph.database.CoursesDb
16
+ import de.koenidv.sph.database.FunctionTilesDb
17
+ import de.koenidv.sph.database.UsersDb
12
18
import de.koenidv.sph.networking.CookieStore
19
+ import de.koenidv.sph.networking.Holidays
20
+ import de.koenidv.sph.networking.Messages
21
+ import de.koenidv.sph.networking.NetworkManager
22
+ import de.koenidv.sph.objects.FunctionTile
23
+ import kotlinx.coroutines.CoroutineScope
24
+ import kotlinx.coroutines.Dispatchers
25
+ import kotlinx.coroutines.launch
13
26
import okhttp3.OkHttpClient
27
+ import java.util.*
28
+
14
29
15
30
// Created by koenidv on 05.12.2020.
16
31
class SphPlanner : Application () {
@@ -33,11 +48,31 @@ class SphPlanner : Application() {
33
48
34
49
// Set application tag for Log
35
50
const val TAG = " SPH-Planner"
51
+
52
+ lateinit var prefs: SharedPreferences
53
+ lateinit var cacheprefs: SharedPreferences
54
+
55
+ /* *
56
+ * Store some processed values from the db in sharedprefs
57
+ */
58
+ fun saveCache () {
59
+ // Users
60
+ if (cacheprefs.getLong(" users_time" , 0 ) == 0L ) {
61
+ cacheprefs.edit().putLong(" users_time" , Date ().time).apply ()
62
+ }
63
+ if (cacheprefs.getString(" users_locale" , " " ) != Locale .getDefault().language) {
64
+ cacheprefs.edit().putString(" users_locale" , Locale .getDefault().language).apply ()
65
+ }
66
+ cacheprefs.edit().putString(" users_cache" , Gson ().toJson(UsersDb .cache)).apply ()
67
+ }
36
68
}
37
69
38
70
override fun onCreate () {
39
71
super .onCreate()
40
72
73
+ prefs = getSharedPreferences(" sharedPrefs" , Context .MODE_PRIVATE )
74
+ cacheprefs = getSharedPreferences(" cache" , Context .MODE_PRIVATE )
75
+
41
76
// Apply default remote configs
42
77
Firebase .remoteConfig.setDefaultsAsync(R .xml.remote_config_defaults)
43
78
@@ -53,38 +88,78 @@ class SphPlanner : Application() {
53
88
.build()
54
89
AndroidNetworking .initialize(applicationContext(), okHttpClient)
55
90
91
+ upgrade()
92
+
93
+ CoroutineScope (Dispatchers .IO ).launch {
94
+ // Store some processed values from the db in sharedprefs
95
+ // Only restore if not older than a week and same locale
96
+ if (Date ().time - cacheprefs.getLong(" users_time" , 0 ) <
97
+ 7 * 24 * 360 * 1000 &&
98
+ cacheprefs.getString(" users_locale" , " " ) ==
99
+ Locale .getDefault().language) {
100
+
101
+ val userstype = object : TypeToken <Map <String , Map <String , String >>>() {}.type
102
+ UsersDb .cache = Gson ().fromJson(
103
+ cacheprefs.getString(" users_cache" , " " ), userstype)
104
+ }
105
+ }
106
+ }
56
107
57
- // Upgrade from previous version
108
+ // Upgrade from previous version
109
+ private fun upgrade () {
58
110
val prefs = getSharedPreferences(" sharedPrefs" , Context .MODE_PRIVATE )
59
- // Upgrade to 120 if user is signed in
60
- if (prefs.getBoolean(" introComplete" , false ) &&
61
- prefs.getInt(" appVersion" , 0 ) < 120 ) {
62
-
63
- // Set new analytics properties
64
- val analytics = FirebaseAnalytics .getInstance(this )
65
- // Log school id GA as user property
66
- analytics.setUserProperty(
67
- " school" ,
68
- prefs.getString(" schoolid" , " 0" )!! )
69
- // Log an school course id example to GA
70
- analytics.setUserProperty(
71
- " courseIdExample" ,
72
- CoursesDb .getInstance().gmbIdExample)
73
-
74
- // Attachments were moved to files/attachments/
75
- // Delete all attachments in the old directory, but keep Firebase files
76
- val files = filesDir.listFiles()
77
- if (files != null ) {
78
- for (f in files) {
79
- if (! f.name.startsWith(" generate" ) &&
80
- ! f.name.startsWith(" Persisted" ) &&
81
- ! f.name.startsWith(" frc" )) {
82
- f.delete()
111
+
112
+ if (prefs.getBoolean(" introComplete" , false )) {
113
+
114
+ // 120: GA props
115
+ if (prefs.getInt(" appVersion" , 0 ) < 120 ) {
116
+ // Set new analytics properties
117
+ val analytics = FirebaseAnalytics .getInstance(this )
118
+ // Log school id GA as user property
119
+ analytics.setUserProperty(
120
+ " school" ,
121
+ prefs.getString(" schoolid" , " 0" )!! )
122
+ // Log an school course id example to GA
123
+ analytics.setUserProperty(
124
+ " courseIdExample" ,
125
+ CoursesDb .getInstance().gmbIdExample)
126
+
127
+ // Attachments were moved to files/attachments/
128
+ // Delete all attachments in the old directory, but keep Firebase files
129
+ val files = filesDir.listFiles()
130
+ if (files != null ) {
131
+ for (f in files) {
132
+ if (! f.name.startsWith(" generate" ) &&
133
+ ! f.name.startsWith(" Persisted" ) &&
134
+ ! f.name.startsWith(" frc" )) {
135
+ f.delete()
136
+ }
83
137
}
84
138
}
139
+
140
+ prefs.edit().putInt(" appVersion" , 120 ).apply ()
85
141
}
86
142
87
- prefs.edit().putInt(" appVersion" , 120 ).apply ()
143
+ // 130: Messages, holidays
144
+ if (prefs.getInt(" appVersion" , 0 ) < 130 ) {
145
+ if (FunctionTilesDb .getInstance().supports(FunctionTile .FEATURE_MESSAGES )) {
146
+ Toast .makeText(this , R .string.upgrading_messages, Toast .LENGTH_LONG ).show()
147
+ NetworkManager ().getOwnUserId {
148
+ Messages ().fetch(archived = true ) {
149
+ Holidays ().fetch {
150
+ Toast .makeText(this , R .string.done, Toast .LENGTH_LONG ).show()
151
+ prefs.edit().putInt(" appVersion" , 130 ).apply ()
152
+ }
153
+ }
154
+ }
155
+ } else {
156
+ NetworkManager ().getOwnUserId {
157
+ Messages ().fetch {
158
+ prefs.edit().putInt(" appVersion" , 130 ).apply ()
159
+ }
160
+ }
161
+ }
162
+ }
88
163
}
89
164
}
90
165
0 commit comments