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

Cached times aren't working, even in previous releases #12

Closed
KiARC opened this issue Mar 25, 2022 · 8 comments
Closed

Cached times aren't working, even in previous releases #12

KiARC opened this issue Mar 25, 2022 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@KiARC
Copy link
Owner

KiARC commented Mar 25, 2022

I am 100% sure they worked before but now on every release on multiple devices the cached times don't reset correctly

Expected: Reset time, close and open app, time is relatively small because of the recent reset
Actual: Reset time, close and open app, time is back where it was

I am genuinely baffled.

@KiARC KiARC added the bug Something isn't working label Mar 25, 2022
@KiARC KiARC self-assigned this Mar 25, 2022
@KiARC
Copy link
Owner Author

KiARC commented Mar 25, 2022

Honestly what even could cause this? A spontaneous bug affecting all versions that was definitely not there before just isn't possible.

@mnowak98
Copy link
Contributor

mnowak98 commented Mar 25, 2022

The problem seems to be in line 129 in Main.kt:

date = Instant.now()

This creates a new Instant with the correct current date. date then points to this new Instant, but the Instant in the HashMap is never updated. This date reference is also used to update the displayed text, which is why everything appears to be working as expected.

In the previous versions this could be fixed, by updating addictions[name] directly, instead of the local variable date. In the current version, where addiction is a HashMap of a String and a Pair, the equivalent would be updating it via

addictions[key].first = Instant.now()

however Pair is immutable, thats why my current workaround is constructing a new Pair using the updated date and the old Buffer like so:

 addictions[key] = Pair(Instant.now(), addictions[key]!!.second)

Im sure there is a cleaner way to do it but this seems to work

@KiARC
Copy link
Owner Author

KiARC commented Mar 25, 2022

Ahhhh that makes sense. I might write a really simple Pair implementation that is mutable to make the fix more readable. Thanks for explaining that haha

@mnowak98
Copy link
Contributor

mnowak98 commented Mar 25, 2022

I was confused about this as well at first😄 This stackoverflow answer explains it very nice in detail. Just like Java, Kotlin is pass-by-value, e.g. it passes a copy of a reference to a method, so reassigning this copy to a new object does not change the original.

@mnowak98
Copy link
Contributor

I might write a really simple Pair implementation that is mutable

Maybe its a better idea, to just create a serializable class "Addiction", with fields name, date, buffer etc. and save that as an ArrayList, then createNewCard could take an Addiction object as input, and simply modify the fields of this object.

This would also make it easier to add new fields later on, (e.g. total money / time saved?), because you wouldnt need to continue stacking Pairs into Pairs

@KiARC
Copy link
Owner Author

KiARC commented Mar 25, 2022

Ahaha I feel so stupid now. I wrote it the way I did at first because I thought Kotlin was PBR but thanks for explaining that.

@KiARC
Copy link
Owner Author

KiARC commented Mar 25, 2022

And yeah that's a good idea

@KiARC
Copy link
Owner Author

KiARC commented Mar 25, 2022

Closing now because a solution has been found, thanks so much ^^

@KiARC KiARC closed this as completed Mar 25, 2022
@KiARC KiARC mentioned this issue Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants