<@U2VU05RC4> I had a look at your multiplatform-se...
# touchlab-tools
d
@russhwolf I had a look at your multiplatform-settings library and it seems very interesting. I was browsing the sample you have in Github and I can see that you defined the settings in the commonMain, but you are instantiating it in the specific-platform app code. I am actually trying to do something a bit different. I have written a ViewModel (and a DataRepository) in CommonMain, while on the platform-specific app code I only keep the UI layer (JetpackCompose in Android and SwiftUI in iOS, then I am planning to use Vue.JS for Web). So in my case the DataRepository is fully on CommonMain. I would like to find a way to connect the MultiplatformSettings to the DataRepository. So I am not looking to instantiate MultiplatformSettings in each platform-specific app code, but just on the shared code, as any data coming from there it would populate the ViewModel, which is shared among all platforms. I wonder if MultiplatformSettings would work in this way too. Clearly, as the Settings Android constructor requires the applicationContext to be passed, I need to find a way to pass the context to the DataRepository where I want to instantiate Settings. As you understand, I am very new to the whole multiplatform thing. Do you have any sample that could explain how to do that in the shared code (in case it’s a feasible thing) ?
r
I tend to err on the side of platform-specific configuration. This is helpful for interop between your shared and platform code. For example if you use a specific
SharedPreferences
instance in your Android code, you can pass that to
Settings
and have the same source of truth in your common code. If you don't care about that for your use-case, there is a
multiplatform-settings-no-arg
module that adds a
Settings()
function to instantiate a default-configured
Settings
without needing to pass dependencies. You could also use any other way you might pass a
Context
around in a normal Android project. For example, that might be a dependency graph from Koin or Dagger or something similar, a manually configured Repository or DependencyManaager class that you construct from platform code, a ContentProvider that you use to store an Application context at app start (or the new
androidx.startup
which helps automate this), or something else.
d
Thanks!!!
multiplatform-settings-no-arg
sounds awesome. But is there any reason why you don’t provide a constructor to specify the name of the SharedPreferences/NSUserDefaults ?
r
Because there's no way to do the same for JS
d
it could maybe just be ignored for JS?
but I get your point
it would not be consistent
r
I guess I could do a second module that only targets platforms that are namable, but I'm not convinced the demand is enough that it's worth maintaining. Feel free to open an issue if it's something you care strongly about.
Since it's just a convenience, and you still always have the option of manually configuring each platform separately, it doesn't feel very high-priority to me until I hear otherwise from the community
d
if I configure each platform separately, I understand that I need to specify the Context on Android, which I prefer not to worry about.
multiplatform-settings-no-arg
is awesome to allow you not to pass the Context
but I was expecting maybe there would also be the flexibility to create different Settings objects with different names
but I guess in JS, this would create conflicts
as the two objects would point to the same resource
@russhwolf I would have another question, is there a maximum length for a String value? I was thinking about using Settings for caching the serialized json of Kotlin data objects.
By the way, I just imported:
implementation("com.russhwolf:multiplatform-settings-no-arg:0.6")
in CommonMain, but I get an error when I try to use Settings
message has been deleted
r
Add an import for
com.russhwolf.settings.Settings.Companion.invoke
re string length, the library imposes no cap but the platforms might have their own limitations.
d
message has been deleted
still doesn’t seem to like it
r
Maybe a gradle sync? Not sure what's going on without seeing more of the project
d
this is my gradle in shared
r
Does
./gradlew build
work from the command line?
d
I get this:
> Task :common:compileDebugKotlinAndroid FAILED
e: /myapp/common/repository/Repository.kt: (16, 38): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public operator fun <T, R> DeepRecursiveFunction<TypeVariable(T), TypeVariable(R)>.invoke(value: TypeVariable(T)): TypeVariable(R) defined in kotlin
r
Weird. Can you open an issue? Not sure what's going wrong and I don't have time to dig in right now. If you can link your entire project (or a minimal version that still fails to build) it would be helpful.
d
ok
I just opened the issue, attaching the sample app code
👍 1