Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup and optimization #593

Merged
merged 7 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AboutFragment : Fragment() {
mActivity.closeKeyboard()
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentAboutBinding.inflate(inflater, container, false)
val view = binding.root

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import android.graphics.PorterDuff
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.preference.PreferenceManager
import com.pennapps.labs.pennmobile.api.OAuth2NetworkManager
import com.pennapps.labs.pennmobile.api.StudentLife
import com.pennapps.labs.pennmobile.classes.GSRBookingResult
import com.pennapps.labs.pennmobile.databinding.GsrDetailsBookBinding
Expand All @@ -26,6 +24,8 @@ class BookGsrFragment : Fragment() {
private var _binding: GsrDetailsBookBinding? = null
private val binding get() = _binding!!

private lateinit var mActivity: MainActivity

// fields for booking
internal lateinit var firstNameEt: EditText
internal lateinit var lastNameEt: EditText
Expand Down Expand Up @@ -57,8 +57,8 @@ class BookGsrFragment : Fragment() {
roomName = arguments.getString("roomName") ?: ""
}
mStudentLife = MainActivity.studentLifeInstance
val mActivity : MainActivity? = activity as MainActivity
mActivity?.setTitle(R.string.gsr)
mActivity = activity as MainActivity
mActivity.setTitle(R.string.gsr)
}

override fun onResume() {
Expand All @@ -68,7 +68,7 @@ class BookGsrFragment : Fragment() {
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
savedInstanceState: Bundle?): View {
_binding = GsrDetailsBookBinding.inflate(inflater, container, false)
val view = binding.root

Expand All @@ -92,7 +92,7 @@ class BookGsrFragment : Fragment() {
|| emailEt.text.toString().matches("".toRegex())) {
Toast.makeText(activity, "Please fill in all fields before booking",
Toast.LENGTH_LONG).show()
} else if (!emailEt.text.toString().matches("""[\w]+@(seas\.|sas\.|wharton\.|nursing\.)?upenn\.edu""".toRegex())) {
} else if (!emailEt.text.toString().matches("""\w+@(seas\.|sas\.|wharton\.|nursing\.)?upenn\.edu""".toRegex())) {
Toast.makeText(activity, "Please enter a valid Penn email", Toast.LENGTH_LONG).show()
} else {
submit.isClickable = false
Expand All @@ -102,7 +102,6 @@ class BookGsrFragment : Fragment() {
}
}

val mActivity : MainActivity = activity as MainActivity
mActivity.hideBottomBar()

return view
Expand All @@ -115,14 +114,10 @@ class BookGsrFragment : Fragment() {

private fun bookGSR(gsrId: Int, gsrLocationCode: String, startTime: String?, endTime: String?, gid: Int, roomId: Int, roomName: String) {

OAuth2NetworkManager(activity as MainActivity).getAccessToken {
var sessionID = ""
mActivity.mNetworkManager.getAccessToken {
var bearerToken = ""
val sp = PreferenceManager.getDefaultSharedPreferences(activity)
Log.i("Bearer Token", sp.getString(getString(R.string.access_token), "").toString());
activity?.let { activity ->
val sp = PreferenceManager.getDefaultSharedPreferences(activity)
sessionID = sp.getString(getString(R.string.huntsmanGSR_SessionID), "") ?: ""
bearerToken = "Bearer " + sp.getString(getString(R.string.access_token), "").toString()
Log.i("BookGSRFragment", bearerToken)
}
Expand All @@ -133,49 +128,49 @@ class BookGsrFragment : Fragment() {
Log.i("BookGSRFragment", "ID $roomId")
Log.i("BookGSRFragment", "Room Name $roomName")


mStudentLife.bookGSR(
//Passing the values
bearerToken,
startTime,
endTime,
gid,
roomId,
roomName,

//Creating an anonymous callback
object : Callback<GSRBookingResult> {
override fun success(result: GSRBookingResult, response: Response) {
//Displaying the output as a toast and go back to GSR fragment
if (result.getDetail().equals("success")) {
Toast.makeText(activity, "GSR successfully booked", Toast.LENGTH_LONG).show()

// Save user info in shared preferences
val sp = PreferenceManager.getDefaultSharedPreferences(activity)
val editor = sp.edit()
editor.putString(getString(R.string.first_name), firstNameEt.text.toString())
editor.putString(getString(R.string.last_name), lastNameEt.text.toString())
editor.putString(getString(R.string.email_address), emailEt.text.toString())
editor.apply()
mStudentLife.bookGSR(
//Passing the values
bearerToken,
startTime,
endTime,
gid,
roomId,
roomName,

//Creating an anonymous callback
object : Callback<GSRBookingResult> {
override fun success(result: GSRBookingResult, response: Response) {
//Displaying the output as a toast and go back to GSR fragment
if (result.getDetail().equals("success")) {
Toast.makeText(activity, "GSR successfully booked", Toast.LENGTH_LONG).show()

// Save user info in shared preferences
val sp = PreferenceManager.getDefaultSharedPreferences(activity)
val editor = sp.edit()
editor.putString(getString(R.string.first_name), firstNameEt.text.toString())
editor.putString(getString(R.string.last_name), lastNameEt.text.toString())
editor.putString(getString(R.string.email_address), emailEt.text.toString())
editor.apply()
}
else {
Toast.makeText(activity, "GSR booking failed", Toast.LENGTH_LONG).show()
Log.e("BookGsrFragment", "GSR booking failed with " + result.getError())
}
// go back to GSR fragment
binding.loading.loadingPanel.visibility = View.GONE
activity?.onBackPressed()
}
else {
Toast.makeText(activity, "GSR booking failed", Toast.LENGTH_LONG).show()
Log.e("BookGsrFragment", "GSR booking failed with " + result.getError())
}
// go back to GSR fragment
binding.loading.loadingPanel.visibility = View.GONE
activity?.onBackPressed()
}

override fun failure(error: RetrofitError) {
//If any error occurred displaying the error as toast
Log.e("BookGSRFragment", "Error booking gsr", error)
Toast.makeText(activity, "An error has occurred. Please try again.", Toast.LENGTH_LONG).show()
binding.loading.loadingPanel.visibility = View.GONE
activity?.onBackPressed()
override fun failure(error: RetrofitError) {
//If any error occurred displaying the error as toast
Log.e("BookGSRFragment", "Error booking gsr", error)
Toast.makeText(activity, "An error has occurred. Please try again.", Toast.LENGTH_LONG).show()
binding.loading.loadingPanel.visibility = View.GONE
activity?.onBackPressed()
}
}
}
)}
)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DiningFragment : Fragment() {
setHasOptionsMenu(true)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentDiningBinding.inflate(inflater, container, false)
val v = binding.root
binding.diningSwiperefresh.setColorSchemeResources(R.color.color_accent, R.color.color_primary)
Expand All @@ -77,8 +77,7 @@ class DiningFragment : Fragment() {
inflater.inflate(R.menu.dining_sort, menu)
val sp = PreferenceManager.getDefaultSharedPreferences(activity)
// sort the dining halls in the user-specified order
val order = sp.getString("dining_sortBy", "RESIDENTIAL")
when (order) {
when (sp.getString("dining_sortBy", "RESIDENTIAL")) {
"RESIDENTIAL" -> {
menu.findItem(R.id.action_sort_residential).isChecked = true
}
Expand Down Expand Up @@ -198,7 +197,7 @@ class DiningFragment : Fragment() {

companion object {
// Gets the dining hall menus
fun getMenus(venues: MutableList<DiningHall>) : Unit {
fun getMenus(venues: MutableList<DiningHall>) {
val idVenueMap = mutableMapOf<Int, DiningHall>()
venues.forEach { idVenueMap[it.id] = it }
val current = LocalDateTime.now()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DiningHolderFragment : Fragment() {
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
savedInstanceState: Bundle?): View {
_binding = FragmentDiningHolderBinding.inflate(inflater, container, false)
val view = binding.root
view.dining_swiperefresh?.setOnRefreshListener { getConnected() }
Expand Down Expand Up @@ -68,14 +68,14 @@ class DiningHolderFragment : Fragment() {
binding.internetConnectionDiningHolder.visibility = View.VISIBLE
//loadingPanel?.visibility = View.GONE
} else {
binding.internetConnectionDiningHolder?.visibility = View.GONE
binding.internetConnectionDiningHolder.visibility = View.GONE
}
}

override fun onResume() {
super.onResume()
getConnected()
mActivity.removeTabs()
//mActivity.toolbar.visibility = View.GONE
mActivity.setTitle(R.string.dining)
mActivity.setSelectedTab(MainActivity.DINING)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.pennapps.labs.pennmobile

import android.content.Context
import android.graphics.Color
import android.net.ConnectivityManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -113,13 +111,6 @@ class DiningInfoFragment : Fragment() {
return vertical
}

private val isNetworkAvailable: Boolean
get() {
val connectivityManager = mActivity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo = connectivityManager.activeNetworkInfo
return activeNetworkInfo != null && activeNetworkInfo.isConnected
}

override fun onDestroyView() {
setHasOptionsMenu(false)
super.onDestroyView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DiningInsightsFragment : Fragment() {
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
savedInstanceState: Bundle?): View {
_binding = FragmentDiningInsightsBinding.inflate(inflater, container, false)
val view = binding.root

Expand Down Expand Up @@ -101,9 +101,6 @@ class DiningInsightsFragment : Fragment() {
}
}

override fun onResume() {
super.onResume()
}

private fun refresh() {
val accessToken = networkManager.getAccessToken()
Expand All @@ -127,7 +124,7 @@ class DiningInsightsFragment : Fragment() {
private fun getInsights(accessToken: String?) {
if (!isOnline(context)) {
binding.internetConnectionDiningInsights.setBackgroundColor(resources.getColor(R.color.darkRedBackground))
binding.internetConnectionMessageDiningInsights.setText("Not Connected to Internet")
binding.internetConnectionMessageDiningInsights.text = "Not Connected to Internet"
binding.internetConnectionDiningInsights.visibility = View.VISIBLE
binding.diningInsightsRefresh.isRefreshing = false
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.fragment.app.Fragment
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.pennapps.labs.pennmobile.adapters.DiningSettingsAdapter
import com.pennapps.labs.pennmobile.api.OAuth2NetworkManager
import com.pennapps.labs.pennmobile.api.StudentLife
import com.pennapps.labs.pennmobile.classes.DiningHall
import com.pennapps.labs.pennmobile.classes.DiningRequest
Expand Down Expand Up @@ -37,7 +36,7 @@ class DiningSettingsFragment : Fragment() {
mActivity.hideBottomBar()
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentDiningPreferencesBinding.inflate(inflater, container, false)
val v = binding.root
binding.diningHallRv.layoutManager = LinearLayoutManager(context,
Expand Down Expand Up @@ -102,7 +101,7 @@ class DiningSettingsFragment : Fragment() {
favoriteDiningHalls.add(hall.id)
}
}
OAuth2NetworkManager(activity as MainActivity).getAccessToken {
mActivity.mNetworkManager.getAccessToken {
val bearerToken =
"Bearer " + sp.getString(getString(R.string.access_token), "").toString()
mStudentLife.sendDiningPref(bearerToken, DiningRequest(favoriteDiningHalls),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FitnessHolderFragment: Fragment() {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
): View {
super.onCreateView(inflater, container, savedInstanceState)
_binding = FragmentFitnessHolderBinding.inflate(inflater, container, false)
return binding.root
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pennapps.labs.pennmobile

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down
Loading
Loading