Skip to content

Commit

Permalink
Code Cleanup and Hopefully fix issue #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
theKidOfArcrania committed Sep 15, 2016
1 parent 4e6ef1f commit 372c98b
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 88 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/app/sunstreak/yourpisd/AppRater.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ public static void showRateDialog(final Context mContext, final SharedPreference
dialog.setButton(DialogInterface.BUTTON_POSITIVE, RATE_YOUR_PISD, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
}
});

Expand All @@ -81,7 +85,6 @@ public void onClick(DialogInterface dialog, int id) {
}
});

//TODO: change this
//dialog.show();
dialog.show();
}
}
65 changes: 48 additions & 17 deletions app/src/main/java/app/sunstreak/yourpisd/ClassSwipeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ public class ClassSwipeActivity extends ActionBarActivity {
static ViewPager mViewPager;

static int studentIndex;
static int classesMade = 0;
static int termNum;
static boolean doneMakingClasses;
static Session session;

static Student student;
Expand Down Expand Up @@ -85,6 +83,11 @@ protected void onCreate(Bundle savedInstanceState) {
setTitle(TermFinder.Term.values()[termNum].name);

session = ((YPApplication) getApplication()).session;
if (session == null)
{
logout(false);
return;
}
session.studentIndex = studentIndex;
student = session.getCurrentStudent();
classesForTerm = student.getClassesForTerm(termNum);
Expand Down Expand Up @@ -171,23 +174,50 @@ else if (termNum == ClassReport.NUM_TERMS - 1)
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
// Handle item selection
switch (item.getItemId()) {
case R.id.log_out:
MainActivity.UserLogoutTask logout = new MainActivity.UserLogoutTask();
((YPApplication) getApplication()).session = session = null;
logout.execute(session);
protected void onRestart() {
super.onRestart();
logout(false);
}

//TODO: logout on MainActivity instead
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
@Override
protected void onDestroy() {
super.onDestroy();
logout(false);
}

private void logout(boolean userIntervention)
{
if (session != null)
{
MainActivity.UserLogoutTask logout = new MainActivity.UserLogoutTask();
logout.execute(session);
((YPApplication) getApplication()).session = session = null;

if (userIntervention)
{
SharedPreferences.Editor editor = getSharedPreferences(
"LoginActivity", Context.MODE_PRIVATE).edit();
editor.putBoolean("auto_login", false);
editor.commit();
intent = new Intent(this, LoginActivity.class);
// Clear all activities between this and LoginActivity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}

SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putBoolean("auto_login", false);
editor.commit();
Intent intent = new Intent(this, LoginActivity.class);
// Clear all activities between this and LoginActivity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.log_out: //TODO: add logout button
logout(true);
return true;
/*
case R.id.refresh:
Expand All @@ -198,7 +228,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
*/
case R.id.previous_term:
intent = new Intent(this, ClassSwipeActivity.class);
Intent intent = new Intent(this, ClassSwipeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("studentIndex", studentIndex);
intent.putExtra("classIndex", mViewPager.getCurrentItem());
Expand Down Expand Up @@ -358,6 +388,7 @@ public void onClick(View arg0) {

StringBuilder msg = new StringBuilder();
msg.append(view.getCategory() == null ? "No category" : view.getCategory().getType())
.append("\nMax Grade: " + String.format("%.1f", view.getMaxGrade()))
.append("\nDue Date: " + DateHelper.daysRelative(view.getDueDate()))
.append("\nWeight: x" + String.format("%.1f", view.getWeight()));
try {
Expand Down
34 changes: 24 additions & 10 deletions app/src/main/java/app/sunstreak/yourpisd/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public class MainActivity extends ActionBarActivity {
public SlidingTabLayout slidingTabLayout;
public Toolbar toolbar;

@Override
protected void onRestart() {
super.onRestart();
logout(false);
}

@Override
protected void onDestroy() {
super.onDestroy();
logout(false);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -126,11 +138,10 @@ protected void onCreate(Bundle savedInstanceState) {
session = ((YPApplication) getApplication()).session;
if (session == null)
{
logout();
logout(false);
return;
}


// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
FragmentManager fm = getSupportFragmentManager();
Expand Down Expand Up @@ -278,18 +289,21 @@ public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}

private void logout()
private void logout(boolean userIntervention)
{
if (session != null)
{
UserLogoutTask logout = new UserLogoutTask();
logout.execute(session);
((YPApplication) getApplication()).session = session = null;

Editor editor = getSharedPreferences(
"LoginActivity", Context.MODE_PRIVATE).edit();
editor.putBoolean("auto_login", false);
editor.commit();
if (userIntervention)
{
Editor editor = getSharedPreferences(
"LoginActivity", Context.MODE_PRIVATE).edit();
editor.putBoolean("auto_login", false);
editor.commit();
}
}
// attendanceTask.cancel(true);
// attendanceTask = null;
Expand All @@ -310,7 +324,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.log_out:
logout();
logout(true);
return true;
case R.id.credits:
System.out.println("SHOWING CREDITS");
Expand All @@ -331,7 +345,8 @@ public static class UserLogoutTask extends AsyncTask<Session, Void, Void> {

@Override
protected Void doInBackground(Session... sessions) {
sessions[0].logout();
if (sessions[0] != null)
sessions[0].logout();
return null;
}
}
Expand Down Expand Up @@ -423,7 +438,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public static class SummaryFragment extends YPMainFragment {

public static final String ARG_SEMESTER_NUM = "semester_number";
//TODO: change here.
public static final String[][] COLUMN_HEADERS = {
{"1st", "2nd", "Exam", "Avg"},
{"3rd", "4th", "Exam", "Avg"}};
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/app/sunstreak/yourpisd/TermFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public class TermFinder {

// TODO Hardcoded for 2016-2017 school year
public enum Term {
TERM_0("1st Nine Weeks"),
TERM_1("2nd Nine Weeks"),
Expand All @@ -13,7 +12,7 @@ public enum Term {

public final String name;

private Term(String name) {
Term(String name) {
this.name = name;
}
}
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/java/app/sunstreak/yourpisd/net/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void parseTermReport(String html, TermReport report) {

String temp = assignment.getElementsByClass("points").get(0).html();
if (temp.isEmpty())
newAssignment.setGrade(-1); //TODO: Grade doesn't exist
newAssignment.setGrade(-1);
else
newAssignment.setGrade(Double.parseDouble(temp));

Expand Down Expand Up @@ -187,16 +187,19 @@ public static void parseGradeSummary(String html, Map<Integer, ClassReport> clas
//Add new class report as needed.
ClassReport report;
if (classes.containsKey(courseID))
{
report = classes.get(courseID);
report.setCourseName(name);
}
else{
// Teacher name
String teacher = courseMain.get(1).children().get(1).getElementsByClass("teacher").get(0).html();
report = new ClassReport(courseID, name);
report.setPeriodNum(Integer.parseInt(period));
report.setTeacherName(teacher);
classes.put(courseID, report);
}

String teacher = courseMain.get(1).children().get(1).getElementsByClass("teacher").get(0).html();
report.setPeriodNum(Integer.parseInt(period));
report.setTeacherName(teacher);

//Create a new term as needed.
TermReport termReport = report.getTerm(termNum);
if (termReport == null)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/app/sunstreak/yourpisd/net/Testing.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Testing {
public static void main(String[] args) {
/*
for (String s : classes)
System.out.println(s + "\t\t" + Student.maxGPA(s));
System.out.println(s + "\t\t" + Student.getMaxGPA(s));
*/

/*
Expand Down
20 changes: 0 additions & 20 deletions app/src/main/java/app/sunstreak/yourpisd/net/data/ClassReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import app.sunstreak.yourpisd.Semester;

public class ClassReport {
//TODO: teacher email
public static int SEMESTER_TERMS = 3;
public static int NUM_TERMS = SEMESTER_TERMS * 2;

private final int classID; //Unique ID that identifies which class this is.
private final TermReport terms[] = new TermReport[NUM_TERMS]; //Terms in this Class.
private int periodNum;
private int average = -1;
private String courseName;
private String teacherName;

Expand Down Expand Up @@ -52,24 +50,6 @@ public int calculateAverage(Semester sem){
return (int) Math.round(grade / weight);
}

public double getMaxGPA() {
String className = getCourseName().toUpperCase();

if (className.contains("PHYS IB SL")
|| className.contains("MATH STDY IB"))
return 4.5;

String[] split = className.split("[\\s()\\d/]+");

for (int i = 0; i < split.length; i++) {
if (split[i].equals("AP") || split[i].equals("IB"))
return 5;
if (split[i].equals("H") || split[i].equals("IH"))
return 4.5;
}
return 4;
}

public void setTerm(int termNum, TermReport term) {
terms[termNum] = term;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Student {

Bitmap studentPictureBitmap;

public static double maxGPA(String className) {
public static double getMaxGPA(String className) {
className = className.toUpperCase();

if (className.contains("PHYS IB SL")
Expand Down Expand Up @@ -195,7 +195,7 @@ public double getGPA(Semester sem) {
if (grade >= 70)
{
//Passing class.
double classGPA = maxGPA(report.getCourseName()) - gpaDifference(grade);
double classGPA = getMaxGPA(report.getCourseName()) - gpaDifference(grade);
pointSum += classGPA;
pointCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import app.sunstreak.yourpisd.net.Parser;
import app.sunstreak.yourpisd.net.Session;

/**
* Created by Henry on 9/3/2016.
*/
public class TermReport {
private final Student student;
private final ClassReport classGrades;
Expand Down Expand Up @@ -55,10 +52,6 @@ public List<GradeCategory> getCategories() {
return categories;
}

public Student getStudent() {
return student;
}

public int getGrade() {
return grade;
}
Expand Down

0 comments on commit 372c98b

Please sign in to comment.