Skip to content

Commit

Permalink
Timeline code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatApo committed Sep 8, 2019
1 parent bc75841 commit 5b580bf
Showing 1 changed file with 33 additions and 46 deletions.
79 changes: 33 additions & 46 deletions app/src/main/java/com/dinodevs/pacecalendarwidget/Timeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ private void init() {
} else {
header_pattern = Constants.HEADER_PATTERN_12H;
time_pattern = Constants.TIME_PATTERN_12H;

}

// Show Time/Date
Expand Down Expand Up @@ -151,7 +150,6 @@ public void onClick(View v) {
}
});


// Scroll to top
TextView top = this.mView.findViewById(R.id.backToTop);
top.setOnClickListener(new View.OnClickListener() {
Expand All @@ -174,16 +172,17 @@ private void loadCalendarEvents() {
eventsList = new ArrayList<>();
long next_event = 0;

try {
// Load data
calendarEvents = Settings.System.getString(mContext.getContentResolver(), Constants.CALENDAR_DATA);

if (!(calendarEvents != null && !calendarEvents.isEmpty() && !calendarEvents.equals("{\"events\":[]}")))
Timeline.this.toast("No events found!", true);
// Load data
calendarEvents = Settings.System.getString(mContext.getContentResolver(), Constants.CALENDAR_DATA);

if (calendarEvents == null)
calendarEvents = "{\"events\":[]}";
// Default when undefined/empty
if (calendarEvents == null || calendarEvents.isEmpty()) {
calendarEvents = "{\"events\":[]}";
Timeline.this.toast("Empty or null events data!", true);
}

// Read Data
try {
// Check if correct form of JSON
JSONObject json_data = new JSONObject(calendarEvents);

Expand All @@ -199,7 +198,9 @@ private void loadCalendarEvents() {
// Get data
for(int i=0; i<event_number; i++) {
JSONArray data = json_data.getJSONArray(Constants.EVENTS_DATA).getJSONArray(i);

HashMap<String, String> event = new HashMap<>();
event.put(DOT, mContext.getResources().getString(R.string.bull) );

// adding each child node to HashMap key => value
event.put(TITLE, data.getString(0));
Expand All @@ -209,19 +210,19 @@ private void loadCalendarEvents() {
//event.put("location", data.getString(4));
//event.put("account", data.getString(5));

// Get event "start" data
String start;
String end = "";
String location = "";

if(!data.getString(2).equals("") && !data.getString(2).equals("null")) {
calendar.setTimeInMillis(Long.parseLong(data.getString(2)));

if(current_time > calendar.getTimeInMillis()){
// Event expired, go to next
continue;
}
if( next_event ==0 ) // Hence this is the next event
next_event = calendar.getTimeInMillis();

// Mark first next event
if( next_event == 0 )
next_event = calendar.getTimeInMillis();// TODO not used

start = dateToString(calendar, time_pattern);

Expand All @@ -240,51 +241,37 @@ private void loadCalendarEvents() {
continue;
}

if(!data.getString(3).equals("") && !data.getString(3).equals("null")) {
calendar.setTimeInMillis(Long.parseLong(data.getString(3)));
end = " - "+ dateToString(calendar, time_pattern);
}
// Get event "end" data
String end = "";

//All day events
// All day events
if((start.startsWith("00") || start.startsWith("12")) && ("null".equals(data.getString(3)) || data.getString(3).isEmpty())) {
start = getString(R.string.all_day);
end = "";
}else{
if(!data.getString(3).equals("") && !data.getString(3).equals("null")) {
calendar.setTimeInMillis(Long.parseLong(data.getString(3)));
end = " - "+ dateToString(calendar, time_pattern);
}
}

if(!data.getString(4).equals("") && !data.getString(4).equals("null")) {
// Get location data
String location = "";
if(!data.getString(4).equals("") && !data.getString(4).equals("null"))
location = "\n@ "+data.getString(4);
}

event.put(SUBTITLE, start + end + location );
event.put(DOT, mContext.getResources().getString(R.string.bull) );
// adding events to events list

// Add event to events list
eventsList.add(event);
}
}else{
HashMap<String, String> event = new HashMap<>();
event.put("title", getString(R.string.no_events));
//event.put("description", "-");
//event.put("start", "-");
//event.put("end", "-");
//event.put("location", "-");
//event.put("account", "-");
event.put(SUBTITLE, "-");
event.put(DOT, "" );
eventsList.add(event);
Timeline.this.toast("No events data in JSON.", true);
}
} catch (JSONException e) {
//default
HashMap<String, String> event = new HashMap<>();
event.put(TITLE, getString(R.string.no_events));
//event.put("description", "-");
//event.put("start", "-");
//event.put("end", "-");
//event.put("location", "-");
//event.put("account", "-");
event.put(SUBTITLE, "-");
event.put(DOT, "" );
eventsList.add(event);
Timeline.this.toast("Corrupted data!", true);
}

// No events message
if(eventsList.isEmpty()){
HashMap<String, String> elem = new HashMap<>();
elem.put("title", "\n"+mContext.getResources().getString(R.string.no_events));
Expand Down

0 comments on commit 5b580bf

Please sign in to comment.