|
| 1 | +package de.domjos.unitrackermobile.activities; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.graphics.Typeface; |
| 5 | +import android.graphics.drawable.Drawable; |
| 6 | +import android.os.Build; |
| 7 | +import android.view.View; |
| 8 | +import android.widget.LinearLayout; |
| 9 | +import android.widget.ProgressBar; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import com.applandeo.materialcalendarview.CalendarUtils; |
| 13 | +import com.applandeo.materialcalendarview.CalendarView; |
| 14 | +import com.applandeo.materialcalendarview.EventDay; |
| 15 | + |
| 16 | +import java.util.Calendar; |
| 17 | +import java.util.Date; |
| 18 | +import java.util.LinkedList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import de.domjos.unitrackerlibrary.interfaces.IBugService; |
| 22 | +import de.domjos.unitrackerlibrary.model.issues.Issue; |
| 23 | +import de.domjos.unitrackerlibrary.model.objects.DescriptionObject; |
| 24 | +import de.domjos.unitrackerlibrary.model.projects.Project; |
| 25 | +import de.domjos.unitrackerlibrary.model.projects.Version; |
| 26 | +import de.domjos.unitrackerlibrary.services.engine.Authentication; |
| 27 | +import de.domjos.unitrackerlibrary.tasks.AbstractTask; |
| 28 | +import de.domjos.unitrackerlibrary.tasks.IssueTask; |
| 29 | +import de.domjos.unitrackerlibrary.tasks.ProjectTask; |
| 30 | +import de.domjos.unitrackerlibrary.tasks.VersionTask; |
| 31 | +import de.domjos.unitrackerlibrary.utils.MessageHelper; |
| 32 | +import de.domjos.unitrackermobile.R; |
| 33 | +import de.domjos.unitrackermobile.custom.AbstractActivity; |
| 34 | +import de.domjos.unitrackermobile.custom.CustomEventDay; |
| 35 | +import de.domjos.unitrackermobile.helper.Helper; |
| 36 | + |
| 37 | +public class CalendarActivity extends AbstractActivity { |
| 38 | + private CalendarView calendarView; |
| 39 | + private ProgressBar progressBar; |
| 40 | + private LinearLayout llToObject; |
| 41 | + private TextView lblCalendarTitle, lblCalendarSubTitle; |
| 42 | + private List<EventDay> eventDays; |
| 43 | + private Intent intent; |
| 44 | + |
| 45 | + |
| 46 | + public CalendarActivity() { |
| 47 | + super(R.layout.calendar_activity); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void initActions() { |
| 52 | + this.calendarView.setOnDayClickListener(eventDay -> { |
| 53 | + if(eventDay instanceof CustomEventDay) { |
| 54 | + CustomEventDay customEventDay = (CustomEventDay) eventDay; |
| 55 | + |
| 56 | + this.lblCalendarSubTitle.setText(customEventDay.getDescriptionObject().getTitle()); |
| 57 | + if(customEventDay.getDescriptionObject() instanceof Project) { |
| 58 | + this.lblCalendarTitle.setText(this.getString(R.string.projects)); |
| 59 | + this.intent = new Intent(this.getApplicationContext(), ProjectActivity.class); |
| 60 | + } else if(customEventDay.getDescriptionObject() instanceof Version) { |
| 61 | + this.lblCalendarTitle.setText(this.getString(R.string.versions)); |
| 62 | + this.intent = new Intent(this.getApplicationContext(), VersionActivity.class); |
| 63 | + } else if(customEventDay.getDescriptionObject() instanceof Issue) { |
| 64 | + this.lblCalendarTitle.setText(this.getString(R.string.issues)); |
| 65 | + this.intent = new Intent(this.getApplicationContext(), MainActivity.class); |
| 66 | + } else { |
| 67 | + this.lblCalendarTitle.setText(this.getString(R.string.calendar)); |
| 68 | + this.intent = null; |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + this.llToObject.setOnClickListener(view -> { |
| 75 | + if(intent!=null) { |
| 76 | + startActivity(intent); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void initControls() { |
| 83 | + this.calendarView = this.findViewById(R.id.cvEventCalendar); |
| 84 | + this.progressBar = this.findViewById(R.id.pbCalendar); |
| 85 | + |
| 86 | + this.llToObject = this.findViewById(R.id.llToObject); |
| 87 | + this.lblCalendarTitle = this.findViewById(R.id.lblCalendarTitle); |
| 88 | + this.lblCalendarSubTitle = this.findViewById(R.id.lblCalendarSubTitle); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void reload() { |
| 93 | + this.insertEvents(); |
| 94 | + } |
| 95 | + |
| 96 | + private void insertEvents() { |
| 97 | + try { |
| 98 | + this.progressBar.setVisibility(View.VISIBLE); |
| 99 | + List<Authentication> accounts = MainActivity.GLOBALS.getSqLiteGeneral().getAccounts(""); |
| 100 | + this.eventDays = new LinkedList<>(); |
| 101 | + for(Authentication account : accounts) { |
| 102 | + IBugService bugService = Helper.getCurrentBugService(account, this.getApplicationContext()); |
| 103 | + |
| 104 | + ProjectTask projectTask = new ProjectTask(CalendarActivity.this, bugService, false, false, R.drawable.ic_apps_black_24dp); |
| 105 | + projectTask.after(new AbstractTask.PostExecuteListener() { |
| 106 | + @Override |
| 107 | + public void onPostExecute(Object o) { |
| 108 | + if(o instanceof List) { |
| 109 | + List objects = (List) o; |
| 110 | + for (Object obj : objects) { |
| 111 | + if(obj instanceof Project) { |
| 112 | + Project project = (Project) obj; |
| 113 | + addEvent(project.getReleasedAt(), project.getTitle(), project); |
| 114 | + |
| 115 | + VersionTask versionTask = new VersionTask(CalendarActivity.this, bugService, project.getId(), false, false, "versions", R.drawable.ic_update_black_24dp); |
| 116 | + versionTask.after(new AbstractTask.PostExecuteListener() { |
| 117 | + @Override |
| 118 | + public void onPostExecute(Object o) { |
| 119 | + List objList = (List) o; |
| 120 | + for(Object obj : objList) { |
| 121 | + Version version = (Version) obj; |
| 122 | + addEvent(version.getReleasedVersionAt(), String.format("%s: %s", project.getTitle(), version.getTitle()), version); |
| 123 | + } |
| 124 | + } |
| 125 | + }); |
| 126 | + versionTask.execute(0); |
| 127 | + |
| 128 | + IssueTask issueTask = new IssueTask(CalendarActivity.this, bugService, project.getId(), false, false, false, R.drawable.ic_bug_report_black_24dp); |
| 129 | + issueTask.after(new AbstractTask.PostExecuteListener() { |
| 130 | + @Override |
| 131 | + public void onPostExecute(Object o) { |
| 132 | + List objList = (List) o; |
| 133 | + loadingFinish(0, objList.size()); |
| 134 | + int i = 0; |
| 135 | + for(Object obj : objList) { |
| 136 | + Issue issue = (Issue) obj; |
| 137 | + |
| 138 | + IssueTask detailTasks = new IssueTask(CalendarActivity.this, bugService, project.getId(), false, true, false, R.drawable.ic_bug_report_black_24dp); |
| 139 | + detailTasks.after(new AbstractTask.PostExecuteListener() { |
| 140 | + @Override |
| 141 | + public void onPostExecute(Object o) { |
| 142 | + List obj = (List) o; |
| 143 | + Issue detailIssue = (Issue) obj.get(0); |
| 144 | + if(detailIssue.getDueDate() != null) { |
| 145 | + addEvent(detailIssue.getDueDate().getTime(), String.format("%s: %s", project.getTitle(), detailIssue.getTitle()), detailIssue); |
| 146 | + } |
| 147 | + } |
| 148 | + }); |
| 149 | + detailTasks.execute(issue.getId()); |
| 150 | + i++; |
| 151 | + loadingFinish(i, objList.size()); |
| 152 | + } |
| 153 | + } |
| 154 | + }); |
| 155 | + issueTask.execute(0); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + }); |
| 161 | + projectTask.execute(0); |
| 162 | + } |
| 163 | + } catch (Exception ex) { |
| 164 | + MessageHelper.printException(ex, CalendarActivity.this); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void loadingFinish(int current, int max) { |
| 169 | + this.progressBar.setMax(max); |
| 170 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 171 | + this.progressBar.setProgress(current, true); |
| 172 | + } else { |
| 173 | + this.progressBar.setProgress(current); |
| 174 | + } |
| 175 | + if(current==max) { |
| 176 | + this.progressBar.setVisibility(View.GONE); |
| 177 | + } else { |
| 178 | + this.progressBar.setVisibility(View.VISIBLE); |
| 179 | + } |
| 180 | + this.progressBar.invalidate(); |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + private void addEvent(long time, String title, DescriptionObject descriptionObject) { |
| 185 | + Calendar calendar = Calendar.getInstance(); |
| 186 | + Date dt = new Date(); |
| 187 | + dt.setTime(time); |
| 188 | + calendar.setTime(dt); |
| 189 | + |
| 190 | + Drawable drawable = CalendarUtils.getDrawableText(this.getApplicationContext(), title, Typeface.DEFAULT_BOLD, R.color.colorPrimary, 14); |
| 191 | + CustomEventDay customEventDay = new CustomEventDay(calendar, drawable, R.color.colorPrimary); |
| 192 | + customEventDay.setDescriptionObject(descriptionObject); |
| 193 | + this.eventDays.add(customEventDay); |
| 194 | + this.calendarView.setEvents(this.eventDays); |
| 195 | + this.calendarView.invalidate(); |
| 196 | + } |
| 197 | +} |
0 commit comments