/
PassphraseViewModel.java
104 lines (85 loc) · 2.96 KB
/
PassphraseViewModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/***
Copyright (c) 2017-2019 CommonsWare, LLC
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.
Covered in detail in the book _Android's Architecture Components_
https://commonsware.com/AndroidArch
*/
package com.commonsware.android.diceware;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import io.reactivex.schedulers.Schedulers;
class PassphraseViewModel extends ViewModel {
private static final String STATE_SOURCE="source";
private static final String STATE_COUNT="count";
private final MutableLiveData<String> livePassphrase=new MutableLiveData<>();
private final Repository repo;
private Uri source=Uri.parse("file:///android_asset/eff_short_wordlist_2_0.txt");
private int count=6;
private Disposable refreshDisposable=Disposables.empty();
PassphraseViewModel(Context ctxt, Bundle state) {
repo=Repository.get(ctxt);
if (state!=null) {
source=state.getParcelable(STATE_SOURCE);
count=state.getInt(STATE_COUNT, 6);
}
refresh();
}
@Override
protected void onCleared() {
super.onCleared();
refreshDisposable.dispose();
}
void onSaveInstanceState(Bundle state) {
state.putParcelable(STATE_SOURCE, source);
state.putInt(STATE_COUNT, count);
}
void setSource(Uri source) {
this.source=source;
refresh();
}
void setCount(int count) {
this.count=count;
refresh();
}
int getCount() {
return(count);
}
LiveData<String> passphraseStream() {
return(livePassphrase);
}
void refresh() {
refreshDisposable.dispose();
refreshDisposable=repo.getPassphrase(source, count)
.subscribeOn(Schedulers.io())
.subscribe(livePassphrase::postValue,
t -> Log.e(getClass().getSimpleName(), "Exception loading words", t));
}
static class Factory implements ViewModelProvider.Factory {
private final Bundle state;
private final Context ctxt;
Factory(Context ctxt, Bundle state) {
this.ctxt=ctxt.getApplicationContext();
this.state=state;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
return((T)new PassphraseViewModel(ctxt, state));
}
}
}