Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
UserActivity.kt
/* * Copyright (C) 2017 The Android Open Source Project * * 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 com.example.android.observability.ui import android.arch.lifecycle.ViewModelProviders import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.util.Log import com.example.android.observability.Injection import com.example.android.observability.R import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers import kotlinx.android.synthetic.main.activity_user.* /** * Main screen of the app. Displays a user name and gives the option to update the user name. */ class UserActivity : AppCompatActivity() { private lateinit var viewModelFactory: ViewModelFactory private lateinit var viewModel: UserViewModel private val disposable = CompositeDisposable() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_user) viewModelFactory = Injection.provideViewModelFactory(this) viewModel = ViewModelProviders.of(this, viewModelFactory).get(UserViewModel::class.java) update_user_button.setOnClickListener { updateUserName() } } override fun onStart() { super.onStart() // Subscribe to the emissions of the user name from the view model. // Update the user name text view, at every onNext emission. // In case of error, log the exception. disposable.add(viewModel.userName() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ this.user_name.text = it }, { error -> Log.e(TAG, "Unable to get username", error) })) } override fun onStop() { super.onStop() // clear all the subscription disposable.clear() } private fun updateUserName() { val userName = user_name_input.text.toString() // Disable the update button until the user name update has been done update_user_button.isEnabled = false // Subscribe to updating the user name. // Enable back the button once the user name has been updated disposable.add(viewModel.updateUserName(userName) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ update_user_button.isEnabled = true }, { error -> Log.e(TAG, "Unable to update username", error) })) } companion object { private val TAG = UserActivity::class.java.simpleName } }