Bo Z
Bo Z

Reputation: 2607

Why instead of string method returns Kotlin.Unit?

I have a regular method which through the presenter returns String from shared preferences class. The return value is correct and shows needed value string but in activity I received Kotlin.Unit instead of that value.

Where might be the issue is?

Shared pref class is java / Activity and presenter Kotlin

enter image description here

Code which in result returns string

public String getAdTimeLeft() {
    String result = "";
    String triggerTime = getTriggertTime();
    String currentTime = AdLauncher.getInstance().getCurrentTimeAsString();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    Date triggerTimeDate = null;
    try {
        triggerTimeDate = simpleDateFormat.parse(triggerTime);
        Date currentTimeDate = simpleDateFormat.parse(currentTime);
        long difference = triggerTimeDate.getTime() - currentTimeDate.getTime();
        long days = (int) (difference / (1000*60*60*24));
        long hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60));
        int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
        result = String.valueOf(min);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return result;
}

Suddenly figured out if I make a call directly from Pref class it works correct. Like this mTimeLeft.text = SharPrefManager.getInstance().adTimeLeft

But through presenter not working. This is presenter code SharPrefManager.getInstance().getAdTimeLeft()

Why with presenter it not working?

Upvotes: 3

Views: 1573

Answers (1)

Bo Z
Bo Z

Reputation: 2607

The solution was missed return state. By default Kotlin leave it as UNIT thats why as result I received kotlin.Unit text. The solution change looks like this

I changed presenter

fun grabAdTimeLeft(): String? {
    return SharPrefManager.getInstance().grabAdTimeLeft()
}

Upvotes: 2

Related Questions