From 0509b590fb3f6c0766dcb822f0fcb30a50b5bad6 Mon Sep 17 00:00:00 2001 From: Divak2004 Date: Sat, 12 Oct 2024 20:20:15 -0400 Subject: [PATCH 1/7] GSR widgets changes --- PennMobile/src/main/AndroidManifest.xml | 10 ++ .../labs/pennmobile/GsrReservationWidget.kt | 129 ++++++++++++++++++ .../adapters/GsrReservationWidgetAdapter.kt | 126 +++++++++++++++++ .../res/layout/gsr_reservation_widget.xml | 33 +++++ .../layout/gsr_reservation_widget_item.xml | 51 +++++++ .../res/xml/gsr_reservation_widget_info.xml | 13 ++ 6 files changed, 362 insertions(+) create mode 100644 PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt create mode 100644 PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt create mode 100644 PennMobile/src/main/res/layout/gsr_reservation_widget.xml create mode 100644 PennMobile/src/main/res/layout/gsr_reservation_widget_item.xml create mode 100644 PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml diff --git a/PennMobile/src/main/AndroidManifest.xml b/PennMobile/src/main/AndroidManifest.xml index c510c0cf..2a4756a8 100644 --- a/PennMobile/src/main/AndroidManifest.xml +++ b/PennMobile/src/main/AndroidManifest.xml @@ -39,6 +39,16 @@ android:resource="@xml/dining_hall_widget_info" /> + + + + + + + + ?>() {}.type, +// Serializer.VenueSerializer(), +// ) +// +// val gson = gsonBuilder.create() +// val okHttpClient = OkHttpClient() +// okHttpClient.setConnectTimeout(35, TimeUnit.SECONDS) // Connection timeout +// okHttpClient.setReadTimeout(35, TimeUnit.SECONDS) // Read timeout +// okHttpClient.setWriteTimeout(35, TimeUnit.SECONDS) // Write timeout +// +// val restAdapter = +// RestAdapter +// .Builder() +// .setConverter(GsonConverter(gson)) +// .setClient(OkClient(okHttpClient)) +// .setEndpoint("https://pennmobile.org/api") +// .build() +// mGSRReservation = restAdapter.create(GSRReservation::class.java) + // change later; bruh I need dummy values + mGSRReservation = GSRReservation() + } + return mGSRReservation!! + } + } +} \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt new file mode 100644 index 00000000..5b13ebcc --- /dev/null +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt @@ -0,0 +1,126 @@ +package com.pennapps.labs.pennmobile.adapters + +import android.appwidget.AppWidgetManager +import android.content.Context +import android.content.Intent +import android.graphics.BitmapFactory +import android.widget.RemoteViews +import android.widget.RemoteViewsService +import com.google.firebase.crashlytics.FirebaseCrashlytics +import com.pennapps.labs.pennmobile.GsrReservationWidget +import com.pennapps.labs.pennmobile.R +import com.pennapps.labs.pennmobile.classes.GSRReservation +import com.squareup.picasso.Picasso +import org.joda.time.format.DateTimeFormat +import org.joda.time.format.DateTimeFormatter +import java.net.HttpURLConnection +import java.net.URL + + +class GsrReservationWidgetAdapter : RemoteViewsService() { + override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { + return GsrReservationWidgetFactory(applicationContext, intent) + } + + class GsrReservationWidgetFactory( + private val context: Context, + intent: Intent + ) : RemoteViewsFactory { + // Need to change later to actual source + private var mGsrReservation: GSRReservation? = null + private var appWidgetId: Int = + intent.getIntExtra( + AppWidgetManager.EXTRA_APPWIDGET_ID, + AppWidgetManager.INVALID_APPWIDGET_ID, + ) + private var dataSet: List = emptyList() + + override fun onCreate() { + mGsrReservation = GsrReservationWidget.gsrReservationInstance + getWidgetGsrReservations() + } + + // Only when there's a change to the user's reservations + override fun onDataSetChanged() { + getWidgetGsrReservations(); + } + + override fun onDestroy() { + + } + + override fun getCount(): Int { + return dataSet.size + } + + // TODO("Set the image, get building name, and hopefully support click behavior") + override fun getViewAt(index: Int): RemoteViews { + val reservation = dataSet[index] + val roomName = reservation.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 day = from.toString("EEEE, MMMM d") + val fromHour = from.toString("h:mm a") + val toHour = to.toString("h:mm a") + + val imageUrl = reservation.info?.get("thumbnail") ?: + "https://s3.us-east-2.amazonaws.com/labs.api/dining/MBA+Cafe.jpg" + + val views = RemoteViews(context.packageName, R.layout.gsr_reservation_widget_item) + views.setTextViewText(R.id.gsr_reservation_widget_item_location_tv, roomName) + views.setTextViewText( + R.id.gsr_reservation_widget_item_time_tv, "$day\n$fromHour-$toHour") +// Picasso +// .get() +// .load(imageUrl) +// .fit() +// .centerCrop() +// .into() + try { + val urlConnection = URL(imageUrl) + val connection = urlConnection + .openConnection() as HttpURLConnection + connection.doInput = true + connection.connect() + val input = connection.inputStream + val myBitmap = BitmapFactory.decodeStream(input) + views.setImageViewBitmap(R.id.gsr_reservation_widget_item_iv, myBitmap) + } catch (e: Exception) { + e.printStackTrace() + } + + return views + } + + override fun getLoadingView(): RemoteViews? { + return null + } + + override fun getViewTypeCount(): Int { + return 1 + } + + override fun getItemId(id: Int): Long { + return id.toLong(); + } + + override fun hasStableIds(): Boolean { + return true; + } + + // TODO("Actually get the user's reservations") + private fun getWidgetGsrReservations() { + try { + if (mGsrReservation != null) { + dataSet = listOf(mGsrReservation!!) + } + } catch (e: Exception) { + FirebaseCrashlytics.getInstance().recordException(e) + e.printStackTrace() + } + } + } +} \ No newline at end of file diff --git a/PennMobile/src/main/res/layout/gsr_reservation_widget.xml b/PennMobile/src/main/res/layout/gsr_reservation_widget.xml new file mode 100644 index 00000000..9377fbba --- /dev/null +++ b/PennMobile/src/main/res/layout/gsr_reservation_widget.xml @@ -0,0 +1,33 @@ + + + + + + + + \ No newline at end of file diff --git a/PennMobile/src/main/res/layout/gsr_reservation_widget_item.xml b/PennMobile/src/main/res/layout/gsr_reservation_widget_item.xml new file mode 100644 index 00000000..91206eac --- /dev/null +++ b/PennMobile/src/main/res/layout/gsr_reservation_widget_item.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml b/PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml new file mode 100644 index 00000000..a231da1c --- /dev/null +++ b/PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml @@ -0,0 +1,13 @@ + + \ No newline at end of file From 4f79102a2d890553034089a02205dd8f8c6a77a4 Mon Sep 17 00:00:00 2001 From: Divak2004 Date: Sun, 13 Oct 2024 14:17:54 -0400 Subject: [PATCH 2/7] Tried to access user token --- .../pennapps/labs/pennmobile/api/GsrReservationsRequest.java | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java new file mode 100644 index 00000000..380afe0c --- /dev/null +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java @@ -0,0 +1,2 @@ +package com.pennapps.labs.pennmobile.api;public interface GsrReservationsRequest { +} From ae35f7590880b4be1f4223f116a03f66b1f55a82 Mon Sep 17 00:00:00 2001 From: Divak2004 Date: Sun, 20 Oct 2024 02:44:35 -0400 Subject: [PATCH 3/7] GSR Reservation widget mostly done --- PennMobile/src/main/AndroidManifest.xml | 9 ++- .../labs/pennmobile/GsrReservationWidget.kt | 75 ++++++++----------- .../pennapps/labs/pennmobile/MainActivity.kt | 9 +++ .../adapters/DiningHallWidgetAdapter.kt | 10 +-- .../adapters/GsrReservationWidgetAdapter.kt | 55 +++++++++----- .../labs/pennmobile/api/DiningRequest.java | 2 +- .../api/GsrReservationsRequest.java | 24 +++++- .../res/layout/gsr_reservation_widget.xml | 7 +- .../layout/gsr_reservation_widget_item.xml | 34 +++++---- .../res/xml/gsr_reservation_widget_info.xml | 6 +- 10 files changed, 139 insertions(+), 92 deletions(-) diff --git a/PennMobile/src/main/AndroidManifest.xml b/PennMobile/src/main/AndroidManifest.xml index 2a4756a8..9cccb919 100644 --- a/PennMobile/src/main/AndroidManifest.xml +++ b/PennMobile/src/main/AndroidManifest.xml @@ -27,6 +27,7 @@ android:theme="@style/AppTheme.Launcher" android:usesCleartextTraffic="true" tools:targetApi="m"> + @@ -39,13 +40,15 @@ android:resource="@xml/dining_hall_widget_info" /> - - @@ -65,6 +68,8 @@ + ?>() {}.type, -// Serializer.VenueSerializer(), -// ) -// -// val gson = gsonBuilder.create() -// val okHttpClient = OkHttpClient() -// okHttpClient.setConnectTimeout(35, TimeUnit.SECONDS) // Connection timeout -// okHttpClient.setReadTimeout(35, TimeUnit.SECONDS) // Read timeout -// okHttpClient.setWriteTimeout(35, TimeUnit.SECONDS) // Write timeout -// -// val restAdapter = -// RestAdapter -// .Builder() -// .setConverter(GsonConverter(gson)) -// .setClient(OkClient(okHttpClient)) -// .setEndpoint("https://pennmobile.org/api") -// .build() -// mGSRReservation = restAdapter.create(GSRReservation::class.java) - // change later; bruh I need dummy values - mGSRReservation = GSRReservation() + if (mGSRReservationsRequest == null) { + val gsonBuilder = GsonBuilder() + + // RegisterTypeAdapter with GsrReservationSerializer + // since we are only accessing gsr reservations + gsonBuilder.registerTypeAdapter( + object : TypeToken?>() {}.type, + Serializer.GsrReservationSerializer(), + ) + + val gson = gsonBuilder.create() + val okHttpClient = OkHttpClient() + okHttpClient.setConnectTimeout(35, TimeUnit.SECONDS) // Connection timeout + okHttpClient.setReadTimeout(35, TimeUnit.SECONDS) // Read timeout + okHttpClient.setWriteTimeout(35, TimeUnit.SECONDS) // Write timeout + + val restAdapter = + RestAdapter + .Builder() + .setConverter(GsonConverter(gson)) + .setClient(OkClient(okHttpClient)) + .setEndpoint("https://pennmobile.org/api") + .build() + mGSRReservationsRequest = restAdapter.create(GsrReservationsRequest::class.java) } - return mGSRReservation!! + return mGSRReservationsRequest!! } } } \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt index e7b1ca4f..d40712f9 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt @@ -130,6 +130,15 @@ class MainActivity : AppCompatActivity() { if (diningWidgetBroadCast != -1) { setTab(DINING_ID) } + + var gsrReservationWidgetBroadCast = 0 + if (intent != null) { + gsrReservationWidgetBroadCast = + intent.getIntExtra("Gsr_Tab_Switch", -1) + } + if (gsrReservationWidgetBroadCast != -1) { + setTab(GSR_ID) + } } private fun onExpandableBottomNavigationItemSelected() { diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningHallWidgetAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningHallWidgetAdapter.kt index 4dd10a38..ca3226ec 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningHallWidgetAdapter.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DiningHallWidgetAdapter.kt @@ -88,11 +88,11 @@ class DiningHallWidgetAdapter : RemoteViewsService() { // For future feature of clicking app widget item would direct to the respective dining // hall info page - val extras = Bundle() - extras.putInt("dining_hall_id", dataSet[position].id) - val fillInIntent = Intent() - fillInIntent.putExtras(extras) - views.setOnClickFillInIntent(R.id.widgetBackground, fillInIntent) +// val extras = Bundle() +// extras.putInt("dining_hall_id", dataSet[position].id) +// val fillInIntent = Intent() +// fillInIntent.putExtras(extras) +// views.setOnClickFillInIntent(R.id.widgetBackground, fillInIntent) return views } diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt index 5b13ebcc..f7fc6541 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt @@ -1,18 +1,28 @@ package com.pennapps.labs.pennmobile.adapters +import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.content.Context import android.content.Intent import android.graphics.BitmapFactory +import android.net.Uri +import android.os.Bundle import android.widget.RemoteViews import android.widget.RemoteViewsService +import androidx.preference.PreferenceManager import com.google.firebase.crashlytics.FirebaseCrashlytics import com.pennapps.labs.pennmobile.GsrReservationWidget +import com.pennapps.labs.pennmobile.MainActivity import com.pennapps.labs.pennmobile.R +import com.pennapps.labs.pennmobile.adapters.DiningHallWidgetAdapter.DiningWidgetFactory.Companion.createHall +import com.pennapps.labs.pennmobile.api.CampusExpress +import com.pennapps.labs.pennmobile.api.GsrReservationsRequest +import com.pennapps.labs.pennmobile.api.StudentLife import com.pennapps.labs.pennmobile.classes.GSRReservation -import com.squareup.picasso.Picasso +import kotlinx.android.synthetic.main.loading_panel.loadingPanel import org.joda.time.format.DateTimeFormat import org.joda.time.format.DateTimeFormatter +import rx.Observable import java.net.HttpURLConnection import java.net.URL @@ -26,23 +36,23 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { private val context: Context, intent: Intent ) : RemoteViewsFactory { - // Need to change later to actual source - private var mGsrReservation: GSRReservation? = null + private var mGsrReservationsRequest: GsrReservationsRequest? = null private var appWidgetId: Int = intent.getIntExtra( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID, ) private var dataSet: List = emptyList() + private var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) override fun onCreate() { - mGsrReservation = GsrReservationWidget.gsrReservationInstance + mGsrReservationsRequest = GsrReservationWidget.gsrReservationsRequestInstance getWidgetGsrReservations() } - // Only when there's a change to the user's reservations + // Not used since already handled override fun onDataSetChanged() { - getWidgetGsrReservations(); + } override fun onDestroy() { @@ -53,7 +63,7 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { return dataSet.size } - // TODO("Set the image, get building name, and hopefully support click behavior") + // TODO("Get building name(?), and hopefully support click behavior") override fun getViewAt(index: Int): RemoteViews { val reservation = dataSet[index] val roomName = reservation.name @@ -73,12 +83,7 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { views.setTextViewText(R.id.gsr_reservation_widget_item_location_tv, roomName) views.setTextViewText( R.id.gsr_reservation_widget_item_time_tv, "$day\n$fromHour-$toHour") -// Picasso -// .get() -// .load(imageUrl) -// .fit() -// .centerCrop() -// .into() + try { val urlConnection = URL(imageUrl) val connection = urlConnection @@ -104,18 +109,32 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { } override fun getItemId(id: Int): Long { - return id.toLong(); + return id.toLong() } override fun hasStableIds(): Boolean { - return true; + return true } - // TODO("Actually get the user's reservations") private fun getWidgetGsrReservations() { try { - if (mGsrReservation != null) { - dataSet = listOf(mGsrReservation!!) + if (mGsrReservationsRequest != null) { + val token = sharedPreferences.getString( + context.getString(R.string.access_token), "") + mGsrReservationsRequest!! + .getGsrReservations("Bearer $token") + .flatMap { reservations -> Observable.from(reservations) } + .flatMap { reservation -> + Observable.just(reservation) + }.toList() + .subscribe { reservations -> + dataSet = reservations + println("subscribed") + val appWidgetManager: AppWidgetManager = + AppWidgetManager.getInstance(context) + appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, + R.id.gsr_reservation_widget_stack_view) + } } } catch (e: Exception) { FirebaseCrashlytics.getInstance().recordException(e) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/DiningRequest.java b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/DiningRequest.java index 4ac8bcc0..7f21aa23 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/DiningRequest.java +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/DiningRequest.java @@ -11,4 +11,4 @@ public interface DiningRequest { @GET("/dining/venues") Observable> venues(); -} +} \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java index 380afe0c..a2e45af6 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/api/GsrReservationsRequest.java @@ -1,2 +1,22 @@ -package com.pennapps.labs.pennmobile.api;public interface GsrReservationsRequest { -} +package com.pennapps.labs.pennmobile.api; + +import com.pennapps.labs.pennmobile.classes.AccessTokenResponse; +import com.pennapps.labs.pennmobile.classes.GSRReservation; +import com.pennapps.labs.pennmobile.classes.Venue; + +import java.util.List; + +import retrofit.Callback; +import retrofit.http.Field; +import retrofit.http.FormUrlEncoded; +import retrofit.http.GET; +import retrofit.http.Header; +import retrofit.http.POST; +import rx.Observable; + +public interface GsrReservationsRequest { + @GET("/gsr/reservations") + Observable> getGsrReservations( + @Header("Authorization") String bearerToken + ); +} \ No newline at end of file diff --git a/PennMobile/src/main/res/layout/gsr_reservation_widget.xml b/PennMobile/src/main/res/layout/gsr_reservation_widget.xml index 9377fbba..899efbdb 100644 --- a/PennMobile/src/main/res/layout/gsr_reservation_widget.xml +++ b/PennMobile/src/main/res/layout/gsr_reservation_widget.xml @@ -1,10 +1,12 @@ - + android:theme="@style/Theme.Pennmobileandroid.AppWidgetContainer" + android:clickable="true" + android:id="@+id/gsr_reservation_widget_root" + android:background="@android:color/transparent"> + android:background="#ffffff" + android:clickable="true" + android:id="@+id/gsr_reservation_widget_item_root"> @@ -27,12 +28,14 @@ "@+id/gsr_reservation_widget_item_location_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginStart="16dp" - android:layout_marginTop="16dp" + android:layout_marginStart="8dp" + android:layout_marginTop="8dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" - android:text="VP Weigle Room 110" - android:textSize="14dp" + android:maxLines = "2" + android:minLines="2" + android:text="VP Weigle Room 110 oiwejfoiwjeof" + android:textSize="14sp" android:textStyle="bold" /> diff --git a/PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml b/PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml index a231da1c..adb16ca4 100644 --- a/PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml +++ b/PennMobile/src/main/res/xml/gsr_reservation_widget_info.xml @@ -3,11 +3,11 @@ android:description="@string/app_widget_description" android:initialKeyguardLayout="@layout/gsr_reservation_widget" android:initialLayout="@layout/gsr_reservation_widget" - android:minWidth="300dp" - android:minHeight="90dp" + android:minWidth="250dp" + android:minHeight="40dp" android:previewLayout="@layout/gsr_reservation_widget" android:resizeMode="none" - android:targetCellWidth="3" + android:targetCellWidth="4" android:targetCellHeight="1" android:updatePeriodMillis="1800000" android:widgetCategory="home_screen" /> \ No newline at end of file From f889a1d071a100efe072e66b5df3b987a54c688f Mon Sep 17 00:00:00 2001 From: Divak2004 Date: Sun, 20 Oct 2024 11:47:41 -0400 Subject: [PATCH 4/7] Gsr widget now clickable --- PennMobile/src/main/AndroidManifest.xml | 1 + .../pennmobile/adapters/DiningHallWidgetAdapter.kt | 10 +++++----- .../adapters/GsrReservationWidgetAdapter.kt | 11 +++++------ .../src/main/res/layout/gsr_reservation_widget.xml | 2 -- .../main/res/layout/gsr_reservation_widget_item.xml | 1 - 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/PennMobile/src/main/AndroidManifest.xml b/PennMobile/src/main/AndroidManifest.xml index 9cccb919..d280bc7f 100644 --- a/PennMobile/src/main/AndroidManifest.xml +++ b/PennMobile/src/main/AndroidManifest.xml @@ -45,6 +45,7 @@ android:exported="false"> + Date: Sun, 20 Oct 2024 11:47:49 -0400 Subject: [PATCH 5/7] Gsr widget now clickable --- .../java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt index 17b834e0..e53555b3 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt @@ -7,13 +7,11 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.widget.RemoteViews -import androidx.preference.PreferenceManager import com.google.gson.GsonBuilder import com.google.gson.reflect.TypeToken import com.pennapps.labs.pennmobile.adapters.GsrReservationWidgetAdapter import com.pennapps.labs.pennmobile.api.GsrReservationsRequest import com.pennapps.labs.pennmobile.api.Serializer -import com.pennapps.labs.pennmobile.api.StudentLife import com.pennapps.labs.pennmobile.classes.GSRReservation import com.squareup.okhttp.OkHttpClient import retrofit.RestAdapter From 6b1fe8ebf8dde008c0ec19778d40312beecff3b8 Mon Sep 17 00:00:00 2001 From: Divak2004 Date: Sun, 20 Oct 2024 11:50:56 -0400 Subject: [PATCH 6/7] Gsr widget now clickable --- .../labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt | 1 - PennMobile/src/main/res/layout/gsr_reservation_widget.xml | 5 ++--- .../src/main/res/layout/gsr_reservation_widget_item.xml | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt index 4ef17797..0e8d04ef 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt @@ -1,6 +1,5 @@ package com.pennapps.labs.pennmobile.adapters -import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.content.Context import android.content.Intent diff --git a/PennMobile/src/main/res/layout/gsr_reservation_widget.xml b/PennMobile/src/main/res/layout/gsr_reservation_widget.xml index 597c8711..5e315798 100644 --- a/PennMobile/src/main/res/layout/gsr_reservation_widget.xml +++ b/PennMobile/src/main/res/layout/gsr_reservation_widget.xml @@ -1,11 +1,9 @@ + android:id="@+id/gsr_reservation_widget_root" > Date: Sun, 20 Oct 2024 13:04:35 -0400 Subject: [PATCH 7/7] Ktlint files --- .../labs/pennmobile/GsrReservationWidget.kt | 14 +++-- .../adapters/GsrReservationWidgetAdapter.kt | 58 +++++++++---------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt index e53555b3..09fc9580 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/GsrReservationWidget.kt @@ -52,8 +52,10 @@ class GsrReservationWidget : AppWidgetProvider() { // Setting up the widget remoteViews; change cardview to something else val views = RemoteViews(context.packageName, R.layout.gsr_reservation_widget) views.setRemoteAdapter(R.id.gsr_reservation_widget_stack_view, serviceIntent) - views.setEmptyView(R.id.gsr_reservation_widget_stack_view, - R.id.gsr_reservation_widget_empty_view) + views.setEmptyView( + R.id.gsr_reservation_widget_stack_view, + R.id.gsr_reservation_widget_empty_view, + ) // Setting up the intents for the remoteview for both when it is empty and // when it loads the collection view (in this case we use setPendingIntentTemplate to @@ -63,8 +65,10 @@ class GsrReservationWidget : AppWidgetProvider() { // Notify appwidgetviewdata has changed to call getViewAt to set up the widget UI // and handle update for every appwidget item in the Collection widget. - appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, - R.id.gsr_reservation_widget_stack_view) + appWidgetManager.notifyAppWidgetViewDataChanged( + appWidgetId, + R.id.gsr_reservation_widget_stack_view, + ) appWidgetManager.updateAppWidget(appWidgetId, views) } } @@ -111,4 +115,4 @@ class GsrReservationWidget : AppWidgetProvider() { return mGSRReservationsRequest!! } } -} \ No newline at end of file +} diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt index 0e8d04ef..8d2e2039 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/GsrReservationWidgetAdapter.kt @@ -19,15 +19,12 @@ import rx.Observable import java.net.HttpURLConnection import java.net.URL - class GsrReservationWidgetAdapter : RemoteViewsService() { - override fun onGetViewFactory(intent: Intent): RemoteViewsFactory { - return GsrReservationWidgetFactory(applicationContext, intent) - } + override fun onGetViewFactory(intent: Intent): RemoteViewsFactory = GsrReservationWidgetFactory(applicationContext, intent) class GsrReservationWidgetFactory( private val context: Context, - intent: Intent + intent: Intent, ) : RemoteViewsFactory { private var mGsrReservationsRequest: GsrReservationsRequest? = null private var appWidgetId: Int = @@ -45,16 +42,12 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { // Not used since already handled override fun onDataSetChanged() { - } override fun onDestroy() { - } - override fun getCount(): Int { - return dataSet.size - } + override fun getCount(): Int = dataSet.size // TODO("Get building name(?), and hopefully support click behavior") override fun getViewAt(index: Int): RemoteViews { @@ -69,18 +62,22 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { val fromHour = from.toString("h:mm a") val toHour = to.toString("h:mm a") - val imageUrl = reservation.info?.get("thumbnail") ?: - "https://s3.us-east-2.amazonaws.com/labs.api/dining/MBA+Cafe.jpg" + val imageUrl = + reservation.info?.get("thumbnail") + ?: "https://s3.us-east-2.amazonaws.com/labs.api/dining/MBA+Cafe.jpg" val views = RemoteViews(context.packageName, R.layout.gsr_reservation_widget_item) views.setTextViewText(R.id.gsr_reservation_widget_item_location_tv, roomName) views.setTextViewText( - R.id.gsr_reservation_widget_item_time_tv, "$day\n$fromHour-$toHour") + R.id.gsr_reservation_widget_item_time_tv, + "$day\n$fromHour-$toHour", + ) try { val urlConnection = URL(imageUrl) - val connection = urlConnection - .openConnection() as HttpURLConnection + val connection = + urlConnection + .openConnection() as HttpURLConnection connection.doInput = true connection.connect() val input = connection.inputStream @@ -98,27 +95,22 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { return views } - override fun getLoadingView(): RemoteViews? { - return null - } + override fun getLoadingView(): RemoteViews? = null - override fun getViewTypeCount(): Int { - return 1 - } + override fun getViewTypeCount(): Int = 1 - override fun getItemId(id: Int): Long { - return id.toLong() - } + override fun getItemId(id: Int): Long = id.toLong() - override fun hasStableIds(): Boolean { - return true - } + override fun hasStableIds(): Boolean = true private fun getWidgetGsrReservations() { try { if (mGsrReservationsRequest != null) { - val token = sharedPreferences.getString( - context.getString(R.string.access_token), "") + val token = + sharedPreferences.getString( + context.getString(R.string.access_token), + "", + ) mGsrReservationsRequest!! .getGsrReservations("Bearer $token") .flatMap { reservations -> Observable.from(reservations) } @@ -130,8 +122,10 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { println("subscribed") val appWidgetManager: AppWidgetManager = AppWidgetManager.getInstance(context) - appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, - R.id.gsr_reservation_widget_stack_view) + appWidgetManager.notifyAppWidgetViewDataChanged( + appWidgetId, + R.id.gsr_reservation_widget_stack_view, + ) } } } catch (e: Exception) { @@ -140,4 +134,4 @@ class GsrReservationWidgetAdapter : RemoteViewsService() { } } } -} \ No newline at end of file +}