44import android .content .Context ;
55import android .content .SharedPreferences ;
66import android .support .v7 .app .AppCompatActivity ;
7+ import android .view .View ;
8+ import android .widget .AdapterView ;
79import android .widget .ArrayAdapter ;
10+ import android .widget .LinearLayout ;
11+ import android .widget .ScrollView ;
812import android .widget .Spinner ;
913import android .widget .TextView ;
1014
1115import java .util .ArrayList ;
16+ import java .util .Calendar ;
1217
1318//contains the base of what an activity using ConnectionThread
1419// and ListenerThread should have
@@ -157,4 +162,132 @@ public int getLocationInSharedMessages(String message) {
157162 }
158163 return -1 ;
159164 }
165+
166+ public void openScheduleViewer () {
167+ ScrollView scheduleViewer = new ScrollView (this );
168+
169+ final LinearLayout scheduleViewerLayout = (LinearLayout ) getLayoutInflater ().inflate (R .layout .schedule_viewer , null );
170+
171+ //figure out when the schedule was last updated
172+ TextView lastUpdated = scheduleViewerLayout .findViewById (R .id .scheduleViewerLastUpdate );
173+ String lastUpdatedMessage = "" ;
174+ SharedPreferences lastUpdatedPrefs = getSharedPreferences ("lastScheduleUpdate" , MODE_PRIVATE );
175+ int year = lastUpdatedPrefs .getInt ("year" , -1 );
176+ int month = lastUpdatedPrefs .getInt ("month" , -1 );
177+ int day = lastUpdatedPrefs .getInt ("day" , -1 );
178+ int hour = lastUpdatedPrefs .getInt ("hour" , -1 );
179+ int minute = lastUpdatedPrefs .getInt ("minute" , -1 );
180+
181+ //get current dates to compare with
182+ int currentYear = Calendar .getInstance ().get (Calendar .YEAR );
183+ int currentMonth = Calendar .getInstance ().get (Calendar .MONTH );
184+ int currentDay = Calendar .getInstance ().get (Calendar .DAY_OF_MONTH );
185+ int currentHour = Calendar .getInstance ().get (Calendar .HOUR );
186+ int currentMinute = Calendar .getInstance ().get (Calendar .MINUTE );
187+
188+ if (year == currentYear ) {
189+ if (month == currentMonth ) {
190+ if (day == currentDay ) {
191+ if (hour == currentHour ) {
192+ lastUpdatedMessage = (currentMinute - minute ) + " minutes ago" ;
193+ } else {
194+ lastUpdatedMessage = (currentHour - hour ) + " hours ago" ;
195+ }
196+ } else {
197+ lastUpdatedMessage = (currentDay - day ) + " days ago" ;
198+ }
199+ } else {
200+ lastUpdatedMessage = (currentMonth - month ) + " months ago" ;
201+ }
202+ } else {
203+ lastUpdatedMessage = (currentYear - year ) + " years ago" ;
204+ }
205+ if (year == -1 ) {
206+ lastUpdatedMessage = "Never" ;
207+ }
208+
209+ //set the message onto the label
210+ lastUpdated .setText ("Last Updated " + lastUpdatedMessage );
211+
212+ userIDSpinner = scheduleViewerLayout .findViewById (R .id .scheduleViewerUserIDSpinner );
213+ updateUserIDSpinner ();
214+
215+ SharedPreferences prefs = getSharedPreferences ("userID" , MODE_PRIVATE );
216+ int userID = prefs .getInt ("userID" , -1 );
217+
218+ if (userID != -1 ) {
219+ //set the spinner to this value
220+ userIDSpinner .setSelection (userID + 1 );
221+
222+ //update the schedule view
223+ updateScheduleDialog (scheduleViewerLayout , userID );
224+ }
225+
226+ //add listener to the spinner
227+ userIDSpinner .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
228+ @ Override
229+ public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
230+ if (position > 0 ) {
231+ //if it is not on the "Choose one" selection
232+ updateScheduleDialog (scheduleViewerLayout , position - 1 );
233+ }
234+ }
235+ @ Override
236+ public void onNothingSelected (AdapterView <?> parent ) {
237+
238+ }
239+ });
240+
241+ //add the layout to the scroll view (the entire view)
242+ scheduleViewer .addView (scheduleViewerLayout );
243+
244+ //create the dialog box
245+ new android .app .AlertDialog .Builder (this )
246+ .setTitle ("View Schedule" )
247+ .setView (scheduleViewer )
248+ .setPositiveButton ("Dismiss" , null )
249+ .show ();
250+ }
251+
252+ public void updateScheduleDialog (View view , int userID ) {
253+ TextView scheduleTextView = view .findViewById (R .id .scheduleViewerTextView );
254+
255+ //show all the times the scout is switching on or off
256+ StringBuilder scheduleMessage = new StringBuilder ();
257+
258+ //the current match number being checked for
259+ int matchNumber = 0 ;
260+
261+ while (matchNumber < schedules .get (userID ).robots .size ()) {
262+ if (!schedules .get (userID ).isOff (matchNumber )) {
263+ //add the next match off
264+ int nextMatchOff = getNextMatchOff (matchNumber , userID );
265+
266+ if (nextMatchOff == -1 ) {
267+ //no more switches on or off, this user is done for the day
268+ break ;
269+ }
270+
271+ scheduleMessage .append ("Off at match " + nextMatchOff + "\n \n " );
272+
273+ //set the match number to check to the match number reached
274+ matchNumber = nextMatchOff ;
275+ } else {
276+ //add the next match on
277+ int nextMatchOn = getNextMatchOn (matchNumber , userID );
278+
279+ if (nextMatchOn == -1 ) {
280+ //no more switches on or off, this user is done for the day
281+ break ;
282+ }
283+
284+ scheduleMessage .append ("On at match " + nextMatchOn + "\n \n " );
285+
286+ //set the match number to check to the match number reached
287+ matchNumber = nextMatchOn ;
288+ }
289+ }
290+
291+ scheduleTextView .setText (scheduleMessage .toString ());
292+ }
160293}
0 commit comments