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.EditText;
import android.widget.TextView;
import android.widget.Toast;
import javax.inject.Inject;
@ -217,6 +218,12 @@ public class LoginActivity extends BaseActivity implements LoginContract.View {
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) {
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 onSyncFailed();
}
interface Presenter extends BaseContract.Presenter<View> {
@ -45,7 +47,7 @@ public interface LoginContract {
void onLoginProgress(int step);
void onEndAsync(boolean success, Exception exception);
void onEndAsync(int success, Exception exception);
void onCanceledAsync();
}

View File

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

View File

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

View File

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

View File

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