Vaibhav Jaiswal
08/06/2024, 1:40 PMinterface Walk {
fun walk()
}
class CatWalk : Walk {
override fun walk(){
println("Cat is walking")
}
}
class Cat : Walk by CatWalk() {
override fun walk(){
println("Cat is running")
}
}
fun main() {
val cat = Cat()
cat.walk()
}
This works fine as all functions are implemented by CatWalk
but I want Walk interface to have some function lets say eat()
which i dont want CatWalk
class to implement but let the class Cat
provide its implementation, how can i do so?
I tried making CatWalk
an abstract class and let Cat
provide the implementation but it did not work that way
Is there any way to achieve such behaviour?CLOVIS
08/06/2024, 1:58 PMinterface Walk {
fun walk()
fun eat()
}
interface CatWalk : Walk {
override fun walk() {
println("Cat is walking")
}
}
class Cat : Walk, CatWalk
// ^ doesn't compile, 'eat' isn't implemented
CLOVIS
08/06/2024, 1:59 PMVaibhav Jaiswal
08/06/2024, 2:04 PMinterface Walk {
fun walk()
fun eat()
}
class CatWalk : Walk {
override fun walk(){
//how can i do this?
showSnackBar("Cat is walking")
}
override fun eat() {
showToast("Eat")
}
}
class BaseCat {
fun showSnackBar(message: String) {
}
fun showToast(message: String) {
}
}
class Cat : BaseCat, Walk by CatWalk() {
}
How can i achieve this, I cannot pass the Cat
instance in CatWalk
constructor, it gives me error
I cant have abstract fun in CatWalk
Vaibhav Jaiswal
08/06/2024, 2:05 PMBaseScreen
which has such utility methods, now i want to add features to Screens using Composition, but i want to call functions which are in the base class BaseScreen
I hope you understand what i mean to sayVaibhav Jaiswal
08/06/2024, 2:07 PMUseCase
in this case
Example
interface PostActionHandler : KoinComponent {
val postActionUseCase: PostActionUseCase
val component: BaseComponent
val onNavEvent: (AppNavEvents) -> Unit
val navigateForRemote: Boolean
get() = false
fun handlePostAction(action: PostAction) = with(action) {
trackPostAction(this)
when (this) {
is PostAction.AuthorClick -> handlePostAuthorClick(post)
is PostAction.Clicked -> handlePostClick(post)
is PostAction.GroupClick -> handleGroupClick(group)
is PostAction.LikeToggle -> handlePostLike(post)
is PostAction.ImageClick -> handlePostImageClick(post, index)
CLOVIS
08/06/2024, 2:08 PMVaibhav Jaiswal
08/06/2024, 2:09 PMCLOVIS
08/06/2024, 2:09 PMVaibhav Jaiswal
08/06/2024, 2:11 PMVaibhav Jaiswal
08/06/2024, 2:11 PMinterface CreatePostFeature {
val createPostUseCase: CreatePostUseCase
val component: BaseComponent
val refType: PostReferenceType
val onNavEvent: (AppNavEvents) -> Unit
val localPostsEventBus: LocalPostEventBus
val imagesLeft
get() = CreatePostUseCase.maxImageCount - images.size
val imagePickerFeature: ImagePickerFeature?
get() = null
val scope
get() = component.componentScope
val text: TextFieldValue
get() = createPostUseCase.text
val images
get() = createPostUseCase.images
val link
get() = createPostUseCase.link
CLOVIS
08/06/2024, 2:12 PMVaibhav Jaiswal
08/06/2024, 2:12 PMUseCase
class for variables and add wrapper variables to access those,
Rather create the stateFlow, mutableState etc directly