Separate synchronization from login (#137)

This commit is contained in:
Rafał Borcz 2018-06-13 18:53:42 +02:00 committed by Mikołaj Pich
parent 072c504d2b
commit b63e28f9a9
6 changed files with 51 additions and 21 deletions

View File

@ -14,6 +14,7 @@ import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import javax.inject.Inject; import javax.inject.Inject;
@ -217,6 +218,12 @@ public class LoginActivity extends BaseActivity implements LoginContract.View {
return findViewById(R.id.login_activity_container); return findViewById(R.id.login_activity_container);
} }
@Override
public void onSyncFailed() {
Toast.makeText(getApplicationContext(), R.string.login_sync_error, Toast.LENGTH_LONG).show();
}
private void onLoginProgressUpdate(String step, String message) { private void onLoginProgressUpdate(String step, String message) {
loginProgressText.setText(String.format("%1$s/2 - %2$s...", step, message)); loginProgressText.setText(String.format("%1$s/2 - %2$s...", step, message));
} }

View File

@ -33,6 +33,8 @@ public interface LoginContract {
void showActionBar(boolean show); void showActionBar(boolean show);
void onSyncFailed();
} }
interface Presenter extends BaseContract.Presenter<View> { interface Presenter extends BaseContract.Presenter<View> {
@ -45,7 +47,7 @@ public interface LoginContract {
void onLoginProgress(int step); void onLoginProgress(int step);
void onEndAsync(boolean success, Exception exception); void onEndAsync(int success, Exception exception);
void onCanceledAsync(); void onCanceledAsync();
} }

View File

@ -83,22 +83,30 @@ public class LoginPresenter extends BasePresenter<LoginContract.View>
} }
@Override @Override
public void onEndAsync(boolean success, Exception exception) { public void onEndAsync(int success, Exception exception) {
if (success) { switch (success) {
FabricUtils.logRegister(true, getRepository().getDbRepo().getCurrentSymbol().getSymbol(), "Success"); case LoginTask.LOGIN_AND_SYNC_SUCCESS:
getView().openMainActivity(); FabricUtils.logRegister(true, getRepository().getDbRepo().getCurrentSymbol().getSymbol(), "Success");
return; getView().openMainActivity();
} else if (exception instanceof BadCredentialsException) { return;
getView().setErrorPassIncorrect(); case LoginTask.SYNC_FAILED:
getView().showSoftInput(); FabricUtils.logRegister(true, symbol, exception.getMessage());
} else if (exception instanceof AccountPermissionException) { getView().onSyncFailed();
getView().setErrorSymbolRequired(); getView().openMainActivity();
getView().showSoftInput(); return;
} else { case LoginTask.LOGIN_FAILED:
FabricUtils.logRegister(false, symbol, exception.getMessage()); if (exception instanceof BadCredentialsException) {
getView().showMessage(getRepository().getResRepo().getErrorLoginMessage(exception)); getView().setErrorPassIncorrect();
getView().showSoftInput();
} else if (exception instanceof AccountPermissionException) {
getView().setErrorSymbolRequired();
getView().showSoftInput();
} else {
FabricUtils.logRegister(false, symbol, exception.getMessage());
getView().showMessage(getRepository().getResRepo().getErrorLoginMessage(exception));
}
break;
} }
getView().showActionBar(true); getView().showActionBar(true);
getView().showLoginProgress(false); getView().showLoginProgress(false);
} }

View File

@ -2,7 +2,13 @@ package io.github.wulkanowy.ui.login;
import android.os.AsyncTask; import android.os.AsyncTask;
public class LoginTask extends AsyncTask<Void, Integer, Boolean> { public class LoginTask extends AsyncTask<Void, Integer, Integer> {
public final static int LOGIN_AND_SYNC_SUCCESS = 1;
public final static int LOGIN_FAILED = -1;
public final static int SYNC_FAILED = 2;
private LoginContract.Presenter presenter; private LoginContract.Presenter presenter;
@ -18,18 +24,23 @@ public class LoginTask extends AsyncTask<Void, Integer, Boolean> {
} }
@Override @Override
protected Boolean doInBackground(Void... params) { protected Integer doInBackground(Void... params) {
try { try {
publishProgress(1); publishProgress(1);
presenter.onDoInBackground(1); presenter.onDoInBackground(1);
} catch (Exception e) {
exception = e;
return LOGIN_FAILED;
}
try {
publishProgress(2); publishProgress(2);
presenter.onDoInBackground(2); presenter.onDoInBackground(2);
} catch (Exception e) { } catch (Exception e) {
exception = e; exception = e;
return false; return SYNC_FAILED;
} }
return true; return LOGIN_AND_SYNC_SUCCESS;
} }
@Override @Override
@ -38,7 +49,7 @@ public class LoginTask extends AsyncTask<Void, Integer, Boolean> {
} }
@Override @Override
protected void onPostExecute(Boolean success) { protected void onPostExecute(Integer success) {
presenter.onEndAsync(success, exception); presenter.onEndAsync(success, exception);
} }

View File

@ -163,4 +163,5 @@
<string name="grades_summary_final_average">Końcowa średnia</string> <string name="grades_summary_final_average">Końcowa średnia</string>
<string name="action_title_summary">Podsumowanie</string> <string name="action_title_summary">Podsumowanie</string>
<string name="action_title_details">Szczegóły</string> <string name="action_title_details">Szczegóły</string>
<string name="login_sync_error">Podczas synchronizacji wystąpił błąd</string>
</resources> </resources>

View File

@ -158,4 +158,5 @@
<string name="grades_summary_final_average">Final average</string> <string name="grades_summary_final_average">Final average</string>
<string name="action_title_summary">Summary</string> <string name="action_title_summary">Summary</string>
<string name="action_title_details">Details</string> <string name="action_title_details">Details</string>
<string name="login_sync_error">An error has occurred during synchronization</string>
</resources> </resources>