-
-
Notifications
You must be signed in to change notification settings - Fork 74
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
Preferences API extensions #255
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be pretty useful, although I'm not sure if we want this as a separate module or in ktx-app
.
As for the get
operator, we could do it like this:
inline operator fun <reified T> Preferences.get(key: String): T =
when(T::class.java) {
String::class.java -> getString(key) as T
Boolean::class.java -> getBoolean(key) as T
// TODO Other types.
else -> throw GdxRuntimeException("Unsupported class: ${T::class.java}")
}
Although since Kotlin does not have union types, we cannot really limit T
to supported preference types, hence the runtime exception. Maybe we could default to JSON parsing for other types to ease (de)serialization of JSON preferences? A similar set
operator could be added as well.
// Read:
else -> Json().fromJson(T::class.java, getString(key))
// Write:
operator fun <T> Preferences.set(key: String, value: T): Preferences =
this.putString(key, Json().toJson(value))
I've thought about this and I think this could be added as a separate module. If there are tiny modules with pretty general purpose utilities like |
Made a quick update on the PR with reified set and get. Also thought of a way to maybe use the pair syntax but did not come up with a very nice way :D I also replaced the mock now with an implementation as you suggested. Will update Readme by end of this week. |
Overloads for |
…information to set and get
I have merged the develop branch now and resolved the CHANGELOG.md conflicts. Also, finished the documentation and everything else from my POV. Let me know if there is anything to change :) |
Thanks, I believe that's all as well. I'll do some minor refactoring and publish the module snapshot today. Thank you for your contribution! |
@Quillraven I ended up changing things a bit as I looked closely into the code and the tests:
You're welcome to review the changes. |
@Quillraven I think there were quite a few useful utilities added since the last release, and this module is a major contribution. We might publish a new stable version this week. Let me know if you think something is missing or if you want to set up any more pull requests soon. |
looks good to me and I like the way you tested the flush extension. I always learn something from your changes, thanks! Found a small typo but other than that it looks great. I have no plans for other PRs as I am very busy at work for the next couple of weeks haha. Looking forward to the new release - thank you a lot! |
Which file and line? |
51 in preferences.kt
I thought I added a comment but now I don't see it anymore. You wrote `amd`
instead of `and`.
|
@Quillraven Thanks, good catch. (Also, Intel probably hates me.) |
@Quillraven By the way, snapshot release should already be out. Assuming you're using the preferences extensions in a personal project, I'd appreciate it if you switched to KTX and tested the API - especially around the JSON serialization. I'll probably do the release during the weekend. |
will work on the private project over the weekend and will update to ktx preferences. If something comes up, I will let you know :) |
@Quillraven Just wanted to let you know that I added some extra utilities from the Cyberpunk framework (#183), including taking screenshots, moving camera and profiling. Snapshots with the changes should already be out. If you have some free time for testing, I'd appreciate it if you looked through the code and tried it out in your project. :) |
@czyzby I updated now to the snapshot, kotlin 1.3.71 and gradle 6.3 (to use the exclusiveContent functionality for the snapshot ;) ) Looked into the Cyberpunk extensions but I cannot use any of them, sorry:
This is just my personal opinion from my current project. I think in general they are useful extensions but at the moment I cannot use them, sorry. |
@Quillraven Thanks for the feedback, it's actually very helpful. By the way, I'm currently working on #182 (about time). I still have a bunch of tests to write and refactor, but the basic functionality is there. I should be done by tomorrow, unless there's some major bug that I'll have to fix. I've written some basic mock-up apps loading and using some assets, but if you're currently using |
sure just let me know. I am using assetmanager in the loading screen that actually loads all assets currently since it is still a small game and does not need load/unload mechanics. |
@Quillraven Just wanted to let you know that |
thank you! will update my project over the weekend :) |
Hi,
was working on preferences in my game and saw there is no nice kotlin extensions. I tried to come up with something but I was not sure how to solve
get
calls to avoidgetInt
,getString
, etc..Any ideas?
I will work on the documentation next week, if someone thinks this might be a useful addition. If not, feel free to delete this PR ;)