Skip to content

Commit

Permalink
Start adding retrofit 2. Also include rewrite for laundry.
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Apr 14, 2024
1 parent 9b7cac6 commit fae8ddb
Show file tree
Hide file tree
Showing 9 changed files with 740 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.pennapps.labs.pennmobile

import StudentLife_rf2
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import androidx.preference.PreferenceManager
import android.view.*
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.pennapps.labs.pennmobile.adapters.LaundryRoomAdapter
import com.pennapps.labs.pennmobile.classes.LaundryRoom
import com.pennapps.labs.pennmobile.classes.LaundryUsage
import com.pennapps.labs.pennmobile.components.collapsingtoolbar.ToolbarBehavior
import com.pennapps.labs.pennmobile.databinding.FragmentLaundryBinding
import com.pennapps.labs.pennmobile.utils.Utils
import com.pennapps.labs.pennmobile.viewmodels.LaundryViewModel
import kotlinx.android.synthetic.main.loading_panel.*
import java.util.*

class LaundryFragment2 : Fragment() {
private lateinit var mActivity: MainActivity

private lateinit var mStudentLife: StudentLife_rf2
private lateinit var mContext: Context

private lateinit var sharedPreferences: SharedPreferences

// list of favorite laundry rooms
private var laundryRooms = ArrayList<LaundryRoom>()
// data for laundry room usage
private var roomsData: ArrayList<LaundryUsage> = ArrayList()

private var mAdapter: LaundryRoomAdapter? = null

private var _binding : FragmentLaundryBinding? = null
private val binding get() = _binding!!

private val laundryViewModel : LaundryViewModel by activityViewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

mStudentLife = MainActivity.studentLifeInstanceRf2
mActivity = activity as MainActivity
mContext = mActivity
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity)
}

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

initAppBar()

binding.favoriteLaundryList.layoutManager = LinearLayoutManager(mContext)
binding.laundryMachineRefresh.setOnRefreshListener {
updateMachines()
}
binding.laundryMachineRefresh.setColorSchemeResources(R.color.color_accent, R.color.color_primary)

updateMachines()
return view
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mActivity.removeTabs()
mActivity.setTitle(R.string.laundry)

mAdapter = LaundryRoomAdapter(mContext, laundryRooms, roomsData, false)
binding.favoriteLaundryList.adapter = mAdapter

loadingPanel?.visibility = View.VISIBLE

laundryViewModel.favoriteRooms.observe(viewLifecycleOwner) { favorites ->
binding.laundryMachineRefresh.isRefreshing = false

laundryRooms.clear()
roomsData.clear()

laundryRooms.addAll(favorites.favoriteRooms)
roomsData.addAll(favorites.roomsData)

for (pos in 0 until LaundryViewModel.maxNumRooms) {
mAdapter!!.notifyItemChanged(pos)
}

loadingPanel?.visibility = View.GONE
binding.laundryHelpText.visibility = View.INVISIBLE
}

}

private fun getOnline() : Boolean {
//displays banner if not connected
if (!isOnline(context)) {
binding.internetConnectionLaundry.setBackgroundColor(resources.getColor(R.color.darkRedBackground))
binding.internetConnectionMessageLaundry.text = getString(R.string.internet_error)
binding.internetConnectionLaundry.visibility = View.VISIBLE
binding.laundryHelpText.visibility = View.INVISIBLE
binding.laundryMachineRefresh.isRefreshing = false
loadingPanel?.visibility = View.GONE
return false
}

binding.internetConnectionLaundry.visibility = View.GONE
return true
}
private fun updateMachines() {
if (!getOnline()) {
return
}
mActivity.mNetworkManager.getAccessToken {
val bearerToken = "Bearer " + sharedPreferences
.getString(getString(R.string.access_token), "").toString()
laundryViewModel.getFavorites(mStudentLife, bearerToken)
}
}

private fun initAppBar() {
(binding.appbarHome.layoutParams as CoordinatorLayout.LayoutParams).behavior = ToolbarBehavior()
binding.titleView.text = getString(R.string.laundry)
binding.dateView.text = Utils.getCurrentSystemTime()
binding.laundryPreferences.setOnClickListener {
val fragmentManager = mActivity.supportFragmentManager
fragmentManager.beginTransaction()
.replace(R.id.content_frame, LaundrySettingsFragment2())
.addToBackStack("Laundry Settings Fragment")
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.pennapps.labs.pennmobile

import StudentLife_rf2
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.preference.PreferenceManager
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.pennapps.labs.pennmobile.adapters.LaundrySettingsAdapter2
import com.pennapps.labs.pennmobile.databinding.FragmentLaundrySettingsBinding
import com.pennapps.labs.pennmobile.viewmodels.LaundryViewModel
import kotlinx.android.synthetic.main.include_main.*
import kotlinx.android.synthetic.main.loading_panel.*
import kotlinx.android.synthetic.main.no_results.*

class LaundrySettingsFragment2 : Fragment() {

private lateinit var mActivity: MainActivity
private lateinit var mStudentLife: StudentLife_rf2
private lateinit var mContext: Context

private var _binding : FragmentLaundrySettingsBinding? = null
private val binding get() = _binding!!

private val laundryViewModel : LaundryViewModel by activityViewModels()
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mStudentLife = MainActivity.studentLifeInstanceRf2
mActivity = activity as MainActivity

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity)

mContext = mActivity
mActivity.closeKeyboard()
mActivity.toolbar.visibility = View.VISIBLE
mActivity.hideBottomBar()
}

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

// set up back button
mActivity.supportActionBar?.setDisplayHomeAsUpEnabled(true)

return view
}

private fun attachAdapter() {
val mAdapter = LaundrySettingsAdapter2(mContext, laundryViewModel)
try {
binding.laundryBuildingExpandableList.setAdapter(mAdapter)
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loadingPanel?.visibility = View.VISIBLE

// if this value is already true, then simply attach adapter
if (laundryViewModel.loadedRooms.value!!) {
attachAdapter()
loadingPanel?.visibility = View.GONE
no_results?.visibility = View.GONE
} else {
// otherwise, wait until the network request is done
laundryViewModel.loadedRooms.observe(viewLifecycleOwner) { loaded ->
if (loaded) {
attachAdapter()
loadingPanel?.visibility = View.GONE
no_results?.visibility = View.GONE
}
}
laundryViewModel.getHalls(mStudentLife)
}
}

override fun onResume() {
super.onResume()
mActivity.removeTabs()
mActivity.setTitle(R.string.laundry)
mActivity.setSelectedTab(MainActivity.LAUNDRY)
}

override fun onDestroyView() {
super.onDestroyView()
if (laundryViewModel.existsDiff()) {
mActivity.mNetworkManager.getAccessToken {
val bearerToken = "Bearer " + sharedPreferences
.getString(getString(R.string.access_token), "").toString()
laundryViewModel.setFavoritesFromToggled(mStudentLife, bearerToken)
}
}
mActivity.supportActionBar?.setDisplayHomeAsUpEnabled(false)
mActivity.toolbar.visibility = View.GONE
_binding = null
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pennapps.labs.pennmobile

import StudentLife_rf2
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageManager
Expand Down Expand Up @@ -52,7 +53,11 @@ import retrofit.android.AndroidLog
import retrofit.client.OkClient
import retrofit.converter.GsonConverter
import java.util.concurrent.TimeUnit

import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import okhttp3.OkHttpClient as OkHttpClient1

class MainActivity : AppCompatActivity() {
private var tabShowed = false
Expand Down Expand Up @@ -270,6 +275,7 @@ class MainActivity : AppCompatActivity() {
val GSR_ID = R.id.nav_gsr

private var mStudentLife: StudentLife? = null
private var mStudentLife_rf2: StudentLife_rf2? = null
private var mPlatform: Platform? = null
private var mCampusExpress: CampusExpress? = null

Expand Down Expand Up @@ -307,6 +313,29 @@ class MainActivity : AppCompatActivity() {
return mPlatform!!
}

val studentLifeInstanceRf2: StudentLife_rf2
get() {
if (mStudentLife_rf2 == null) {
val okHttpClient = OkHttpClient1.Builder()
.connectTimeout(35, TimeUnit.SECONDS)
.readTimeout(35, TimeUnit.SECONDS)
.writeTimeout(35, TimeUnit.SECONDS)
.build()

val retrofit = Retrofit.Builder()
.baseUrl("https://pennmobile.org/api/")
.client(okHttpClient)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()

mStudentLife_rf2 = retrofit.create(StudentLife_rf2::class.java)
}
return mStudentLife_rf2!!
}


@JvmStatic
val studentLifeInstance: StudentLife
get() {
Expand Down
Loading

0 comments on commit fae8ddb

Please sign in to comment.