@@ -29,13 +29,19 @@ import com.google.android.play.core.install.model.InstallStatus
29
29
import com.google.android.play.core.install.model.UpdateAvailability
30
30
import com.google.android.play.core.tasks.Task
31
31
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
32
+ import de.koenidv.sph.SphPlanner.Companion.prefs
32
33
import de.koenidv.sph.database.ChangesDb
33
34
import de.koenidv.sph.database.FunctionTilesDb
34
35
import de.koenidv.sph.debugging.Debugger
35
36
import de.koenidv.sph.networking.NetworkManager
36
37
import de.koenidv.sph.objects.FunctionTile
37
38
import de.koenidv.sph.ui.OnboardingActivity
38
39
import de.koenidv.sph.ui.OptionsSheet
40
+ import kotlinx.coroutines.CoroutineScope
41
+ import kotlinx.coroutines.Dispatchers
42
+ import kotlinx.coroutines.delay
43
+ import kotlinx.coroutines.launch
44
+ import java.util.*
39
45
40
46
// Created by koenidv on 05.12.2020.
41
47
@@ -84,6 +90,47 @@ class MainActivity : AppCompatActivity() {
84
90
}
85
91
}
86
92
93
+
94
+ // If app install is at least 3 days ago,
95
+ // the last share snackbar is at least 12 days ago,
96
+ // and the app has not been shared before,
97
+ // show a snackbar asking the user to share the app after 30 seconds
98
+ if (prefs.getLong(" install_time" , 0 ) == 0L ) {
99
+ prefs.edit().putLong(" install_time" , Date ().time).apply ()
100
+ } else if (Date ().time - prefs.getLong(" install_time" , 0 ) > 3 * 24 * 360 * 1000 &&
101
+ ! prefs.getBoolean(" share_done" , false ) &&
102
+ Date ().time - prefs.getLong(" share_snackbar_time" , 0 ) >
103
+ 12 * 24 * 360 * 1000 ) {
104
+
105
+ CoroutineScope (Dispatchers .Main ).launch {
106
+ delay(30000 )
107
+
108
+ // Create a snackbar with share action
109
+ val snackbar = Snackbar .make(
110
+ findViewById<FragmentContainerView >(R .id.nav_host_fragment),
111
+ R .string.share_prompt, Snackbar .LENGTH_INDEFINITE )
112
+ .setAnchorView(R .id.nav_view)
113
+ .setAction(R .string.share_action) {
114
+ val sendIntent: Intent = Intent ().apply {
115
+ // Action: Share plain text and save action completion in prefs
116
+ action = Intent .ACTION_SEND
117
+ putExtra(Intent .EXTRA_TEXT , getString(R .string.share_text))
118
+ this .type = " text/plain"
119
+ prefs.edit().putBoolean(" share_done" , true ).apply ()
120
+ }
121
+ startActivity(Intent .createChooser(sendIntent, getString(R .string.share_action)))
122
+ }
123
+ snackbar.show()
124
+ prefs.edit().putLong(" share_snackbar_time" , Date ().time).apply ()
125
+
126
+ // Dismiss the snackbar after 10 seconds
127
+ delay(10000 )
128
+ snackbar.dismiss()
129
+
130
+ }
131
+ }
132
+
133
+
87
134
}
88
135
89
136
override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -132,36 +179,40 @@ class MainActivity : AppCompatActivity() {
132
179
/*
133
180
* Pull to refresh
134
181
*/
135
- swipeRefresh = findViewById< SwipeRefreshLayout > (R .id.swipeRefresh)
182
+ swipeRefresh = findViewById(R .id.swipeRefresh)
136
183
swipeRefresh.setOnRefreshListener {
137
184
navController.currentDestination?.id?.let { destination ->
138
185
// If destination is known, let network manager handle the refreshing
139
186
NetworkManager ().handlePullToRefresh(destination, lastNavArguments) { success ->
140
- val errorSnackbar = Snackbar .make(findViewById<FragmentContainerView >(R .id.nav_host_fragment), " " , Snackbar .LENGTH_LONG )
141
- errorSnackbar.setAnchorView(R .id.nav_view)
142
- // Show error message if needed
143
- when (success) {
144
- NetworkManager .FAILED_NO_NETWORK -> errorSnackbar.setText(R .string.error_offline).show()
145
- NetworkManager .FAILED_MAINTENANCE -> errorSnackbar.setText(R .string.error_maintenance).show()
146
- NetworkManager .FAILED_SERVER_ERROR -> errorSnackbar.setText(R .string.error_server).show()
147
- NetworkManager .FAILED_UNKNOWN , NetworkManager .FAILED_CANCELLED ->
148
- errorSnackbar.setText(R .string.error).show()
149
- else -> if (success != NetworkManager .SUCCESS ) errorSnackbar.setText(R .string.error).show()
150
- }
151
- // If this is due to a server error, display a link to sph's status page
152
- if (success == NetworkManager .FAILED_MAINTENANCE
153
- || success == NetworkManager .FAILED_SERVER_ERROR
154
- || success == NetworkManager .FAILED_UNKNOWN ) {
155
- errorSnackbar.setAction(R .string.sph_status) {
156
- startActivity(Intent (Intent .ACTION_VIEW , Uri .parse(getString(R .string.url_status))))
187
+ // Just to make sure we're actually running on ui thread after networking
188
+ // issue fc-21432b0dd857aa2a3e29e938109bf74c to be specific
189
+ runOnUiThread {
190
+ val errorSnackbar = Snackbar .make(findViewById<FragmentContainerView >(R .id.nav_host_fragment), " " , Snackbar .LENGTH_LONG )
191
+ errorSnackbar.setAnchorView(R .id.nav_view)
192
+ // Show error message if needed
193
+ when (success) {
194
+ NetworkManager .FAILED_NO_NETWORK -> errorSnackbar.setText(R .string.error_offline).show()
195
+ NetworkManager .FAILED_MAINTENANCE -> errorSnackbar.setText(R .string.error_maintenance).show()
196
+ NetworkManager .FAILED_SERVER_ERROR -> errorSnackbar.setText(R .string.error_server).show()
197
+ NetworkManager .FAILED_UNKNOWN , NetworkManager .FAILED_CANCELLED ->
198
+ errorSnackbar.setText(R .string.error).show()
199
+ else -> if (success != NetworkManager .SUCCESS ) errorSnackbar.setText(R .string.error).show()
200
+ }
201
+ // If this is due to a server error, display a link to sph's status page
202
+ if (success == NetworkManager .FAILED_MAINTENANCE
203
+ || success == NetworkManager .FAILED_SERVER_ERROR
204
+ || success == NetworkManager .FAILED_UNKNOWN ) {
205
+ errorSnackbar.setAction(R .string.sph_status) {
206
+ startActivity(Intent (Intent .ACTION_VIEW , Uri .parse(getString(R .string.url_status))))
207
+ }
208
+ }
209
+ // Indicate no longer refreshing
210
+ swipeRefresh.isRefreshing = false
211
+ if (success == NetworkManager .FAILED_INVALID_CREDENTIALS ) {
212
+ // Credentials seem to be invalid
213
+ // Show sign in screen
214
+ prefs.edit().remove(" credsVerified" ).apply ()
157
215
}
158
- }
159
- // Indicate no longer refreshing
160
- swipeRefresh.isRefreshing = false
161
- if (success == NetworkManager .FAILED_INVALID_CREDENTIALS ) {
162
- // Credentials seem to be invalid
163
- // Show sign in screen
164
- prefs.edit().remove(" credsVerified" ).apply ()
165
216
}
166
217
}
167
218
}
0 commit comments