Home > Enterprise >  How to handle business logic and Android string resources inside Kotlin data classes?
How to handle business logic and Android string resources inside Kotlin data classes?

Time:03-09

Is there a recommended practice on how to handle the business logic inside a Kotlin Data class? For eg: I have a DTO object thats coming from a REST API like this:

data class ResponseDTO(
    @SerializedName("isAuto") val isAuto: Boolean,
    @SerializedName("autoStatus") val autoStatus: String
) {
    fun getStatus() : String {
        if (isAuto) {
            if (autoStatus.isNotEmpty()) {
                return autoStatus
            } else {
                return "Default Auto"
            }
        }
        else {
            return "Default Manual"
        }
    }
}

We need to return different Strings based on the values returned so what are the best practices to

  1. Define the business logic inside the Kotlin data class
  2. Get the string resources inside the Kotlin data class

This is only an example and not the exact code but our API is returning basic values and we have lot of Business logic to deduce what we want to display in the UI.

CodePudding user response:

I would separate the data classes (transfer objects) from the ones that implement business logic, because they change for different reasons.

Thus I would create a class that takes the transfer object (maybe an interface of the transfer object) as it's argument. E.g.

// I'm not very good in Kotlin so I will use Java here.
// I guess you'll find better names

public interface Data { 
   public Boolean isAuto();
   public String getAutoStatus();
}
   
public class SomeGoodName {
    private Data data;
    
    public SomeGoodName(Data data){
        this.data = data;
    }

    public String getStatus(){
       if(data.isAuto()){
            if (autoStatus.isNotEmpty()) {
                return data.getAutoStatus()
            } else {
                return "Default Auto"
            }
       } else {
            return "Default Manual"
       }
    }
}

The transfer object can then implement the Data interface.

CodePudding user response:

try this


enum class Default {
  AUTO,MANUAL;
  
  fun getStringMessage(context:Context): String {
    val res = context.resources
    return when(this){
       AUTO -> res.getString(R.string.auto)
       MANUAL-> res.getString(R.string.manual)
    }
  }
}

data class ResponseDTO(
    @SerializedName("isAuto") val isAuto: Boolean,
    @SerializedName("autoStatus") val autoStatus: String
) {
    fun getStatus() : Default {
        if (isAuto) {
            if (autoStatus.isNotEmpty()) {
                return autoStatus
            } else {
                return Default.AUTO
            }
        }
        else {
            return Default.MANUAL
        }
    }
}

in activity or fragment or viewModel


...
val defaultMessage = ResponseDTO(
    isAuto = true,
    autoStatus = "somthing"
).getStatus().getStringMessage(context)
...

  • Related