react-native-move-sdk 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,8 +4,7 @@ import android.content.Context
4
4
  import android.content.SharedPreferences
5
5
  import io.dolphin.move.DrivingService
6
6
  import io.dolphin.move.MoveAuth
7
- import io.dolphin.move.OtherService
8
- import io.dolphin.move.TimelineDetectionService
7
+ import io.dolphin.move.MoveDetectionService
9
8
  import io.dolphin.move.WalkingService
10
9
 
11
10
  private const val SHARED_PREF_NAME = "move-react"
@@ -13,14 +12,12 @@ private const val SHARED_PREF_NAME = "move-react"
13
12
  private const val PROPERTY_TIMELINE_SERVICES = "timelineDetectionServices"
14
13
  private const val PROPERTY_DRIVING_SERVICES = "drivingServices"
15
14
  private const val PROPERTY_WALKING_SERVICES = "walkingServices"
16
- private const val PROPERTY_OTHER_SERVICES = "otherServices"
17
15
 
18
16
  private const val PROPERTY_PRODUCT_ID = "productId"
19
- private const val PROPERTY_CONTRACT_ID = "contractId"
17
+ private const val PROPERTY_USER_ID = "contractId"
20
18
  private const val PROPERTY_ACCESS_TOKEN = "accessToken"
21
19
  private const val PROPERTY_REFRESH_TOKEN = "refreshToken"
22
20
 
23
- private const val PROPERTY_ALLOW_MOCK = "allowMockLocations"
24
21
  private const val PROPERTY_STARTED = "isInRunningState"
25
22
 
26
23
  private const val PROPERTY_NOTIFICATION_TITLE = "NotificationTitle"
@@ -31,164 +28,176 @@ private const val PROPERTY_NOTIFICATION_CHANNELDESCRIPTION = "NotificationChanne
31
28
 
32
29
  private const val NOTIFICATION_RECOGNITION = "recognition"
33
30
  private const val NOTIFICATION_TRIP = "trip"
31
+ private const val NOTIFICATION_WALKING = "walking"
34
32
 
35
33
  class MoveSdkConfigRepository(context: Context) {
36
34
 
37
- private val sharedPreferences: SharedPreferences =
38
- context.applicationContext.getSharedPreferences(
39
- SHARED_PREF_NAME,
40
- Context.MODE_PRIVATE
41
- )
42
-
43
- fun storeConfig(config: MoveSdkConfig) {
44
-
45
- // Persist data for native init next app start
46
- val editor = sharedPreferences.edit()
47
-
48
- storeAuth(config.auth)
49
-
50
- editor.putStringSet(
51
- PROPERTY_TIMELINE_SERVICES,
52
- config.timelineDetectionServices.map { it.toString() }.toSet()
53
- )
54
- editor.putStringSet(
55
- PROPERTY_DRIVING_SERVICES,
56
- config.drivingServices.map { it.toString() }.toSet()
57
- )
58
- editor.putStringSet(
59
- PROPERTY_WALKING_SERVICES,
60
- config.walkingServices.map { it.toString() }.toSet()
61
- )
62
- editor.putStringSet(
63
- PROPERTY_OTHER_SERVICES,
64
- config.otherServices.map { it.toString() }.toSet()
65
- )
66
-
67
- editor.putBoolean(PROPERTY_ALLOW_MOCK, config.allowMockLocations)
68
- editor.apply()
69
-
70
- // Platform config
71
- val recognitionNotification = config.recognitionNotification
72
- storeNotificationConfig(NOTIFICATION_RECOGNITION, recognitionNotification)
73
-
74
- val tripNotification = config.tripNotification
75
- storeNotificationConfig(NOTIFICATION_TRIP, tripNotification)
76
- }
77
-
78
- private fun storeNotificationConfig(
79
- prefix: String,
80
- recognitionNotification: MoveNotificationConfig
81
- ) {
82
- val editor = sharedPreferences.edit()
83
- editor.putString(prefix + PROPERTY_NOTIFICATION_TITLE, recognitionNotification.title)
84
- editor.putString(prefix + PROPERTY_NOTIFICATION_TEXT, recognitionNotification.text)
85
- editor.putString(
86
- prefix + PROPERTY_NOTIFICATION_CHANNELID,
87
- recognitionNotification.channelId
88
- )
89
- editor.putString(
90
- prefix + PROPERTY_NOTIFICATION_CHANNELNAME,
91
- recognitionNotification.channelName
92
- )
93
- editor.putString(
94
- prefix + PROPERTY_NOTIFICATION_CHANNELDESCRIPTION,
95
- recognitionNotification.channelDescription
96
- )
97
- editor.apply()
98
- }
99
-
100
- fun storeAuth(auth: MoveAuth) {
101
- // Persist data for native init next app start
102
- val editor = sharedPreferences.edit()
103
- editor.putInt(PROPERTY_PRODUCT_ID, auth.productId.toInt())
104
- editor.putString(PROPERTY_CONTRACT_ID, auth.contractId)
105
- editor.putString(PROPERTY_ACCESS_TOKEN, auth.accessToken)
106
- editor.putString(PROPERTY_REFRESH_TOKEN, auth.refreshToken)
107
- editor.apply()
108
- }
109
-
110
- fun loadConfig(): MoveSdkConfig {
111
-
112
- val productId = sharedPreferences.getInt(PROPERTY_PRODUCT_ID, 0)
113
- val contractId = sharedPreferences.getString(PROPERTY_CONTRACT_ID, "") ?: ""
114
- val accessToken: String = sharedPreferences.getString(PROPERTY_ACCESS_TOKEN, "") ?: ""
115
- val refreshToken = sharedPreferences.getString(PROPERTY_REFRESH_TOKEN, "") ?: ""
116
-
117
- // Config
118
-
119
- val timelineDetectionServices: List<TimelineDetectionService> =
120
- sharedPreferences.getStringSet(PROPERTY_TIMELINE_SERVICES, emptySet())?.map {
121
- TimelineDetectionService.valueOf(it)
122
- } ?: emptyList()
123
-
124
- val drivingServices: List<DrivingService> =
125
- sharedPreferences.getStringSet(PROPERTY_DRIVING_SERVICES, emptySet())?.map {
126
- DrivingService.valueOf(it)
127
- } ?: emptyList()
128
-
129
- val walkingServices: List<WalkingService> =
130
- sharedPreferences.getStringSet(PROPERTY_WALKING_SERVICES, emptySet())?.map {
131
- WalkingService.valueOf(it)
132
- } ?: emptyList()
133
-
134
- val otherServices: List<OtherService> =
135
- sharedPreferences.getStringSet(PROPERTY_OTHER_SERVICES, emptySet())?.map {
136
- OtherService.valueOf(it)
137
- } ?: emptyList()
138
-
139
- // Platform config
140
- val allowMockLocations: Boolean = sharedPreferences.getBoolean(PROPERTY_ALLOW_MOCK, false)
141
-
142
- val recognitionNotification = loadNotificationConfig(NOTIFICATION_RECOGNITION)
143
- val tripNotification = loadNotificationConfig(NOTIFICATION_TRIP)
144
-
145
- return MoveSdkConfig(
146
- auth = MoveAuth(
147
- contractId = contractId,
148
- accessToken = accessToken,
149
- refreshToken = refreshToken,
150
- productId = productId.toLong(),
151
- ),
152
- timelineDetectionServices = timelineDetectionServices,
153
- drivingServices = drivingServices,
154
- walkingServices = walkingServices,
155
- otherServices = otherServices,
156
- tripNotification = tripNotification,
157
- recognitionNotification = recognitionNotification,
158
- allowMockLocations = allowMockLocations
159
- )
160
- }
161
-
162
- private fun loadNotificationConfig(prefix: String): MoveNotificationConfig {
163
- val notificationTitle: String =
164
- sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_TITLE, "") ?: ""
165
- val notificationText: String =
166
- sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_TEXT, "") ?: ""
167
- val notificationChannelId: String =
168
- sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_CHANNELID, "") ?: ""
169
- val notificationChannelName: String =
170
- sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_CHANNELNAME, "") ?: ""
171
- val notificationChannelDescription: String =
172
- sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_CHANNELDESCRIPTION, "") ?: ""
173
-
174
- return MoveNotificationConfig(
175
- title = notificationTitle,
176
- text = notificationText,
177
- channelId = notificationChannelId,
178
- channelName = notificationChannelName,
179
- channelDescription = notificationChannelDescription
180
- )
181
- }
182
-
183
- fun storeRunningState(running: Boolean) {
184
- sharedPreferences.edit().putBoolean(PROPERTY_STARTED, running).apply()
185
- }
186
-
187
- fun loadRunningState(): Boolean {
188
- return sharedPreferences.getBoolean(PROPERTY_STARTED, false)
189
- }
190
-
191
- fun clear() {
192
- sharedPreferences.edit().clear().apply()
193
- }
35
+ private val sharedPreferences: SharedPreferences =
36
+ context.applicationContext.getSharedPreferences(
37
+ SHARED_PREF_NAME,
38
+ Context.MODE_PRIVATE
39
+ )
40
+
41
+ fun storeConfig(config: MoveSdkConfig) {
42
+
43
+ // Persist data for native init next app start
44
+ val editor = sharedPreferences.edit()
45
+ editor.putStringSet(
46
+ PROPERTY_TIMELINE_SERVICES,
47
+ config.timelineDetectionServices.map { service -> service.name() }.toSet()
48
+ )
49
+ editor.apply()
50
+
51
+ storeAuth(config.auth)
52
+
53
+ // Platform config
54
+ val recognitionNotification = config.recognitionNotification
55
+ storeNotificationConfig(NOTIFICATION_RECOGNITION, recognitionNotification)
56
+
57
+ val tripNotification = config.tripNotification
58
+ storeNotificationConfig(NOTIFICATION_TRIP, tripNotification)
59
+
60
+ val walkingNotification = config.walkingNotification
61
+ storeNotificationConfig(NOTIFICATION_WALKING, walkingNotification)
62
+ }
63
+
64
+ private fun storeNotificationConfig(
65
+ prefix: String,
66
+ recognitionNotification: MoveNotificationConfig
67
+ ) {
68
+ val editor = sharedPreferences.edit()
69
+ editor.putString(prefix + PROPERTY_NOTIFICATION_TITLE, recognitionNotification.title)
70
+ editor.putString(prefix + PROPERTY_NOTIFICATION_TEXT, recognitionNotification.text)
71
+ editor.putString(
72
+ prefix + PROPERTY_NOTIFICATION_CHANNELID,
73
+ recognitionNotification.channelId
74
+ )
75
+ editor.putString(
76
+ prefix + PROPERTY_NOTIFICATION_CHANNELNAME,
77
+ recognitionNotification.channelName
78
+ )
79
+ editor.putString(
80
+ prefix + PROPERTY_NOTIFICATION_CHANNELDESCRIPTION,
81
+ recognitionNotification.channelDescription
82
+ )
83
+ editor.apply()
84
+ }
85
+
86
+ fun storeAuth(auth: MoveAuth) {
87
+ // Persist data for native init next app start
88
+ val editor = sharedPreferences.edit()
89
+ editor.putInt(PROPERTY_PRODUCT_ID, auth.projectId.toInt())
90
+ editor.putString(PROPERTY_USER_ID, auth.userId)
91
+ editor.putString(PROPERTY_ACCESS_TOKEN, auth.accessToken)
92
+ editor.putString(PROPERTY_REFRESH_TOKEN, auth.refreshToken)
93
+ editor.apply()
94
+ }
95
+
96
+ fun loadConfig(): MoveSdkConfig {
97
+
98
+ val projectId = sharedPreferences.getInt(PROPERTY_PRODUCT_ID, 0)
99
+ val userId = sharedPreferences.getString(PROPERTY_USER_ID, "") ?: ""
100
+ val accessToken: String = sharedPreferences.getString(PROPERTY_ACCESS_TOKEN, "") ?: ""
101
+ val refreshToken = sharedPreferences.getString(PROPERTY_REFRESH_TOKEN, "") ?: ""
102
+
103
+ // Config
104
+
105
+ /*
106
+ val drivingServices: List<DrivingService> =
107
+ sharedPreferences.getStringSet(PROPERTY_DRIVING_SERVICES, emptySet())?.map { service ->
108
+ DrivingService.valueOf(service)
109
+ } ?: emptyList()
110
+
111
+ val walkingServices: List<WalkingService> =
112
+ sharedPreferences.getStringSet(PROPERTY_WALKING_SERVICES, emptySet())?.map { service ->
113
+ WalkingService.valueOf(service)
114
+ } ?: emptyList()
115
+
116
+ val timelineDetectionServices: List<MoveDetectionService> = emptyList()
117
+ sharedPreferences.getStringSet(PROPERTY_TIMELINE_SERVICES, emptySet())?.map { service ->
118
+ valueOfMoveDetectionService(service, drivingServices, walkingServices)
119
+ } ?: emptyList()
120
+ */
121
+
122
+ val recognitionNotification = loadNotificationConfig(NOTIFICATION_RECOGNITION)
123
+ val tripNotification = loadNotificationConfig(NOTIFICATION_TRIP)
124
+ val walkingNotification = loadNotificationConfig(NOTIFICATION_WALKING)
125
+
126
+ return MoveSdkConfig(
127
+ auth = MoveAuth(
128
+ userId = userId,
129
+ accessToken = accessToken,
130
+ refreshToken = refreshToken,
131
+ projectId = projectId.toLong(),
132
+ ),
133
+ timelineDetectionServices = emptyList(),
134
+ tripNotification = tripNotification,
135
+ recognitionNotification = recognitionNotification,
136
+ walkingNotification = walkingNotification
137
+ )
138
+ }
139
+
140
+ private fun loadNotificationConfig(prefix: String): MoveNotificationConfig {
141
+ val notificationTitle: String =
142
+ sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_TITLE, "") ?: ""
143
+ val notificationText: String =
144
+ sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_TEXT, "") ?: ""
145
+ val notificationChannelId: String =
146
+ sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_CHANNELID, "") ?: ""
147
+ val notificationChannelName: String =
148
+ sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_CHANNELNAME, "") ?: ""
149
+ val notificationChannelDescription: String =
150
+ sharedPreferences.getString(prefix + PROPERTY_NOTIFICATION_CHANNELDESCRIPTION, "") ?: ""
151
+
152
+ return MoveNotificationConfig(
153
+ title = notificationTitle,
154
+ text = notificationText,
155
+ channelId = notificationChannelId,
156
+ channelName = notificationChannelName,
157
+ channelDescription = notificationChannelDescription
158
+ )
159
+ }
160
+
161
+ fun storeRunningState(running: Boolean) {
162
+ sharedPreferences.edit().putBoolean(PROPERTY_STARTED, running).apply()
163
+ }
164
+
165
+ fun loadRunningState(): Boolean {
166
+ return sharedPreferences.getBoolean(PROPERTY_STARTED, false)
167
+ }
168
+
169
+ fun clear() {
170
+ sharedPreferences.edit().clear().apply()
171
+ }
194
172
  }
173
+
174
+ /**
175
+ * Resolve a sealed object by its name
176
+ */
177
+ internal fun valueOfMoveDetectionService(
178
+ it: String,
179
+ drivingServices: List<DrivingService>,
180
+ walkingServices: List<WalkingService>
181
+ ) =
182
+ if (it.equalsService(MoveDetectionService.Driving())) {
183
+ MoveDetectionService.Driving(drivingServices)
184
+ } else if (it.equalsService(MoveDetectionService.Walking())) {
185
+ MoveDetectionService.Walking(walkingServices)
186
+ } else if (it.equalsService(MoveDetectionService.Cycling)) {
187
+ MoveDetectionService.Cycling
188
+ } else if (it.equalsService(MoveDetectionService.Places)) {
189
+ MoveDetectionService.Places
190
+ } else if (it.equalsService(MoveDetectionService.PublicTransport)) {
191
+ MoveDetectionService.PublicTransport
192
+ } else if (it.equalsService(MoveDetectionService.PointsOfInterest)) {
193
+ MoveDetectionService.PointsOfInterest
194
+ } else if (it.equalsService(MoveDetectionService.AssistanceCall)) {
195
+ MoveDetectionService.AssistanceCall
196
+ } else if (it.equalsService(MoveDetectionService.AutomaticImpactDetection)) {
197
+ MoveDetectionService.AutomaticImpactDetection
198
+ } else {
199
+ // default case?
200
+ throw IllegalArgumentException("$it is not known, use one of " + MoveDetectionService::class.sealedSubclasses.joinToString { kClass ->
201
+ kClass.simpleName?.toSnakeCase() ?: ""
202
+ })
203
+ }