1
+ package vocabletrainer.heinecke.aron.vocabletrainer.lib.ViewModel
2
+
3
+ import org.acra.ACRA.errorReporter
4
+ import androidx.lifecycle.ViewModel
5
+ import androidx.lifecycle.MutableLiveData
6
+ import kotlin.jvm.Synchronized
7
+ import org.json.JSONObject
8
+ import android.os.Build
9
+ import android.util.Log
10
+ import androidx.lifecycle.LiveData
11
+ import org.acra.ktx.sendWithAcra
12
+ import vocabletrainer.heinecke.aron.vocabletrainer.BuildConfig
13
+ import java.io.BufferedInputStream
14
+ import java.io.ByteArrayOutputStream
15
+ import java.io.InputStream
16
+ import java.lang.Exception
17
+ import java.net.HttpURLConnection
18
+ import java.net.URL
19
+ import java.nio.charset.StandardCharsets
20
+
21
+ /* *
22
+ * ViewModel for survey, doing survey submit
23
+ */
24
+ class SurveyViewModel : ViewModel () {
25
+ private val loading: MutableLiveData <Boolean >
26
+ private val error: MutableLiveData <Boolean >
27
+ private var runner: Thread ? = null
28
+ @Synchronized
29
+ fun submitSurvey () {
30
+ if (wasRunning()) {
31
+ return
32
+ }
33
+ loading.postValue(true )
34
+ System .setProperty(" http.keepAlive" , " false" )
35
+ val url: String = if (BuildConfig .BUILD_TYPE == " debug" ) {
36
+ // "http://192.168.178.32:3219/data/add/api"
37
+ " http://localhost:3219/data/add/api"
38
+ } else {
39
+ // allow tls 1.0
40
+ // can't be tested fully due to different device libraries
41
+ " https://vct.proctet.net/data/add/api"
42
+ }
43
+ runner = Thread {
44
+ val jsonBody: JSONObject
45
+ try {
46
+ errorReporter.putCustomData(" url" , url)
47
+ val con = URL (url).openConnection() as HttpURLConnection
48
+ con.setRequestProperty(" Content-Type" , " application/json; charset=UTF-8" )
49
+ con.setRequestProperty(" Accept" , " application/json" )
50
+ con.doOutput = true
51
+ con.doInput = true
52
+ jsonBody = JSONObject (" {\" api\" :" + Build .VERSION .SDK_INT + " }" )
53
+ val wr = con.outputStream
54
+ wr.write(jsonBody.toString().toByteArray(StandardCharsets .UTF_8 ))
55
+ wr.flush()
56
+ wr.close()
57
+ val `in `: InputStream = BufferedInputStream (con.inputStream)
58
+ val result = ByteArrayOutputStream ()
59
+ val buffer = ByteArray (1024 )
60
+ var length: Int
61
+ while (`in `.read(buffer).also { length = it } != - 1 ) {
62
+ result.write(buffer, 0 , length)
63
+ }
64
+
65
+ val resultString = result.toString(" UTF-8" )
66
+ errorReporter.putCustomData(" server response" , resultString)
67
+ val jsonObject = JSONObject (resultString)
68
+ `in `.close()
69
+ con.disconnect()
70
+ Log .d(TAG , " received json: $jsonObject " )
71
+ loading.postValue(false )
72
+ } catch (e: Exception ) {
73
+ Log .wtf(TAG , e)
74
+ e.sendWithAcra()
75
+ error.postValue(true )
76
+ }
77
+ }
78
+ runner!! .start()
79
+ }
80
+
81
+ fun wasRunning (): Boolean {
82
+ return runner != null
83
+ }
84
+
85
+ val errorLiveData: LiveData <Boolean >
86
+ get() = error
87
+ val loadingLiveData: LiveData <Boolean >
88
+ get() = loading
89
+
90
+ companion object {
91
+ private const val TAG = " SurveyViewModel"
92
+ }
93
+
94
+ init {
95
+ loading = MutableLiveData ()
96
+ error = MutableLiveData ()
97
+ loading.value = false
98
+ error.value = false
99
+ }
100
+ }
0 commit comments