Created
December 13, 2018 12:41
-
-
Save huxaiphaer/8c6e1d0141b4042ba0a1123dbffd6dee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.andela.mrm.room_availability.room_availability_utils; | |
import android.content.Context; | |
import android.content.SharedPreferences; | |
import android.os.Process; | |
import android.support.constraint.ConstraintLayout; | |
import android.view.View; | |
import com.andela.mrm.room_events.CalendarEvent; | |
import com.andela.mrm.room_setup.country.CountryActivity; | |
import com.andela.mrm.util.ConvergeUIUtils; | |
import com.andela.mrm.util.InsertGoogleCalendar; | |
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; | |
import java.util.Map; | |
import io.reactivex.disposables.CompositeDisposable; | |
import static com.andela.mrm.room_setup.country.CountryActivity.SHARED_CALENDAR_ID; | |
import static com.andela.mrm.room_setup.country.CountryActivity.SHARED_PREF_FILENAME; | |
/** | |
* Add a quick meeting and refresh app in background thread. | |
*/ | |
public class AddEventInBackground { | |
Context mContext; | |
public AddEventInBackground(Context mContext) | |
{ | |
this.mContext = mContext; | |
} | |
SharedPreferences sharedPreferences = this.mContext.getSharedPreferences(SHARED_PREF_FILENAME, | |
Context.MODE_PRIVATE); | |
CountryActivity countryActivity = new CountryActivity(); | |
// TODO: sharedPreference should be used to retrieve calendarId instead of hard coding it. | |
String calendarId = countryActivity.getSharedCalendarId(); | |
View mView; | |
Map mSnackbarStringMap; | |
CompositeDisposable mDisposables; | |
/** | |
* Getter for the RoomAvailablityActivity context. | |
* | |
* @param context the room availablity activity context. | |
*/ | |
public void getContext(Context context) { | |
this.mContext = context; | |
} | |
/** | |
* Getter for resources from RoomAvailablityActivity needed to create meeting. | |
* | |
* @param map map containing strings flashed to user for better UX. | |
* @param view the main view. | |
* @param disposables disposables used for background reloading. | |
*/ | |
public void getQuickMeetingResources(Map map, View view, CompositeDisposable disposables) { | |
this.mView = view; | |
this.mSnackbarStringMap = map; | |
this.mDisposables = disposables; | |
} | |
/** | |
* Set up quick meeting. | |
* @param selectedDuration duration of quick meeting. | |
* @param freeDisplay button clicked. | |
* @param credential Google account credentials of logged in user. | |
*/ | |
public void setQuickMeeting(long selectedDuration, ConstraintLayout freeDisplay, | |
GoogleAccountCredential credential) { | |
//TODO:sharedPreference should be used tO retrieve admin email address | |
CalendarEvent calendarEvent = CurrentCalendarEventUtils.getCurrentCalendarEvent(); | |
long currentTime = System.currentTimeMillis(); | |
if (calendarEvent != null && "Available".equals(calendarEvent.getSummary())) { | |
long availableTime = calendarEvent.getEndTime() - currentTime; | |
if (availableTime > selectedDuration) { | |
addEventInBackground(currentTime, selectedDuration, freeDisplay, credential); | |
} else { | |
displayEventSnackbar(freeDisplay, | |
mSnackbarStringMap.get("roomUnavailable").toString()); | |
} | |
} else if (calendarEvent == null && CurrentCalendarEventUtils.calendarEvents.isEmpty()) { | |
addEventInBackground(currentTime, selectedDuration, freeDisplay, credential); | |
} else { | |
displayEventSnackbar(freeDisplay, | |
mSnackbarStringMap.get("roomUnavailable").toString()); | |
} | |
} | |
/** | |
* Add the event and reload activity in the background. | |
* | |
* @param currentTime current device time. | |
* @param selectedTime time selected for event creation. | |
* @param freeDisplay button clicked. | |
* @param credential credentials of signed in google account. | |
*/ | |
private void addEventInBackground( | |
long currentTime, | |
long selectedTime, | |
ConstraintLayout freeDisplay, | |
GoogleAccountCredential credential | |
) { | |
new Thread(() -> { | |
Process.setThreadPriority( | |
Process.THREAD_PRIORITY_BACKGROUND); | |
try { | |
Boolean response = InsertGoogleCalendar.createCalendarEvent( | |
credential, calendarId, currentTime, selectedTime); | |
if (response) { | |
displayEventSnackbar(freeDisplay, | |
mSnackbarStringMap.get("eventSetupSuccess").toString()); | |
} else { | |
displayEventSnackbar(freeDisplay, | |
mSnackbarStringMap.get("eventSetupFail").toString()); | |
} | |
} catch (Exception ex) { | |
// Logs disabled | |
} | |
}).start(); | |
} | |
/** | |
* Display snackbar with appropriate message. Change color of clicked button. | |
* | |
* @param freeDisplay button that was clicked. | |
* @param displayString string to be displayed on snackbar. | |
*/ | |
private void displayEventSnackbar(ConstraintLayout freeDisplay, String displayString) { | |
ConvergeUIUtils.Companion.setButtonColor(mContext, freeDisplay); | |
ConvergeUIUtils.Companion.showSnackBar(mContext, | |
mView, | |
displayString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment