@@ -6,18 +6,21 @@ import androidx.lifecycle.MutableLiveData
6
6
import androidx.lifecycle.ViewModel
7
7
import androidx.lifecycle.viewModelScope
8
8
import io.davedavis.todora.model.*
9
- import io.davedavis.todora.network.Auth
10
- import io.davedavis.todora.network.JiraApi
9
+ import io.davedavis.todora.network.*
11
10
import io.davedavis.todora.utils.SharedPreferencesManager
12
11
import kotlinx.coroutines.launch
13
12
import retrofit2.Response
14
13
import timber.log.Timber
15
14
15
+ /* *
16
+ * ViewModel class that holds all the data and logic for the create issue flow.
17
+ */
16
18
class CreateViewModel : ViewModel () {
17
19
20
+ // Response from the API as a string as it's only in English so can't be localized.
18
21
var responseMessage: String = " No response yet"
19
22
20
- // The internal MutableLiveData for the selected property
23
+ // The internal MutableLiveData for the selected issue
21
24
private val _newIssue = MutableLiveData <NewIssue >()
22
25
val newIssue: LiveData <NewIssue >
23
26
get() = _newIssue
@@ -60,8 +63,11 @@ class CreateViewModel : ViewModel() {
60
63
61
64
62
65
// Set up the edit fields on changed listeners (Bound in the XML)
63
- // Thanks https://stackoverflow.com/questions/33798426/how-to-databind-to-ontextchanged-for-an-edittext-on-android
64
- // ToDo: Move this to the Binding Adapter Class.
66
+
67
+ /* *
68
+ * When the description is changed, set the livedata of [newSummary] to the updated value
69
+ * so when there's a lifecycle change, the user won't lose any input.
70
+ */
65
71
fun onSummaryTextChange (newSummary : Editable ? ) {
66
72
_newIssue .value?.fields?.summary = newSummary.toString()
67
73
if (! _newIssue .value?.fields?.description?.content?.get(0 )?.content?.get(0 )?.actualDescriptionText.isNullOrEmpty()) {
@@ -70,6 +76,10 @@ class CreateViewModel : ViewModel() {
70
76
71
77
}
72
78
79
+ /* *
80
+ * When the description is changed, set the livedata of [newDescription] to the updated value
81
+ * so when there's a lifecycle change, the user won't lose any input.
82
+ */
73
83
fun onDescriptionTextChange (newDescription : Editable ? ) {
74
84
_newIssue .value?.fields?.description?.content?.get(0 )?.content?.get(0 )?.actualDescriptionText =
75
85
newDescription.toString()
@@ -79,20 +89,28 @@ class CreateViewModel : ViewModel() {
79
89
80
90
}
81
91
92
+ /* *
93
+ * When the issue/to-do is updated, set the [priority] [MutableLiveData]
94
+ * @param updatedPriority to the updated one in the view.
95
+ */
82
96
fun updatePriority (updatedPriority : Int ) {
83
97
_newIssue .value?.fields?.priority?.name =
84
98
PriorityOptions .values()[updatedPriority].toString()
85
99
86
100
}
87
101
88
-
102
+ /* *
103
+ * Submits new issue to the [JiraApiService] by calling [newJiraIssue] through a coroutine.
104
+ * @param filter on the [JiraApiFilter]
105
+ * @param [newIssue] that goes along with the post request
106
+ * Returns a string response.
107
+ */
89
108
fun submitNewJiraIssue () {
90
109
Timber .i(" >>> updateJiraIssue in ViewModel Called" )
91
110
viewModelScope.launch {
92
111
_status .value = JiraAPIStatus .LOADING
93
112
try {
94
113
Timber .i(" >>> Trying ..." )
95
- Timber .i(newIssue.value.toString())
96
114
val response: Response <Unit > = JiraApi .retrofitService.newJiraIssue(
97
115
Auth .getAuthHeaders(),
98
116
newIssue.value
0 commit comments