-
Notifications
You must be signed in to change notification settings - Fork 437
Update providing of SharedPreferences #2054
Update providing of SharedPreferences #2054
Conversation
a7db88b
to
5661113
Compare
@imsodin could you check this PR again? |
I don't understand the example: That's the same setting used in five different places - starting on boot is not something that can differ in separate activities. You inherently can't modify that between screens. I assume what you really look for is something like having separate preference categories for maybe folders, devices, app runtime, ...? Generally I very much like iterative, small PRs - thanks a lot for that. However please do show me (drafts) of future improvements building on changes/PRs like this. Unfortunately I need to weight the potential impact/improvement a change brings with the effort to review it, as I have very limited time for maintaining this app. I wont review/accept refactors that don't bring an improvement on their own. I assume you do have some tangible improvement planned/in the works already? |
It can be done unfortunately. SharedPreferences offer no type safety, thus another screen could save an integer instead of a boolean with the same key name or a wrong key could be used to save the data that should end up under another name. This may never happen as the app is still small but there are multiple keys used in multiple parts of the application, thus the possibility of a mistake only increases. Currently the logic for reading/writing the value is duplicated in each location where a particular key is used.
For the This PR makes sure that that there is only one way to provide the default SharedPreferences instance that is injected in other classes. The next step is going to be that of making sure there is only one place that provides the default SharedPreferences in the entire app: the dagger A future step would introduce the Repository pattern and replace the direct usage of SharedPreferences in favor of calling a method to get/set a value while hiding the intricacies of how that value is read/stored/cached. Note:The |
Cool, that end result I understand and agree with.
I guess that refers to moving the dagger providers into a separate module? The
That also makes sense, I just don't get why the default preferences introduced in this PR are relevant for that. Nor for the matter moving dagger providers to a separate module. You could just replace those usages with an injected preferences member and you'd achieve the same. And a step further I don't get how that helps towards reaching the repo pattern: It doesn't make that step smaller, both from the current state or from injected preferences, you'll replace the same preferences with the repo pattern. |
This is because the methods that provide dependencies in Dagger modules cannot be private.
I think introducing the repo pattern right away would be too big of a jump at the moment. |
I really do get (and agree with) that goal. What I don't see is how adding |
Agreed 👍 |
Description
Refactor a bit the way the default preferences are provided by making it more explicit they are the default ones. There are only the default ones at the moment but it may change in the future.
In the next step I'd also want to make the
SyncthingModule
the single source of these SharedPreferences.At the moment there are 19 places where
PreferenceManager.getDefaultSharedPreferences(...)
is invoked.There should be only one source of it, the SyncthingModule, and there should be only one way of getting these preferences: injecting them where needed.
Changes
DefaultSharedPreferences
, to distinguish which preferences are provided/injectedDefaultSharedPreferences
Why differentiate between different SharedPrefernces?
![image](https://private-user-images.githubusercontent.com/6529875/302758659-0c3ab65d-f825-4997-90b0-d359d519002b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5NTAyMzksIm5iZiI6MTczODk0OTkzOSwicGF0aCI6Ii82NTI5ODc1LzMwMjc1ODY1OS0wYzNhYjY1ZC1mODI1LTQ5OTctOTBiMC1kMzU5ZDUxOTAwMmIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDdUMTczODU5WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MTczNWEzYzU3YzM5YmVmM2Q1MTY5NjkwMDYwMTMxMWY2YTZjNjJlYzhlZmQ3NmE5MmRlYmMwOGVhYmJkZTNlMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.sTAJ2CXQWbv-nNeruWIfcodn_HMWP6NmGZtTAuCzDQY)
For additional clarity.
It may not be needed now, as everything is stored inside a single instance of SharedPrefences, the default one, but we may want to change that in the future.
Ideally each feature screen that needs to store small bits of data should be able to do so in its own dedicated SharedPreferences instance (also dedicated xml file on the device) that is independent from other features.
This also helps to decouple features as it's always painful to decouple screens that are tightly coupled because of same storage.
Example:
The same key is used in five different places. Trying to modify anything related to that key in one screen may break unexpectedly functionalities in other screens.
This is fine for now, but we should strive to avoid this kind of accidental complexity in the future in my opinion.