SHARE
TWEET

CallbackAsync

a guest Apr 18th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.demo.poc.merkurvut.login;
  2.  
  3. import javax.inject.Inject;
  4. import java.lang.ref.WeakReference;
  5.  
  6. import android.content.Context;
  7. import android.support.annotation.NonNull;
  8. import android.support.annotation.Nullable;
  9.  
  10. import com.demo.poc.merkurvut.base.NetworkAsyncTask;
  11. import com.demo.poc.merkurvut.connection.VutConnection;
  12. import com.demo.poc.merkurvut.dagger.annotations.ApplicationContext;
  13. import com.demo.poc.merkurvut.data.parser.VUTParser;
  14. import com.demo.poc.merkurvut.db.entities.StudentInformation;
  15.  
  16. /**
  17.  * Asynchronous task to log in.
  18.  *
  19.  * @author Jan Smyčka (jan.smycka@post.cz)
  20.  */
  21. public class LoginAsyncTask extends NetworkAsyncTask {
  22.  
  23.     /**
  24.      * Interface for class that wants to receive message about finishing this task.
  25.      */
  26.     public interface LoginResultsCallback extends NetworkTaskCallback {
  27.  
  28.         /**
  29.          * Callback for successful log in.
  30.          *
  31.          * @param studentInformation information about a student
  32.          */
  33.         void onLoginSuccess(@NonNull StudentInformation studentInformation);
  34.  
  35.         /**
  36.          * Callback for unsuccessful log in.
  37.          */
  38.         void onLoginFailed();
  39.     }
  40.  
  41.     @NonNull
  42.     private VUTParser mVutParser;
  43.  
  44.     @NonNull
  45.     private VutConnection mVutConnection;
  46.  
  47.     private WeakReference<LoginResultsCallback> mLoginTaskCallbackWeakReference;
  48.     private String mLogin;
  49.     private String mPassword;
  50.  
  51.     @Inject
  52.     LoginAsyncTask(@NonNull @ApplicationContext Context context, @NonNull VutConnection vutConnection, @NonNull VUTParser vutParser) {
  53.         super(context);
  54.         mVutConnection = vutConnection;
  55.         mVutParser = vutParser;
  56.     }
  57.  
  58.     /**
  59.      * Starts AsyncTask that logs in.
  60.      *
  61.      * @param loginTaskCallback listener for results.
  62.      * @param login             input from user
  63.      * @param password          input from user
  64.      */
  65.     public void logIn(@NonNull LoginResultsCallback loginTaskCallback, @NonNull String login, @NonNull String password) {
  66.         mNetworkTaskCallbackWeakReference = new WeakReference<>(loginTaskCallback);
  67.         mLoginTaskCallbackWeakReference = new WeakReference<>(loginTaskCallback);
  68.         mLogin = login;
  69.         mPassword = password;
  70.  
  71.         execute();
  72.     }
  73.  
  74.     @Nullable
  75.     @Override
  76.     protected StudentInformation doInBackground(Void... voids) {
  77.         if (mLoginTaskCallbackWeakReference == null || mLogin == null || mPassword == null) {
  78.             throw new IllegalArgumentException("You must call #logIn with non-null parameters in order to start this AsyncTask.");
  79.         }
  80.  
  81.         // TODO "Command && Query separation
  82.         if (mVutConnection.login(mLogin, mPassword)) {
  83.             return mVutParser.getStudentInfo();
  84.         } else {
  85.             return null;
  86.         }
  87.     }
  88.  
  89.     @Override
  90.     protected void onPostExecute(@Nullable Object studentInformation) {
  91.         super.onPostExecute(studentInformation);
  92.         final LoginResultsCallback loginTaskCallback = mLoginTaskCallbackWeakReference.get();
  93.  
  94.         if (studentInformation != null) {
  95.             loginTaskCallback.onLoginSuccess((StudentInformation) studentInformation);
  96.         } else {
  97.             loginTaskCallback.onLoginFailed();
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     protected boolean resultObjectClassCheck(@NonNull Object resultObject) {
  103.         return resultObject instanceof StudentInformation;
  104.     }
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top