-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the package structure and adding a ViewModel
- Loading branch information
1 parent
645bf45
commit a33ba90
Showing
28 changed files
with
477 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
core/src/main/java/io/plaidapp/core/designernews/login/ui/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2018 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.plaidapp.core.designernews.login.ui | ||
|
||
import android.arch.lifecycle.LiveData | ||
import android.arch.lifecycle.MutableLiveData | ||
import android.arch.lifecycle.ViewModel | ||
import io.plaidapp.core.data.CoroutinesContextProvider | ||
import io.plaidapp.core.data.Result | ||
import io.plaidapp.core.designernews.login.data.LoginRepository | ||
import io.plaidapp.core.util.exhaustive | ||
import kotlinx.coroutines.experimental.Job | ||
import kotlinx.coroutines.experimental.launch | ||
|
||
/** | ||
* View Model for [DesignerNewsLogin] | ||
* TODO move the rest of the logic from activity here. | ||
* TODO keep this in core for now, to be moved to designernews module | ||
*/ | ||
class LoginViewModel( | ||
private val loginRepository: LoginRepository, | ||
private val contextProvider: CoroutinesContextProvider | ||
) : ViewModel() { | ||
|
||
private var currentJob: Job? = null | ||
|
||
private val _uiState = MutableLiveData<Result<LoginUiModel>>() | ||
val uiState: LiveData<Result<LoginUiModel>> | ||
get() = _uiState | ||
|
||
fun login(username: String, password: String) { | ||
// only allow one login at a time | ||
if (currentJob?.isActive == true) { | ||
return | ||
} | ||
currentJob = launchLogin(username, password) | ||
} | ||
|
||
private fun launchLogin(username: String, password: String) = launch(contextProvider.io) { | ||
_uiState.postValue(Result.Loading) | ||
val result = loginRepository.login(username, password) | ||
|
||
when (result) { | ||
is Result.Success -> { | ||
val user = result.data | ||
val uiModel = LoginUiModel( | ||
user.displayName.toLowerCase(), | ||
user.portraitUrl | ||
) | ||
_uiState.postValue(Result.Success(uiModel)) | ||
} | ||
is Result.Error -> _uiState.postValue(result) | ||
is Result.Loading -> { | ||
/* we ignore the loading state */ | ||
} | ||
}.exhaustive | ||
} | ||
|
||
override fun onCleared() { | ||
super.onCleared() | ||
// when the VM is destroyed, cancel the running job. | ||
currentJob?.cancel() | ||
} | ||
} | ||
|
||
/** | ||
* UI model for [DesignerNewsLogin] | ||
*/ | ||
data class LoginUiModel(val displayName: String, val portraitUrl: String?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Damn, this is great! :D