@@ -24,16 +24,18 @@ import java.net.URISyntaxException
2424import javax.inject.Inject
2525
2626@HiltViewModel
27- class CreateSubscriptionModel @Inject constructor(
27+ class AddSubscriptionModel @Inject constructor(
2828 @param:ApplicationContext private val context : Context ,
29- private val db : AppDatabase
29+ private val db : AppDatabase ,
30+ val validationUseCase : ValidationUseCase ,
31+ val subscriptionSettingsUseCase : SubscriptionSettingsUseCase
3032) : ViewModel() {
3133
3234 data class UiState (
3335 val success : Boolean = false ,
3436 val errorMessage : String? = null ,
3537 val isCreating : Boolean = false ,
36- val showNextButton : Boolean = false
38+ val showNextButton : Boolean = false ,
3739 )
3840
3941 var uiState by mutableStateOf(UiState ())
@@ -43,53 +45,81 @@ class CreateSubscriptionModel @Inject constructor(
4345 uiState = uiState.copy(showNextButton = value)
4446 }
4547
48+ fun resetValidationResult () = validationUseCase.resetResult()
49+ fun validateUrl (originalUri : Uri , username : String? = null, password : String? = null) =
50+ validationUseCase.validate(originalUri, username, password)
51+
52+ fun checkUrlIntroductionPage () {
53+ with (subscriptionSettingsUseCase) {
54+ if (validationUseCase.uiState.isVerifyingUrl) {
55+ setShowNextButton(true )
56+ } else {
57+ val uri = validateUri(
58+ url = uiState.url,
59+ onSetUrl = ::setUrl,
60+ onSetCredentials = { username, password ->
61+ setUsername(username)
62+ setPassword(password)
63+ },
64+ requiresAuth = uiState.requiresAuth,
65+ onSetRequiresAuth = ::setRequiresAuth,
66+ onSetIsInsecure = ::setIsInsecure,
67+ onSetUrlError = ::setUrlError
68+ )
69+ val authOK =
70+ if (uiState.requiresAuth)
71+ ! uiState.username.isNullOrEmpty() &&
72+ ! uiState.password.isNullOrEmpty()
73+ else
74+ true
75+ setShowNextButton(uri != null && authOK)
76+ }
77+ }
78+ }
79+
4680 /* *
4781 * Creates a new subscription taking the data from the given models.
4882 */
49- fun create (
50- subscriptionSettingsModel : SubscriptionSettingsModel ,
51- ) {
52- viewModelScope.launch(Dispatchers .IO ) {
53- uiState = uiState.copy(isCreating = true )
54- try {
83+ fun createSubscription () = viewModelScope.launch(Dispatchers .IO ) {
84+ uiState = uiState.copy(isCreating = true )
85+ try {
86+ with (subscriptionSettingsUseCase.uiState) {
5587 val subscription = Subscription (
56- displayName = subscriptionSettingsModel.uiState. title!! ,
57- url = Uri .parse(subscriptionSettingsModel.uiState. url),
58- color = subscriptionSettingsModel.uiState. color,
59- ignoreEmbeddedAlerts = subscriptionSettingsModel.uiState. ignoreAlerts,
60- defaultAlarmMinutes = subscriptionSettingsModel.uiState. defaultAlarmMinutes,
61- defaultAllDayAlarmMinutes = subscriptionSettingsModel.uiState. defaultAllDayAlarmMinutes,
62- ignoreDescription = subscriptionSettingsModel.uiState. ignoreDescription,
88+ displayName = title!! ,
89+ url = Uri .parse(url),
90+ color = color,
91+ ignoreEmbeddedAlerts = ignoreAlerts,
92+ defaultAlarmMinutes = defaultAlarmMinutes,
93+ defaultAllDayAlarmMinutes = defaultAllDayAlarmMinutes,
94+ ignoreDescription = ignoreDescription,
6395 )
6496
6597 /* * A list of all the ids of the inserted rows */
6698 val id = db.subscriptionsDao().add(subscription)
6799
68100 // Create the credential in the IO thread
69- if (subscriptionSettingsModel.uiState. requiresAuth) {
101+ if (requiresAuth) {
70102 // If the subscription requires credentials, create them
71- val username = subscriptionSettingsModel.uiState.username
72- val password = subscriptionSettingsModel.uiState.password
73103 if (username != null && password != null ) {
74- val credential = Credential (
75- subscriptionId = id,
76- username = username,
77- password = password
104+ db.credentialsDao().create(
105+ Credential (
106+ subscriptionId = id,
107+ username = username,
108+ password = password
109+ )
78110 )
79- db.credentialsDao().create(credential)
80111 }
81112 }
82113
83114 // sync the subscription to reflect the changes in the calendar provider
84115 SyncWorker .run (context)
85-
86- uiState = uiState.copy(success = true )
87- } catch (e: Exception ) {
88- Log .e(Constants .TAG , " Couldn't create calendar" , e)
89- uiState = uiState.copy(errorMessage = e.localizedMessage ? : e.message)
90- } finally {
91- uiState = uiState.copy(isCreating = false )
92116 }
117+ uiState = uiState.copy(success = true )
118+ } catch (e: Exception ) {
119+ Log .e(Constants .TAG , " Couldn't create calendar" , e)
120+ uiState = uiState.copy(errorMessage = e.localizedMessage ? : e.message)
121+ } finally {
122+ uiState = uiState.copy(isCreating = false )
93123 }
94124 }
95125
@@ -172,33 +202,4 @@ class CreateSubscriptionModel @Inject constructor(
172202 }
173203 return uri
174204 }
175-
176- fun checkUrlIntroductionPage (
177- subscriptionSettingsModel : SubscriptionSettingsModel ,
178- validationModel : ValidationModel
179- ) {
180- if (validationModel.uiState.isVerifyingUrl) {
181- setShowNextButton(true )
182- } else {
183- val uri = validateUri(
184- url = subscriptionSettingsModel.uiState.url,
185- onSetUrl = subscriptionSettingsModel::setUrl,
186- onSetCredentials = { username, password ->
187- subscriptionSettingsModel.setUsername(username)
188- subscriptionSettingsModel.setPassword(password)
189- },
190- requiresAuth = subscriptionSettingsModel.uiState.requiresAuth,
191- onSetRequiresAuth = subscriptionSettingsModel::setRequiresAuth,
192- onSetIsInsecure = subscriptionSettingsModel::setIsInsecure,
193- onSetUrlError = subscriptionSettingsModel::setUrlError
194- )
195- val authOK =
196- if (subscriptionSettingsModel.uiState.requiresAuth)
197- ! subscriptionSettingsModel.uiState.username.isNullOrEmpty() &&
198- ! subscriptionSettingsModel.uiState.password.isNullOrEmpty()
199- else
200- true
201- setShowNextButton(uri != null && authOK)
202- }
203- }
204205}
0 commit comments