Deepti M
04/16/2020, 10:09 PMCody Engel
04/16/2020, 10:19 PMContext
instance for? I’ve found most project benefit from an interface StringProvider
which has an implementation that relies on the Context
. This can be easily added to a dagger dependency graph, but again it all depends on the use case.
Singleton isn’t great but there are likely plenty of apps out there today which do that and functions perfectly well.Deepti M
04/16/2020, 10:22 PMCody Engel
04/16/2020, 10:26 PMResourceProvider
another as PreferenceProvider
and a final one as StorageProvider
.Deepti M
04/16/2020, 10:27 PMCody Engel
04/16/2020, 10:27 PMDeepti M
04/16/2020, 10:30 PMCody Engel
04/16/2020, 10:31 PMDeepti M
04/16/2020, 10:32 PMCody Engel
04/16/2020, 10:33 PMstartKoin
function look like?Deepti M
04/16/2020, 10:33 PMstartKoin {
androidLogger()
androidContext(this@BaseApplication)
modules(allModules)
}
Cody Engel
04/16/2020, 10:34 PMDeepti M
04/16/2020, 10:36 PMandroidContext()
is available in all modulesCody Engel
04/16/2020, 10:37 PMDeepti M
04/16/2020, 10:38 PMfun getVideoUrl(context: Context): String? {
val isTab = context.resources.getBoolean(R.bool.isTab)
val isPortrait =
context.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT
var resolution = PreferenceHelper.readSelectedResolution(context)
if (resolution == CamcorderProfile.QUALITY_2160P) {
resolution =
if (TextUtils.isEmpty(video2kShortPortrait) ||
TextUtils.isEmpty(video2kShortLandscape)
) {
CamcorderProfile.QUALITY_1080P
} else {
return if (isTab && !isPortrait)
video2kShortLandscape
else
video2kShortPortrait
}
}
var videoType: String? =
context.applicationContext?.let {
DeviceHelper().getVideoType(it, resolution)
}
if (IDevice.SMALL_TABS == videoType && isPortrait) {
videoType = IDevice.SMALL_TABS_PORTRAIT
} else if (IDevice.LARGE_TABS == videoType && isPortrait) {
videoType = IDevice.LARGE_TABS_PORTRAIT
}
var videoUrl: String? =
getVideoByType(PreferenceHelper.readSelectedVideoQuality(context), videoType)
if (TextUtils.isEmpty(videoUrl)) {
videoUrl = getVideoByType(IDevice.QUALITY_HD, videoType)
}
return videoUrl
}
Cody Engel
04/16/2020, 10:44 PMVideoFactory
which is part of your Koin dependency graph. Then in places which need to create a Video
they could invoke VideoFactory
to create a new instance of it. I’m not sure what the exact parameters are, it could be setup something like this…
VideoFactory(preferenceProvider: PreferenceProvider, storageProvider: StorageProvider) {
fun create(/* specific properties for model /*): Video {
return Video(preferenceProvider, storageProvider, /* specific properties for model */)
}
}
Deepti M
04/16/2020, 10:45 PMCody Engel
04/16/2020, 10:47 PMDeepti M
04/16/2020, 10:47 PMCody Engel
04/16/2020, 10:49 PMDeepti M
04/16/2020, 10:53 PMPablo L.
04/16/2020, 11:57 PMvenomvendor
04/17/2020, 4:38 AMstartKoin {
androidLogger()
androidContext(this@BaseApplication) // <-- Making context glolbally available
modules(allModules)
}
fun getVideoUrl(): String? {
// Get context injected during initialization.
val context: Context = get()
}
Ahmed Ibrahim
04/17/2020, 8:22 AMCody Engel
04/17/2020, 1:25 PMStringProvider
which just provides strings, the other ones from that post are other interfaces you could extract as well.Ahmed Ibrahim
04/17/2020, 1:29 PMProcess.killProcess()
to destroy the application and to reload everything, then yeah application context would work, but if you just do`activity.recreate()` then you will need to use the activity context to load the newly configured strings.
At least that how it did work for me.Cody Engel
04/17/2020, 1:40 PMclass LocalizedActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(
TextView(baseContext)
.also { it.text = application.getString(R.string.hello) }
)
}
}
Ahmed Ibrahim
04/17/2020, 1:41 PMCody Engel
04/17/2020, 1:45 PMAhmed Ibrahim
04/17/2020, 1:47 PMResourceProvider
that has application context injected, the problem becomes when your application has a custom resources.configuration
than what's coming from the Phone settings. Then you'll need activity context to resolve strings correctly. Again that's how it did work for me, I might've been doing something wrong that made things didn't work with application context 🤷♂️Cody Engel
04/17/2020, 1:56 PMAhmed Ibrahim
04/17/2020, 2:00 PM