Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
4,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. // Singleton
  2. public class Repository {
  3.     private Repository instance = new Repository();
  4.  
  5.     private Repository() {}
  6.  
  7.     public static Repository getInstance() {
  8.         return instance == null ? new Repository() : instance;
  9.     }
  10.  
  11.     /**
  12.      * Creates a {@link Single} which will send a sign in request to remote API, if necessary, and
  13.      * return the appropriate UI transition as {@link Intent} with appropriate action and extras.
  14.      *
  15.      * @param context {@link Context} to create an Intent
  16.      * @param context {@link String} something that authenticates the user from input
  17.      * @return {@link Single<Intent>} for the requested operation
  18.      */
  19.     @NonNull
  20.     public Single<Intent> signInOperation(@NonNull Context context, @NonNull String userInput) {
  21.         if (!tokenFresh() && isConnectionUp()) {
  22.             return remoteSignInSingle(userInput);
  23.         } else if (userAuthenticatedLocally(userInput)) {
  24.             Intent cachedLogIn = Ui.buildSignedInIntent(context);
  25.             cachedLogIn.putExtra(Ui.USER_EXTRA, getAuthenticatedUser());
  26.             cachedLogIn.putExtra(Ui.ONLINE_EXTRA, false);
  27.             return Single.just(cachedLogIn);
  28.         } else {
  29.             Intent errorLogIn = Ui.buildErrorIntent(context);
  30.             errorLogIn.putExtra(Ui.MESSAGE_EXTRA, "No connection!");
  31.             return Single.just(errorLogIn);
  32.         }
  33.     }
  34.  
  35.     @NonNull
  36.     public Single<List<Stuff> getStuffOperation(Context context) {
  37.         // TODO
  38.     }
  39. }
  40. public class SignInViewModel extends ViewModel {
  41.     /**
  42.      * Handles intention of submit from a sign in screen, calling remote API. This either loads from
  43.      * server, local cache, fails with a proper Intent, or throws an exception if errors unhandled.
  44.      *
  45.      * @param context   context to acquire the next UI Intent
  46.      * @param userInput {@link String} for input to pass to remote API (assume no password required)
  47.      */
  48.     public Single<Intent> clickSignIn(@NonNull final Context context,
  49.                                 @NonNull final String userInput) {
  50.         return Repository.getInstance().signInOperation(context, userInput)
  51.                 .map(intent -> {
  52.                     // return unmodified, for now
  53.                     return intent;
  54.                 });
  55.     }
  56. }
  57. public abstract class SignInScreen extends Fragment {
  58.     /**
  59.      * ViewModel performs submit operations for sign in. This screen uses the resulting
  60.      * {@link Intent} to start next activity, transition to next screen, finish, or show an error.
  61.      *
  62.      * @param context   a context to perform next ui flow call
  63.      * @param userInput input to perform the request with
  64.      */
  65.     protected Disposable onClickSubmitLanding(@NonNull Context context, String userInput) {
  66.         return viewModel.clickSignIn(context, userInput)
  67.                 .observeOn(Schedulers.io())
  68.                 .subscribeOn(AndroidSchedulers.mainThread())
  69.                 .subscribe((Consumer<Intent>) context::startActivity);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement