Skip to content

Commit

Permalink
fix: ignore friends timetable cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankbasant committed Jan 28, 2024
1 parent 518a2fa commit ee612c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/dscvit/vitty/adapter/DayAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import com.dscvit.vitty.ui.schedule.DayFragment
import timber.log.Timber

class DayAdapter(fa: Fragment, private val username: String?) : FragmentStateAdapter(fa) {
class DayAdapter(fa: Fragment, private val username: String?, private val isFriendsTimetable: Boolean = false) : FragmentStateAdapter(fa) {
private val numPages = 7

override fun getItemCount(): Int = numPages
Expand All @@ -15,6 +15,7 @@ class DayAdapter(fa: Fragment, private val username: String?) : FragmentStateAda
val bundle = Bundle()
bundle.putString("frag_id", position.toString())
bundle.putString("username", username)
bundle.putBoolean("isFriendsTimetable", isFriendsTimetable)
val fragment = DayFragment()
fragment.arguments = bundle
return fragment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class FriendFragment : Fragment() {
else -> 0
}

val pagerAdapter = DayAdapter(this, username)
val pagerAdapter = DayAdapter(this, username, true)
binding.pager.adapter = pagerAdapter
TabLayoutMediator(
binding.tabs, binding.pager
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/java/com/dscvit/vitty/ui/schedule/DayFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DayFragment : Fragment() {
listOf("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")
lateinit var day: String
private lateinit var scheduleViewModel: ScheduleViewModel
private var isFriendsTimetable = false


override fun onCreateView(
Expand All @@ -63,6 +64,7 @@ class DayFragment : Fragment() {
Constants.COMMUNITY_USERNAME,
null
) ?: ""
isFriendsTimetable = requireArguments().getBoolean("isFriendsTimetable")
Timber.d("token $token username $username")
Timber.d(
"pref username is ${
Expand Down Expand Up @@ -90,26 +92,30 @@ class DayFragment : Fragment() {
).toString() else days[fragID]

val cachedData = sharedPref.getString(Constants.CACHE_COMMUNITY_TIMETABLE, null)
if (cachedData != null) {
if (cachedData != null && !isFriendsTimetable) {
// If cached data is available, load from cache
Timber.d("Loading from cache")
Timber.d("$cachedData")
val response = Gson().fromJson(cachedData, UserResponse::class.java)
// Toast.makeText(context, "Loaded from cache", Toast.LENGTH_SHORT).show()
processTimetableData(response)
UtilFunctions.reloadWidgets(requireContext())

}

UtilFunctions.reloadWidgets(requireContext())

scheduleViewModel.user.observe(viewLifecycleOwner) {
if (it != null) {
//cache response for widget
val response = Gson().toJson(it)
val editor = sharedPref.edit()
editor.putString(Constants.CACHE_COMMUNITY_TIMETABLE, response)
editor.apply()
val cachedData = sharedPref.getString(Constants.CACHE_COMMUNITY_TIMETABLE, null)
Timber.d("cached data is $cachedData")
if(!isFriendsTimetable) {
val response = Gson().toJson(it)
val editor = sharedPref.edit()
editor.putString(Constants.CACHE_COMMUNITY_TIMETABLE, response)
editor.apply()
val cachedData = sharedPref.getString(Constants.CACHE_COMMUNITY_TIMETABLE, null)
Timber.d("cached data is $cachedData")
}

// Toast.makeText(context, "Updated Timetable from internet.", Toast.LENGTH_SHORT).show()


Expand Down

0 comments on commit ee612c5

Please sign in to comment.