Search in sources :

Example 1 with InvalidResponseException

use of app.abhijit.iter.exceptions.InvalidResponseException in project bunk by abhijitparida.

the class ResponseParser method processLogin.

private Student processLogin(String loginJson) {
    JsonObject login;
    try {
        login = this.jsonParser.parse(loginJson).getAsJsonObject();
    } catch (Exception e) {
        throw new InvalidResponseException();
    }
    if (!login.has("status") || !login.has("name")) {
        throw new InvalidResponseException();
    }
    if (!login.get("status").getAsString().equals("success")) {
        throw new InvalidCredentialsException();
    }
    Student student = new Student();
    student.name = WordUtils.capitalizeFully(login.get("name").getAsString());
    return student;
}
Also used : InvalidCredentialsException(app.abhijit.iter.exceptions.InvalidCredentialsException) JsonObject(com.google.gson.JsonObject) Student(app.abhijit.iter.models.Student) InvalidResponseException(app.abhijit.iter.exceptions.InvalidResponseException) InvalidResponseException(app.abhijit.iter.exceptions.InvalidResponseException) InvalidCredentialsException(app.abhijit.iter.exceptions.InvalidCredentialsException)

Example 2 with InvalidResponseException

use of app.abhijit.iter.exceptions.InvalidResponseException in project bunk by abhijitparida.

the class ResponseParser method processAttendance.

private HashMap<String, Subject> processAttendance(String attendanceJson) {
    JsonArray attendance;
    try {
        attendance = this.jsonParser.parse(attendanceJson).getAsJsonObject().get("griddata").getAsJsonArray();
    } catch (Exception e) {
        throw new InvalidResponseException();
    }
    HashMap<String, Subject> subjects = new HashMap<>();
    Pattern classesPattern = Pattern.compile("^(\\d+) / (\\d+)$");
    for (int i = 0; i < attendance.size(); i++) {
        JsonObject s = attendance.get(i).getAsJsonObject();
        if (!s.has("Latt") || !s.has("Patt") || !s.has("subject") || !s.has("subjectcode")) {
            throw new InvalidResponseException();
        }
        Subject subject = new Subject();
        subject.name = s.get("subject").getAsString();
        subject.code = s.get("subjectcode").getAsString();
        subject.lastUpdated = new Date().getTime();
        Matcher theoryClassesMatcher = classesPattern.matcher(s.get("Latt").getAsString());
        if (theoryClassesMatcher.find()) {
            subject.theoryPresent = Integer.parseInt(theoryClassesMatcher.group(1));
            subject.theoryTotal = Integer.parseInt(theoryClassesMatcher.group(2));
        }
        Matcher labClassesMatcher = classesPattern.matcher(s.get("Patt").getAsString());
        if (labClassesMatcher.find()) {
            subject.labPresent = Integer.parseInt(labClassesMatcher.group(1));
            subject.labTotal = Integer.parseInt(labClassesMatcher.group(2));
        }
        subjects.put(subject.code, subject);
    }
    return subjects;
}
Also used : JsonArray(com.google.gson.JsonArray) Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) JsonObject(com.google.gson.JsonObject) InvalidResponseException(app.abhijit.iter.exceptions.InvalidResponseException) InvalidResponseException(app.abhijit.iter.exceptions.InvalidResponseException) InvalidCredentialsException(app.abhijit.iter.exceptions.InvalidCredentialsException) Subject(app.abhijit.iter.models.Subject) Date(java.util.Date)

Example 3 with InvalidResponseException

use of app.abhijit.iter.exceptions.InvalidResponseException in project bunk by abhijitparida.

the class LoginActivity method setupLoginButton.

private void setupLoginButton() {
    Button loginButton = findViewById(R.id.login);
    loginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String username = mUsernameInput.getText().toString();
            String password = mPasswordInput.getText().toString();
            mSharedPreferences.edit().putString("pref_student", username).apply();
            mUsernameInput.setEnabled(false);
            mPasswordInput.setEnabled(false);
            mPasswordVisilibity.setEnabled(false);
            mLoginButton.setEnabled(false);
            mLoginButton.setText("LOADING...");
            mLoginButton.setBackgroundResource(R.drawable.bg_login_button_loading);
            ((AnimationDrawable) mLoginButton.getBackground()).start();
            mIterApi.getStudent(username, password, new IterApi.Callback() {

                @Override
                public void onData(@NonNull final Student student) {
                    if (mCache.getStudent(student.username) == null) {
                        Toast.makeText(mContext, "Credentials will be stored on your device until you Logout", Toast.LENGTH_SHORT).show();
                    }
                    mLoginButton.setText(emoji(0x1F60F) + emoji(0x1F60F) + emoji(0x1F60F));
                    new Handler().postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            Intent intent = new Intent(LoginActivity.this, AttendanceActivity.class);
                            intent.putExtra("student", new Gson().toJson(student));
                            startActivity(intent);
                            finish();
                        }
                    }, 750);
                }

                @Override
                public void onError(@NonNull RuntimeException error) {
                    if (error instanceof ConnectionFailedException) {
                        Toast.makeText(mContext, "ITER servers are currently down", Toast.LENGTH_LONG).show();
                    } else if (error instanceof InvalidCredentialsException) {
                        Toast.makeText(mContext, "Invalid credentials", Toast.LENGTH_LONG).show();
                    } else if (error instanceof InvalidResponseException) {
                        Toast.makeText(mContext, "Invalid API response", Toast.LENGTH_LONG).show();
                    }
                    if ((error instanceof InvalidResponseException || error instanceof ConnectionFailedException) && mCache.getStudent(mSharedPreferences.getString("pref_student", null)) != null) {
                        mLoginButton.setText("¯\\_(ツ)_/¯");
                        new Handler().postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                Intent intent = new Intent(LoginActivity.this, AttendanceActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }, 750);
                    } else {
                        mSharedPreferences.edit().putString("pref_student", null).apply();
                        ((AnimationDrawable) mLoginButton.getBackground()).stop();
                        mUsernameInput.setEnabled(true);
                        mPasswordInput.setEnabled(true);
                        mPasswordVisilibity.setEnabled(true);
                        mLoginButton.setBackgroundResource(R.drawable.bg_login_button_error);
                        mLoginButton.setText("ERROR");
                        new Handler().postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                mLoginButton.setEnabled(true);
                                mLoginButton.setText("BUNK!");
                                mLoginButton.setBackgroundResource(R.drawable.bg_login_button);
                            }
                        }, 2000);
                    }
                }
            });
        }
    });
}
Also used : AnimationDrawable(android.graphics.drawable.AnimationDrawable) Handler(android.os.Handler) Gson(com.google.gson.Gson) Intent(android.content.Intent) Student(app.abhijit.iter.models.Student) View(android.view.View) AdapterView(android.widget.AdapterView) AutoCompleteTextView(android.widget.AutoCompleteTextView) Button(android.widget.Button) CompoundButton(android.widget.CompoundButton) InvalidCredentialsException(app.abhijit.iter.exceptions.InvalidCredentialsException) NonNull(android.support.annotation.NonNull) ConnectionFailedException(app.abhijit.iter.exceptions.ConnectionFailedException) InvalidResponseException(app.abhijit.iter.exceptions.InvalidResponseException)

Aggregations

InvalidCredentialsException (app.abhijit.iter.exceptions.InvalidCredentialsException)3 InvalidResponseException (app.abhijit.iter.exceptions.InvalidResponseException)3 Student (app.abhijit.iter.models.Student)2 JsonObject (com.google.gson.JsonObject)2 Intent (android.content.Intent)1 AnimationDrawable (android.graphics.drawable.AnimationDrawable)1 Handler (android.os.Handler)1 NonNull (android.support.annotation.NonNull)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 AutoCompleteTextView (android.widget.AutoCompleteTextView)1 Button (android.widget.Button)1 CompoundButton (android.widget.CompoundButton)1 ConnectionFailedException (app.abhijit.iter.exceptions.ConnectionFailedException)1 Subject (app.abhijit.iter.models.Subject)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1