[英]How to use livedata for making multiple network calls?
我正在一個項目中嘗試通過數據綁定實現Android體系結構組件。 我有一個活動,viewmodel和存儲庫,並且現在正在創建一個登錄頁面。 我必須創建兩個存儲庫函數以使用戶登錄,第一個函數將返回uid,並通過傳遞此uid,第二個函數將返回詳細信息。 一旦一切都完成了,那么只有我想將用戶重定向到內頁。 我已經嘗試過Transformations.switchMap,但無法正常工作。 請幫忙...
public class LoginViewModel extends ViewModel {
private final VendorRepository vendorRepository;
private final ResourceProvider resourceProvider;
public MutableLiveData<String> error = new MutableLiveData<>();
public MutableLiveData<Boolean> loading = new MutableLiveData<>();
private MutableLiveData<Resource<String>> vendorId = new MutableLiveData<>();
public LiveData<Resource<Vendor>> vendor;
public LoginViewModel() {
this.vendorRepository = VendorRepository.getInstance();
this.resourceProvider = ResourceProvider.getInstance();
/*vendor = Transformations.switchMap(vendorId, new Function<Resource<String>, LiveData<Resource<Vendor>>>() {
@Override
public LiveData<Resource<Vendor>> apply(Resource<String> input) {
if (input!=null&&input.status.equals(Status.SUCCESS))
return vendorRepository.getVendor(vendorId.getValue().data);
else return null;
}
});*/
}
/**
* called when a user clicks on the login button
* if the inputs are valid, will call the login function
*
* @param email entered email
* @param password entered password
*/
public void onClickLogin(String email, String password) {
//loading.setValue(true);
if (validInputs(email, password)) {
vendorId = vendorRepository.login(
email, password
);
} else loading.setValue(false);
}}
這是我的視圖模型
public class VendorRepository {
private static VendorRepository INSTANCE;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
public static VendorRepository getInstance() {
if (INSTANCE == null)
INSTANCE = new VendorRepository();
return INSTANCE;
}
private VendorRepository() {
this.firebaseAuth = FirebaseAuth.getInstance();
this.firebaseFirestore = FirebaseFirestore.getInstance();
}
public MutableLiveData<Resource<String>> login(String email, String password) {
final MutableLiveData<Resource<String>> data = new MutableLiveData<>();
data.setValue(Resource.<String>loading(null));
firebaseAuth
.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
data.postValue(Resource.success(task.getResult().getUser().getUid()));
} else {
data.postValue(Resource.<String>error(task.getException().getMessage(), null));
}
}
});
return data;
}
public LiveData<Resource<Vendor>> getVendor(String id) {
final MutableLiveData<Resource<Vendor>> data = new MutableLiveData<>();
data.postValue(Resource.<Vendor>loading(null));
firebaseFirestore
.collection(DB_VENDOR)
.document(id)
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
Vendor vendor = task.getResult().toObject(Vendor.class);
data.postValue(Resource.success(vendor));
} else {
data.postValue(Resource.<Vendor>error(task.getException().getMessage(), null));
}
}
});
return data;
} }
這是我的回購
您需要在activity
某個地方添加vendorId
livedata的observer
,可能是在初始化viewModel
之后添加到onCreate(Bundle)
。
您的代碼將如下所示。
public final class MyActivity extends AppCompatActivity {
...
LoginViewModel mViewModel;
@Override protected final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mViewModel = ViewModelProviders.of(this)
.get(LoginViewModel.class);
//make vendorId public in the viewModel
mViewModel.vendorId.observe ( this, new Observer<String> (){
@Override public void onChanged(@Nullable final String vendorId) {
VendorRepository.getInstance().getVendor(vendorId);
}
});
//Similarly, add observer for the vendor live data
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.