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

Fixed TimerTask Object #545

Merged
merged 4 commits into from
Feb 24, 2024
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
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {
applicationId "org.hackillinois.android"
minSdkVersion 23
targetSdkVersion 33
versionCode 60
versionName "2024.2.2"
versionCode 61
versionName "2024.3.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/hackillinois/android/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ interface API {

// SHOP

@GET("shop/")
@GET("shop/v2/")
suspend fun shop(): List<ShopItem>

@POST("shop/item/buy/")
Expand Down Expand Up @@ -92,7 +92,7 @@ interface API {
@PUT("user/unfollow/")
fun unfollowEvent(@Body eventId: EventId): Call<FavoritesResponse>

@GET("user/qr/")
@GET("user/v2-qr/")
suspend fun qrCode(): QR

@PUT("user/scan-event/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ShopRepository {
GlobalScope.launch(Dispatchers.IO) {
try {
val shop = App.getAPI().shop()
Log.d("SHOP REFRESH ALL", shop.toString())
shopDao.clearTableAndInsertShopItems(shop)
} catch (e: Exception) {
Log.e("SHOP REFRESH ALL", e.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ class ProfileFragment : Fragment() {
private var pro = false
private var userRoles: Roles? = null

override fun onPause() {
super.onPause()
if (isAttendee()) {
profileViewModel.stopTimer()
}
}

override fun onResume() {
super.onResume()
if (isAttendee()) {
profileViewModel.startTimer()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -65,13 +79,13 @@ class ProfileFragment : Fragment() {
// set up LiveData observers
profileViewModel.currentProfileLiveData.observe(
this@ProfileFragment,
Observer { updateProfileUI(it) },
Observer { updateProfileUI(it) }
)
profileViewModel.qr.observe(
this@ProfileFragment,
Observer {
updateQrView(it)
},
}
)
profileViewModel.roles.observe(
this@ProfileFragment,
Expand All @@ -81,13 +95,13 @@ class ProfileFragment : Fragment() {
pro = it.isPro()
updateProTag()
}
},
}
)
profileViewModel.ranking.observe(
this@ProfileFragment,
Observer {
updateRanking(it)
},
}
)

// do view creation here if attendee
Expand Down Expand Up @@ -175,4 +189,10 @@ class ProfileFragment : Fragment() {
val prefString = context.getString(R.string.authorization_pref_file_key)
return context.getSharedPreferences(prefString, Context.MODE_PRIVATE).getString("provider", "") ?: "" == "google"
}

private fun isAttendee(): Boolean {
val context = requireActivity().applicationContext
val prefString = context.getString(R.string.authorization_pref_file_key)
return context.getSharedPreferences(prefString, Context.MODE_PRIVATE).getString("provider", "") ?: "" == "github"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ class ShopFragment : Fragment() {
// Merch tab is default selected
private var showingMerch: Boolean = true

override fun onPause() {
super.onPause()
shopViewModel.stopTimer()
}

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

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProfileViewModel : ViewModel() {
var ranking: MutableLiveData<Ranking> = MutableLiveData()
lateinit var roles: LiveData<Roles>
lateinit var timerObj: Timer
private var isTimerRunning = false

fun init() {
this.roles = rolesRepository.fetch()
Expand All @@ -32,21 +33,36 @@ class ProfileViewModel : ViewModel() {
currentProfileLiveData = profileRepository.fetchProfile()
// Initial qr code fetching
qr = qrRepository.fetch()
startTimer()
}

fun startTimer() {
// should refresh QR code every 15 seconds using Timer() class
timerObj = Timer()
val timerTaskObj: TimerTask = object : TimerTask() {
override fun run() {
qr = qrRepository.fetch()
if (!isTimerRunning) {
timerObj = Timer()
val timerTaskObj: TimerTask = object : TimerTask() {
override fun run() {
Log.d("QR FETCH", "...")
qr = qrRepository.fetch()
}
}
// Runs TimerTask every 15 seconds, with a 0 second delay upon the call of init().
timerObj.scheduleAtFixedRate(timerTaskObj, 0, 15000)
isTimerRunning = true
}
}

fun stopTimer() {
if (isTimerRunning && ::timerObj.isInitialized) {
timerObj.cancel()
isTimerRunning = false
}
// Runs TimerTask every 15 seconds, with a 0 second delay upon the call of init().
timerObj.scheduleAtFixedRate(timerTaskObj, 0, 15000)
}

override fun onCleared() {
super.onCleared()
timerObj.cancel()
Log.d("OnCleared", "QR")
stopTimer()
}

private fun fetchRanking() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,46 @@ class ShopViewModel : ViewModel() {
lateinit var profileLiveData: LiveData<Profile>

lateinit var timerObj: Timer
private var isTimerRunning = false
private var isAttendee = true

fun init(isAttendee: Boolean) {
fun init(inIsAttendee: Boolean) {
// initial fetch
shopLiveData = shopRepository.fetchShop()
isAttendee = inIsAttendee
if (isAttendee) {
profileLiveData = profileRepository.fetchProfile()
}

startTimer()
}

fun startTimer() {
// runs TimerTask every 10 seconds to fetch profile and shop data
timerObj = Timer()
val timerTaskObj: TimerTask = object : TimerTask() {
override fun run() {
shopLiveData = shopRepository.fetchShop()
if (isAttendee) {
profileLiveData = profileRepository.fetchProfile()
if (!isTimerRunning) {
timerObj = Timer()
val timerTaskObj: TimerTask = object : TimerTask() {
override fun run() {
shopLiveData = shopRepository.fetchShop()
if (isAttendee) {
profileLiveData = profileRepository.fetchProfile()
}
}
}
timerObj.scheduleAtFixedRate(timerTaskObj, 0, 10000)
isTimerRunning = true
}
}

fun stopTimer() {
if (isTimerRunning && ::timerObj.isInitialized) {
timerObj.cancel()
isTimerRunning = false
}
timerObj.scheduleAtFixedRate(timerTaskObj, 0, 10000)
}

override fun onCleared() {
super.onCleared()
timerObj.cancel()
stopTimer()
}
}
Loading