Last active
May 1, 2019 17:37
-
-
Save dinorahtovar/1202b9ddcfc6a16b871cdd5ce9e31472 to your computer and use it in GitHub Desktop.
MVVM Architecture
This file contains hidden or 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
/** | |
* Created by Dinorah Tovar on 18/01/19. | |
* Main Activity that verify if the user is availbale | |
*/ | |
class MainActivity : AppCompatActivity() { | |
private var viewModel: MainViewModel? = null | |
/** | |
* On Create View instance | |
* @param savedInstanceState Bundle? | |
*/ | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) | |
viewModel?.userLiveData?.observe(this, userLogin()) | |
viewModel?.userAvailable() | |
} | |
/** | |
* Search for user available on the view | |
*/ | |
private fun userLogin() = Observer<Boolean> { existingUser -> | |
if (existingUser) { | |
startActivity(Intent(context, HomeActivity::class.java)) | |
} else { | |
startActivity(Intent(context, LoginActivity::class.java)) | |
} | |
} | |
} |
This file contains hidden or 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
/** | |
* Created by Dinorah Tovar on 18/01/2019. | |
* Main View model for the MainActivity class | |
*/ | |
class MainViewModel : ViewModel() { | |
@Inject | |
internal lateinit var loginRepository: LoginRepository | |
val userLiveData: MutableLiveData<Boolean> by lazy { MutableLiveData<Boolean>() } | |
/** | |
* Init injector for main class | |
*/ | |
init { | |
YourMainApplication.appComponent?.inject(this) | |
} | |
/** | |
* User available and observer Mutable live data object | |
*/ | |
fun userAvailable() { | |
val user = loginRepository.currentUser() | |
userLiveData.value = user?.let { true } ?: false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment