如何使用实时数据进行多个网络呼叫?

时间:2018-07-11 07:05:21

标签: android android-architecture-components android-viewmodel android-livedata mutablelivedata

我正在一个项目中,我正在尝试通过数据绑定实现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;
} }

这是我的仓库

1 个答案:

答案 0 :(得分:0)

在初始化$ curl -sI https://curl.haxx.se -o/dev/null -w '%{http_version}\n' 之后,您需要在observer的某个地方(可能在vendorId的某个地方添加activity个活动数据中的onCreate(Bundle)

您的代码将如下所示。

viewModel