Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Singleton
- public class Repository {
- private Repository instance = new Repository();
- private Repository() {}
- public static Repository getInstance() {
- return instance == null ? new Repository() : instance;
- }
- /**
- * Creates a {@link Single} which will send a sign in request to remote API, if necessary, and
- * return the appropriate UI transition as {@link Intent} with appropriate action and extras.
- *
- * @param context {@link Context} to create an Intent
- * @param context {@link String} something that authenticates the user from input
- * @return {@link Single<Intent>} for the requested operation
- */
- @NonNull
- public Single<Intent> signInOperation(@NonNull Context context, @NonNull String userInput) {
- if (!tokenFresh() && isConnectionUp()) {
- return remoteSignInSingle(userInput);
- } else if (userAuthenticatedLocally(userInput)) {
- Intent cachedLogIn = Ui.buildSignedInIntent(context);
- cachedLogIn.putExtra(Ui.USER_EXTRA, getAuthenticatedUser());
- cachedLogIn.putExtra(Ui.ONLINE_EXTRA, false);
- return Single.just(cachedLogIn);
- } else {
- Intent errorLogIn = Ui.buildErrorIntent(context);
- errorLogIn.putExtra(Ui.MESSAGE_EXTRA, "No connection!");
- return Single.just(errorLogIn);
- }
- }
- @NonNull
- public Single<List<Stuff> getStuffOperation(Context context) {
- // TODO
- }
- }
- public class SignInViewModel extends ViewModel {
- /**
- * Handles intention of submit from a sign in screen, calling remote API. This either loads from
- * server, local cache, fails with a proper Intent, or throws an exception if errors unhandled.
- *
- * @param context context to acquire the next UI Intent
- * @param userInput {@link String} for input to pass to remote API (assume no password required)
- */
- public Single<Intent> clickSignIn(@NonNull final Context context,
- @NonNull final String userInput) {
- return Repository.getInstance().signInOperation(context, userInput)
- .map(intent -> {
- // return unmodified, for now
- return intent;
- });
- }
- }
- public abstract class SignInScreen extends Fragment {
- /**
- * ViewModel performs submit operations for sign in. This screen uses the resulting
- * {@link Intent} to start next activity, transition to next screen, finish, or show an error.
- *
- * @param context a context to perform next ui flow call
- * @param userInput input to perform the request with
- */
- protected Disposable onClickSubmitLanding(@NonNull Context context, String userInput) {
- return viewModel.clickSignIn(context, userInput)
- .observeOn(Schedulers.io())
- .subscribeOn(AndroidSchedulers.mainThread())
- .subscribe((Consumer<Intent>) context::startActivity);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement