Merge pull request #39 from Monke14/dev

Android widget fix
This commit is contained in:
Márton Kiss 2023-09-04 23:14:38 +02:00 committed by GitHub
commit c29ab3de29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 47 deletions

View File

@ -26,6 +26,11 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import hu.refilc.naplo.database.DBManager; import hu.refilc.naplo.database.DBManager;
import hu.refilc.naplo.MainActivity; import hu.refilc.naplo.MainActivity;
@ -247,71 +252,61 @@ public class WidgetTimetable extends HomeWidgetProvider {
} }
public static List<JSONArray> genJsonDays(Context context) { public static List<JSONArray> genJsonDays(Context context) {
List<JSONArray> gen_days = new ArrayList<>(); List<JSONArray> genDays = new ArrayList<>();
Map<String, JSONArray> dayMap = new HashMap<>();
DBManager dbManager = new DBManager(context.getApplicationContext()); DBManager dbManager = new DBManager(context.getApplicationContext());
try { try {
dbManager.open(); dbManager.open();
Cursor ct = dbManager.fetchTimetable(); Cursor ct = dbManager.fetchTimetable();
dbManager.close();
if (ct.getCount() == 0) { if (ct.getCount() == 0) {
return gen_days; return genDays;
} }
JSONObject fecthtt = new JSONObject(ct.getString(0)); JSONObject fetchedTimetable = new JSONObject(ct.getString(0));
String currentWeek = String.valueOf(Week.current().id());
JSONArray week = fetchedTimetable.getJSONArray(currentWeek);
JSONArray dayArray = new JSONArray(); // Organize lessons into dates
String currday = ""; for (int i = 0; i < week.length(); i++) {
String currweek = String.valueOf(Week.current().id());
JSONArray week = fecthtt.getJSONArray(currweek);
for (int i=0; i < week.length(); i++)
{
try { try {
if(i == 0) currday = week.getJSONObject(0).getString("Datum"); JSONObject entry = week.getJSONObject(i);
JSONObject oraObj = week.getJSONObject(i); String date = entry.getString("Datum");
dayMap.computeIfAbsent(date, k -> new JSONArray()).put(entry);
if(!currday.equals(oraObj.getString("Datum"))) { } catch (JSONException e) {
gen_days.add(dayArray);
currday = week.getJSONObject(i).getString("Datum");
dayArray = new JSONArray();
}
dayArray.put(oraObj);
if(i == week.length() - 1) {
gen_days.add(dayArray);
}
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} catch (Exception e) {
e.printStackTrace();
}
Collections.sort(gen_days, new Comparator<JSONArray>() { genDays.addAll(dayMap.values());
public int compare(JSONArray a, JSONArray b) {
long valA = 0;
long valB = 0;
// Sort the 'genDays' list of JSON based on the start time of the first entry
genDays.sort((day1, day2) -> {
try { try {
valA = (long) new DateTime( a.getJSONObject(0).getString("Datum")).getMillis(); // Extract the start time of the first entry in each day's JSON
valB = (long) new DateTime( b.getJSONObject(0).getString("Datum")).getMillis(); String startTime1 = day1.getJSONObject(0).getString("KezdetIdopont");
} String startTime2 = day2.getJSONObject(0).getString("KezdetIdopont");
catch (JSONException ignored) { // Compare the start times and return the result for sorting
} return startTime1.compareTo(startTime2);
} catch (JSONException e) {
return (int) (valA - valB); e.printStackTrace();
return 0;
} }
}); });
return gen_days; } catch (Exception e) {
e.printStackTrace();
} finally {
dbManager.close();
} }
return genDays;
}
public static String zeroPad(int value, int padding){ public static String zeroPad(int value, int padding){
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
b.append(value); b.append(value);

View File

@ -335,10 +335,12 @@ public class WidgetTimetableDataProvider implements RemoteViewsService.RemoteVie
public Lesson jsonToLesson(JSONObject json) { public Lesson jsonToLesson(JSONObject json) {
try { try {
String name = json.getString("Nev");
name = name.substring(0, 1).toUpperCase() + name.substring(1); // Capitalize name
return new Lesson( return new Lesson(
json.getJSONObject("Allapot").getString("Nev"), json.getJSONObject("Allapot").getString("Nev"),
!json.getString("Oraszam").equals("null") ? json.getString("Oraszam") : "+", !json.getString("Oraszam").equals("null") ? json.getString("Oraszam") : "+",
json.getString("Nev"), name,
json.getString("Tema"), json.getString("Tema"),
json.getString("TeremNeve"), json.getString("TeremNeve"),
new DateTime(json.getString("KezdetIdopont")).getMillis(), new DateTime(json.getString("KezdetIdopont")).getMillis(),