Skip to content

Commit

Permalink
Start fix for GSR Booking
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Mar 22, 2024
1 parent ca635d6 commit 0c75e50
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import com.pennapps.labs.pennmobile.adapters.GsrReservationsAdapter
import com.pennapps.labs.pennmobile.databinding.FragmentGsrReservationsBinding

import kotlinx.android.synthetic.main.loading_panel.loadingPanel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class GsrReservationsFragment : Fragment() {

Expand All @@ -26,7 +30,6 @@ class GsrReservationsFragment : Fragment() {
private var _binding : FragmentGsrReservationsBinding? = null
private val binding get() = _binding!!


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -46,8 +49,6 @@ class GsrReservationsFragment : Fragment() {
binding.gsrReservationsRefreshLayout.setColorSchemeResources(R.color.color_accent, R.color.color_primary)
binding.gsrReservationsRefreshLayout.setOnRefreshListener { getReservations() }

getReservations()

return view
}

Expand All @@ -58,16 +59,7 @@ class GsrReservationsFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (!isOnline(context)) {
binding.internetConnectionGSRReservations.setBackgroundColor(resources.getColor(R.color.darkRedBackground))
binding.internetConnectionMessageGsrReservations.text = "Not Connected to Internet"
binding.internetConnectionGSRReservations.visibility = View.VISIBLE
binding.gsrReservationsRefreshLayout.isRefreshing = false
loadingPanel?.visibility = View.GONE
binding.gsrNoReservations.visibility = View.VISIBLE
} else {
binding.internetConnectionGSRReservations.visibility = View.GONE
}
getReservations()
}

private fun getReservations() {
Expand All @@ -91,37 +83,53 @@ class GsrReservationsFragment : Fragment() {
val sessionID = sp.getString(getString(R.string.huntsmanGSR_SessionID), "")
val email = sp.getString(getString(R.string.email_address), "")
val token = sp.getString(getString(R.string.access_token), "")
labs.getGsrReservations("Bearer $token").subscribe({ reservations ->
mActivity.runOnUiThread {
loadingPanel?.visibility = View.GONE

try {
CoroutineScope(Dispatchers.IO).launch {
val response = labs.getGsrReservations2("Bearer $token")
if (response.isSuccessful) {
val reservations = response.body()!!
withContext(Dispatchers.Main) {
loadingPanel?.visibility = View.GONE
binding.gsrReservationsRv.adapter = GsrReservationsAdapter(ArrayList(reservations))
if (reservations.size > 0) {
binding.gsrNoReservations.visibility = View.GONE
} else {
binding.gsrNoReservations.visibility = View.VISIBLE
withContext(Dispatchers.Main) {
if (reservations.size > 0) {
binding.gsrNoReservations.visibility = View.GONE
} else {
binding.gsrNoReservations.visibility = View.VISIBLE
}
// stop refreshing
binding.gsrReservationsRefreshLayout.isRefreshing = false
}
// stop refreshing
binding.gsrReservationsRefreshLayout.isRefreshing = false
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
}
} else {
withContext(Dispatchers.Main) {
Log.e("GsrReservationsFragment", "Error getting reservations")
loadingPanel?.visibility = View.GONE
try {
binding.gsrReservationsRv.adapter = GsrReservationsAdapter(ArrayList())
binding.gsrNoReservations.visibility = View.VISIBLE
binding.gsrReservationsRefreshLayout.isRefreshing = false
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
}
}
}
}, { throwable ->
}
/*labs.getGsrReservations("Bearer $token").subscribe({ reservations ->
mActivity.runOnUiThread {
Log.e("GsrReservationsFragment", "Error getting reservations", throwable)
throwable.printStackTrace()
loadingPanel?.visibility = View.GONE
Log.i("Fleegle", "fleegle")
try {
binding.gsrReservationsRv.adapter = GsrReservationsAdapter(ArrayList())
binding.gsrNoReservations.visibility = View.VISIBLE
binding.gsrReservationsRefreshLayout.isRefreshing = false
Log.i("Fleegle", "${reservations.size}")
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
}
}
})
}, { throwable ->
mActivity.runOnUiThread {
}
})*/
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,8 @@ class MainActivity : AppCompatActivity() {
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<Contact?>?>() {}.type, DataSerializer<Any?>())
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<Venue?>?>() {}.type, VenueSerializer())
gsonBuilder.registerTypeAdapter(DiningHall::class.java, MenuSerializer())
// gets room
gsonBuilder.registerTypeAdapter(object : TypeToken<LaundryRoom?>() {}.type, LaundryRoomSerializer())
// gets laundry room list
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<LaundryRoomSimple?>?>() {}.type, LaundryRoomListSerializer())
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<GSRLocation?>?>() {}.type, GsrLocationSerializer())
// gets laundry usage
gsonBuilder.registerTypeAdapter(object : TypeToken<LaundryUsage?>() {}.type, LaundryUsageSerializer())
// gets laundry preferences (used only for testing)
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<Int?>?>() {}.type, LaundryPrefSerializer())
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<FlingEvent?>?>() {}.type, FlingEventSerializer())
// gets gsr reservations
gsonBuilder.registerTypeAdapter(object : TypeToken<MutableList<GSRReservation?>?>() {}.type, GsrReservationSerializer())
// gets user
gsonBuilder.registerTypeAdapter(Account::class.java, UserSerializer())
// gets posts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ class GsrReservationsAdapter(private var reservations: ArrayList<GSRReservation>
override fun onBindViewHolder(holder: GsrReservationViewHolder, position: Int) {
val reservation = reservations[position]

val roomName = reservation.name
val roomName = reservation.gsr!!.name

val formatter: DateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ")
val from = formatter.parseDateTime(reservation.fromDate)
val to = formatter.parseDateTime(reservation.toDate)
val from = formatter.parseDateTime(reservation.start)
val to = formatter.parseDateTime(reservation.end)
val day = from.toString("EEEE, MMMM d")
val fromHour = from.toString("h:mm a")
val toHour = to.toString("h:mm a")

// huntsman reservation responses don't have an image url so we set it here
val imageUrl = reservation.info?.get("thumbnail") ?: "https://s3.us-east-2.amazonaws.com/labs.api/dining/MBA+Cafe.jpg"
val imageUrl = reservation.gsr?.image_url?: "https://s3.us-east-2.amazonaws.com/labs.api/dining/MBA+Cafe.jpg"
Picasso.get().load(imageUrl).fit().centerCrop().into(holder.itemView.gsr_reservation_iv)

holder.itemView.gsr_reservation_location_tv.text = roomName
Expand All @@ -67,10 +67,7 @@ class GsrReservationsAdapter(private var reservations: ArrayList<GSRReservation>
(mContext as MainActivity).mNetworkManager.getAccessToken {

val sp = PreferenceManager.getDefaultSharedPreferences(mContext)
val sessionID = if (reservation.info == null) sp.getString(
mContext.getString(R.string.huntsmanGSR_SessionID),
""
) else null
val sessionID = ""

val labs = MainActivity.studentLifeInstance
val bearerToken =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public List<FlingEvent> deserialize(JsonElement json, Type typeOfT, JsonDeserial
}

// for GSR Reservations
public static class GsrReservationSerializer implements JsonDeserializer<List<GSRReservation>> {
/*public static class GsrReservationSerializer implements JsonDeserializer<List<GSRReservation>> {
@Override
public List<GSRReservation> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
Expand All @@ -236,7 +236,7 @@ public List<GSRReservation> deserialize(JsonElement je, Type type, JsonDeseriali
}
return reservationList;
}
}
}*/

// for custom posts
public static class PostsSerializer implements JsonDeserializer<List<Post>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ interface StudentLife {
@GET("gsr/reservations")
fun getGsrReservations(@Header("Authorization") bearerToken: String): Observable<List<GSRReservation>>

@GET("gsr/reservations")
suspend fun getGsrReservations2(@Header("Authorization") bearerToken: String): Response<List<GSRReservation>>

@FormUrlEncoded
@POST("gsr/cancel/")
fun cancelReservation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pennapps.labs.pennmobile.classes

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

//class that keeps track of all the GSR rooms themselves
class GSR2 {

@SerializedName("id")
@Expose
var id : Int? = null

@SerializedName("kind")
@Expose
var kind: String? = null

@SerializedName("lid")
@Expose
var lid: String? = null

@SerializedName("gid")
@Expose
var gid: Int? = null

@SerializedName("name")
@Expose
var name: String? = null

@SerializedName("image_url")
@Expose
var image_url: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,28 @@ class GSRReservation {
@JvmField
var booking_id: String? = null

@SerializedName("name")
@SerializedName("gsr")
@Expose
@JvmField
var name: String? = null
var gsr: GSR2? = null

@SerializedName("fromDate")
@SerializedName("room_id")
@Expose
@JvmField
var fromDate: String? = null
var room_id: Int? = null

@SerializedName("toDate")
@SerializedName("room_name")
@Expose
@JvmField
var toDate: String? = null
var room_name: String? = null

@SerializedName("gid")
@SerializedName("start")
@Expose
@JvmField
var gid: String? = null
var start: String? = null

@SerializedName("lid")
@Expose
var lid: String? = null

@SerializedName("info")
@SerializedName("end")
@Expose
@JvmField
var info: Map<String, String>? = null
var end: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Venue {
var venueType: String? = null
var extras: ArrayList<String>? = null

//@SerializedName("dateHours")
var hours: List<VenueInterval> = ArrayList()

/**
Expand Down

0 comments on commit 0c75e50

Please sign in to comment.