Created
March 28, 2018 17:31
-
-
Save yccheok/60debc144765e337fe70a64f8d42a780 to your computer and use it in GitHub Desktop.
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
| /* | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical"> | |
| <EditText | |
| android:id="@+id/edit_text" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" /> | |
| <TextView | |
| android:id="@+id/text_view" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" /> | |
| </LinearLayout> | |
| */ | |
| package com.xxx.home; | |
| import android.arch.core.util.Function; | |
| import android.arch.lifecycle.LiveData; | |
| import android.arch.lifecycle.MutableLiveData; | |
| import android.arch.lifecycle.Observer; | |
| import android.arch.lifecycle.Transformations; | |
| import android.arch.lifecycle.ViewModel; | |
| import android.arch.lifecycle.ViewModelProvider; | |
| import android.support.annotation.Nullable; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.os.Bundle; | |
| import android.text.Editable; | |
| import android.text.TextWatcher; | |
| import android.widget.EditText; | |
| import android.widget.TextView; | |
| public class MainActivity extends AppCompatActivity { | |
| private String string; | |
| @Override | |
| public void onSaveInstanceState(Bundle savedInstanceState) { | |
| super.onSaveInstanceState(savedInstanceState); | |
| savedInstanceState.putString("STRING", string); | |
| } | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| if (savedInstanceState != null) { | |
| this.string = savedInstanceState.getString("STRING"); | |
| } | |
| final TextView textView = this.findViewById(R.id.text_view); | |
| ViewModelFactory factory = new ViewModelFactory(this.string); | |
| final MyViewModel myViewModel = android.arch.lifecycle.ViewModelProviders.of(this, factory).get(MyViewModel.class); | |
| myViewModel.getCode().observe(this, new Observer<Integer>() { | |
| @Override | |
| public void onChanged(@Nullable Integer integer) { | |
| textView.setText("" + integer); | |
| android.util.Log.i("CHEOK", "UI -> " + integer); | |
| } | |
| }); | |
| EditText editText = this.findViewById(R.id.edit_text); | |
| editText.addTextChangedListener(new TextWatcher() { | |
| @Override | |
| public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
| } | |
| @Override | |
| public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
| } | |
| @Override | |
| public void afterTextChanged(Editable editable) { | |
| string = editable.toString(); | |
| myViewModel.search(string); | |
| } | |
| }); | |
| } | |
| class MyViewModel extends ViewModel { | |
| final MutableLiveData<String> mString = new MutableLiveData<>(); | |
| final LiveData<Integer> mCode; | |
| public MyViewModel(String string) { | |
| mCode = Transformations.switchMap(mString, new Function<String, LiveData<Integer>>() { | |
| @Override | |
| public LiveData<Integer> apply(final String input) { | |
| final MutableLiveData<Integer> result = new MutableLiveData<>(); | |
| new Thread(new Runnable() { | |
| @Override | |
| public void run() { | |
| // Pretend we are busy | |
| try { | |
| Thread.sleep(5000); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| int code = 0; | |
| for (int i=0; i<input.length(); i++) { | |
| code = code + (int)input.charAt(i); | |
| } | |
| android.util.Log.i("CHEOK", "Processing : " + input + " -> " + code); | |
| result.postValue(code); | |
| } | |
| }).start(); | |
| return result; | |
| } | |
| }); | |
| if (string != null) { | |
| mString.setValue(string); | |
| } | |
| } | |
| public LiveData<Integer> getCode() { | |
| return mCode; | |
| } | |
| public void search(String string) { | |
| mString.setValue(string); | |
| } | |
| } | |
| class ViewModelFactory implements ViewModelProvider.Factory { | |
| private final String string; | |
| public ViewModelFactory(String string) { | |
| this.string = string; | |
| } | |
| @Override | |
| public MyViewModel create(Class modelClass) { | |
| return new MyViewModel(string); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment