Easy SharedPreferences with Delegated Properties in Kotlin

Поділитися
Вставка
  • Опубліковано 8 січ 2025

КОМЕНТАРІ • 70

  • @typealias
    @typealias  8 місяців тому +5

    Jetpack DataStore is Google's newer recommended solution for key-value storage. If you're developing a new Android app, you should consider using it instead of SharedPreferences: developer.android.com/topic/libraries/architecture/datastore

  • @akashkhatkale1149
    @akashkhatkale1149 2 роки тому +25

    So much of in depth industry level knowledge in one video 🙌🏻 please continue uploading such videos

    • @typealias
      @typealias  2 роки тому +2

      Thanks so much, Akash! I'll keep at it! 🙂

    • @akashkhatkale1149
      @akashkhatkale1149 2 роки тому +4

      UA-cam lacks advance android knowledge like the one shown in your video. All the best :)

  • @jessehill4658
    @jessehill4658 2 роки тому +8

    This was a great way to see delegates in action and also an interesting example of working with generics + reified. Thanks for putting it together!

    • @typealias
      @typealias  2 роки тому +1

      You're most welcome, Jesse! I'm glad you liked it!

  • @shubhamnanche
    @shubhamnanche 4 місяці тому +1

    You are doing a great job, Dave! Bravo!

  • @vengateshm2122
    @vengateshm2122 2 роки тому +2

    Thanks for the detailed explanation of different approaches. Expecting more videos from you on Kotlin programming.

    • @typealias
      @typealias  2 роки тому +1

      You're most welcome! And yes, more videos coming!

  • @wagnerarcieri
    @wagnerarcieri 2 роки тому +1

    best content on kotlin I've ever found, congrats! Please more!

    • @typealias
      @typealias  2 роки тому +1

      Wow, thanks so much for the kind words!

  • @behnamahmadi2834
    @behnamahmadi2834 9 місяців тому +1

    great Explanation, just found your channel and subscribed immediately.

    • @typealias
      @typealias  9 місяців тому

      Hey, that's fantastic! Thank you so much!

  • @Carlos-xz9zq
    @Carlos-xz9zq 2 роки тому +2

    I really liked the type video, nice work, going to check more!

    • @typealias
      @typealias  2 роки тому

      Thanks Carlos! I've only created a handful of videos so far, but I'm glad to hear that you enjoyed this one!

  • @Namesake..
    @Namesake.. 2 місяці тому

    Great video. Thanks for this! It would be interesting to see how you would unit test this code.

  • @AndreiBacalu
    @AndreiBacalu Рік тому

    somehow youtube recommended me this video which is awesome. i'm looking forward to see more videos from you, but in the meantime you got me as a subscriber.

    • @typealias
      @typealias  Рік тому

      Sounds great, Andrei! Thanks for subscribing! 🙂

  • @jackli1924
    @jackli1924 Рік тому

    amazing video! You explained it concise and step by step, from easy to hard. Thanks a lot bro you inspired me a lot

  • @PeteC62
    @PeteC62 7 місяців тому

    Nice video! When i have to do a comparison of the sort you did between the Boolean and Float delegates, I'll copy one version into the clipboard, select the other version, and then use Compare Clipboard to Selection (I think thats the name, I'm on my phone away from my laptop!)

    • @typealias
      @typealias  7 місяців тому

      Thanks Pete! Yeah, that's a handy feature! I love using it when I'm developing but it didn't occur to me to use it for the video! 😅

  • @h4m74ro
    @h4m74ro 2 роки тому +1

    This is really great content! I already learned so much. I wish you all the best and hope you will make more videos 🙂

    • @typealias
      @typealias  2 роки тому

      Thank you so much. I'm glad to hear! And yes, more videos will be coming!

  • @aiklklivlind9451
    @aiklklivlind9451 2 роки тому +1

    Very interesting and good presentation of the material. Keep up the good work. Good luck! subscription

    • @typealias
      @typealias  2 роки тому +1

      Many thanks, Мaikl!

  • @waelalghamdi5298
    @waelalghamdi5298 2 роки тому +2

    As usual, a great video 👍

    • @typealias
      @typealias  2 роки тому

      Thanks so much! Glad you enjoyed it! 🙂

  • @derekdevs
    @derekdevs Рік тому

    This is incredible. Thank you!

    • @typealias
      @typealias  Рік тому +1

      You're most welcome, Derek! Thanks for your kind words!

  • @aungkhanthtoo7678
    @aungkhanthtoo7678 9 місяців тому

    So insightful!

  • @mdisi5967
    @mdisi5967 2 роки тому

    That was super informative, Thank you!

    • @typealias
      @typealias  2 роки тому

      You're most welcome! Glad you enjoyed it!

  • @skytech2501
    @skytech2501 2 роки тому +1

    One of the best explainer videos! something like this for a Sqlite Database !?

    • @typealias
      @typealias  2 роки тому +1

      Thanks Sky Tech! Just to clarify - are you asking about using delegated properties with SQLite, or are you just asking for an explainer video about SQLite in general?

  • @rajeshpattanaik6597
    @rajeshpattanaik6597 Рік тому

    Love your videos ❤

    • @typealias
      @typealias  Рік тому

      Thanks so much, Rajesh! 🙂

  • @BewareOfStinger
    @BewareOfStinger 7 місяців тому

    Amazing video as always. I was wondering if there is something similar that is not specific to Android?

    • @typealias
      @typealias  7 місяців тому

      I've heard of Multiplatform Settings (github.com/russhwolf/multiplatform-settings), but I don't have any experience with it myself. It supports Android, iOS, macOS, watchOS, tvOS, JavaScript, JVM, and Windows - and looks like its API supports delegates. Might be worth checking out!

  • @MuhammadAwais-hc6ry
    @MuhammadAwais-hc6ry 5 місяців тому

    Just awesome 🎉

    • @typealias
      @typealias  5 місяців тому +1

      Hey, thanks so much, Muhammad! I appreciate that!

  • @etherxi
    @etherxi Рік тому

    Really nice and helpful idea

    • @typealias
      @typealias  Рік тому

      Thanks so much! I'm glad you like it!

  • @rohitjakhar6672
    @rohitjakhar6672 Рік тому

    Great video, please make more video

    • @typealias
      @typealias  Рік тому

      Thanks so much! More videos are on the way!

  • @cal1314
    @cal1314 8 місяців тому

    For the overloading of getters problem, would @JvmName be an appropriate solution?

    • @typealias
      @typealias  8 місяців тому

      That can work in certain cases, like when you've got a generic with a type argument as a function parameter (or as a receiver on an extension function). In the example in this video, there just aren't any parameters that can be used to distinguish between the functions. In other words, the clash isn't just for the JVM platform; it's a clash in Kotlin, regardless of the platform you're targeting. It was a good thought, though!

  • @deepakshah3108
    @deepakshah3108 Рік тому

    Could you make videos on Difference between videos like -> Extension fun vs Delegates

    • @typealias
      @typealias  Рік тому

      Hey, Deepak, thanks for the suggestion! Just to clarify, are you looking for videos that explain the trade-offs between different design decisions? (e.g., "when should I use an extension function, and when should I use a delegate?")

    • @deepakshah3108
      @deepakshah3108 Рік тому

      @@typealias Yes, it will help us to get deeper knowledge(may be for interview purpose)

    • @typealias
      @typealias  Рік тому

      Great, thanks again! 👍

  • @lg.studio
    @lg.studio 11 місяців тому

    Thanks for the information. How can I make my list automatically save to the shared preferences again when an item is removed or added to it? I have been struggling to find a solution for this problem. Thanks.

    • @typealias
      @typealias  11 місяців тому

      Hey, LG, thanks for asking! Is this a list of strings, where you're saving the whole list to SharedPreferences (e.g., using putStringSet()), or are you trying to save individual items of the list as individual values, or something else...? Creating your own implementation of `MutableList` is a likely option, but it might help to see some example code to understand your particular situation better.

    • @lg.studio
      @lg.studio 11 місяців тому

      @@typealias I would like to store a list of strings/ids (10-12 character long ones). dynamically add and remove from the list, search in the list for matching ID, and want them to be stored in the shared prefs all the time, as it is quite an important data I need during runtime. I was able to create the one you have, but this way I have to create and copy all existing items into a new list and assign it to my variable.

    • @typealias
      @typealias  11 місяців тому +1

      Okay, thanks. You could consider extending AbstractMutableSet with your own class, e.g., named SharedPreferencesSet. Its constructor could accept an instance of SharedPreferences and the key. It would also hold a private instance of an underlying mutable set. Then, the modification functions (e.g., add/remove/etc.) would be overridden to call the same function on the private instance _and also_ save the set to shared preferences. Would definitely need some good testing to make sure it works as expected.
      Hopefully the description above is enough to give you the idea, but if you need more info, just send me a direct message on X, LinkedIn, or Kotlin Slack.

  • @lglf77
    @lglf77 2 роки тому

    make one video with sharefPreferences in fragment without to user Activity.

  • @safionweb
    @safionweb 2 роки тому

    Amazing!

  • @ztao3907
    @ztao3907 10 місяців тому +1

    nice!!!

  • @mohamednizam5298
    @mohamednizam5298 2 роки тому

    Forgive me are u somehow related to sheldon cooper? P/s great vid thanks

    • @typealias
      @typealias  2 роки тому

      Haha, I'm tall and slender, but not nearly smart enough to be related to Sheldon! Glad you enjoyed the video!

  • @scottbiggs8894
    @scottbiggs8894 7 місяців тому

    Excellent explanation of delegates. However this is not really "easy" once you get past the initial version. SharedPrefs are quite easy to use--I fail to see the need for all this complicated code to do something that already works well and is straight-forward.

  • @roman3249
    @roman3249 2 роки тому

    🔥

  • @TerryQuiet
    @TerryQuiet Рік тому

    whoa

  • @igorshaula9097
    @igorshaula9097 7 місяців тому

    a good example of delegation in the very beginning of this video!
    but what goes after - is a sample of over-engineering, to my mind. I'm sorry for that, but this wrapping of SP leads only to useless complications of code with no real benefits.
    again - thanks for the video, this one, and for the content on this channel! you give really great lessons about Kotlin mastery! the line above concerns only the given approach to SP.

    • @typealias
      @typealias  7 місяців тому +1

      Hey Igor, I'm glad you've been enjoying the videos! I can certainly respect your conclusion that the first, simpler approach is adequate. It's fun to explore the possibilities, and getting to full type-safety is a nice idea, but not always worth the cost at the usage site. Got diminishing returns trying to get the last little bit of type safety on this one, for sure!

  • @dmytroberezhnyi717
    @dmytroberezhnyi717 2 роки тому +1

    Man, you are a wizard! How to do that? ua-cam.com/video/KFgb6l1PUJI/v-deo.html

    • @typealias
      @typealias  2 роки тому +3

      Thanks Dmytro! I was using the "Add Selection for Next Occurrence" keyboard shortcut for multiple cursors. Here's the documentation about it: www.jetbrains.com/help/idea/multicursor.html?#multiple_words

  • @Adam0001
    @Adam0001 2 роки тому

    That's amazing
    Thank you
    Can we use delegation with Flow? (Kotlin Coroutines)
    Instead of return Flow we return the value
    Like the way collectAsState() works with in JetpackCompose
    I hope you understood me
    Keep going 👏

    • @typealias
      @typealias  2 роки тому +2

      Hey, thanks Adam! If I understand right, you're looking for a way to use a delegated property to connect a Flow so that it always gives you the latest value when you evaluate it. If that's what you're looking for, I slapped together an example of that here: gist.github.com/djleeds/716f3acbd94b8e4367ab66ba403778d7
      Keep in mind that I only have limited experience with Flow so far... so someone else might be able to comment on a better way to achieve this... or maybe the coroutines core library might even give us something similar to this out of the box... ?

    • @ahmedel-hady3249
      @ahmedel-hady3249 10 місяців тому

      if I am not mistaken Mutablestateflow.collectAsState() does that