-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start adding retrofit 2. Also include rewrite for laundry.
- Loading branch information
Showing
9 changed files
with
740 additions
and
2 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
PennMobile/src/main/java/com/pennapps/labs/pennmobile/LaundryFragment2.kt
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
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() | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
PennMobile/src/main/java/com/pennapps/labs/pennmobile/LaundrySettingsFragment2.kt
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
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 | ||
} | ||
} |
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
Oops, something went wrong.