Skip to content

Commit

Permalink
Translated TextViewBA
Browse files Browse the repository at this point in the history
  • Loading branch information
miaboloix committed Aug 7, 2020
1 parent 48ecefc commit 849549d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.oppia.app.databinding;

import android.content.Context;
import android.content.res.Resources;
import android.widget.TextView;
import androidx.databinding.BindingAdapter;
import org.jetbrains.annotations.NotNull;
import org.oppia.app.R;
import org.oppia.util.system.OppiaDateTimeFormatter;
import java.util.Locale;

public final class TextViewBindingAdapters {
/**
* Binds date text with relative time.
*/
@BindingAdapter("profile:created")
public static void setProfileDataText(@NotNull TextView textView, Long timestamp) {
OppiaDateTimeFormatter oppiaDateTimeFormatter = new OppiaDateTimeFormatter();
String time = oppiaDateTimeFormatter.formatDateFromDateString(
OppiaDateTimeFormatter.DD_MMM_YYYY,
timestamp,
Locale.getDefault()
);
textView.setText(textView.getContext().getString(R.string.profile_edit_created, time));
}

@BindingAdapter("profile:lastVisited")
public static void setProfileLastVisitedText(@NotNull TextView textView, Long timestamp) {
textView.setText(
String.format(
textView.getContext().getString(R.string.profile_last_used) + " " + getTimeAgo(
timestamp,
textView.getContext()
)
)
);
}

private static int SECOND_MILLIS = 1000;
private static int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static int DAY_MILLIS = 24 * HOUR_MILLIS;

public static String getTimeAgo(long lastVisitedTimeStamp, Context context) {

OppiaDateTimeFormatter oppiaDateTimeFormatter = new OppiaDateTimeFormatter();
long timeStamp =
oppiaDateTimeFormatter.checkAndConvertTimestampToMilliseconds(lastVisitedTimeStamp);
long now = oppiaDateTimeFormatter.currentDate().getTime();

if (timeStamp > now || timeStamp <= 0) { return ""; }

Resources res = context.getResources();
long timeDifference = now - timeStamp;

if (timeDifference < MINUTE_MILLIS) {
return context.getString(R.string.just_now);
} else if (timeDifference < 50 * MINUTE_MILLIS) {
return context.getString(
R.string.time_ago,
res.getQuantityString(
R.plurals.minutes,
(int) timeDifference / MINUTE_MILLIS,
timeDifference / MINUTE_MILLIS
)
);
} else if (timeDifference < 24 * HOUR_MILLIS) {
return context.getString(
R.string.time_ago,
res.getQuantityString(
R.plurals.hours,
(int) timeDifference / HOUR_MILLIS,
timeDifference / HOUR_MILLIS
)
);
} else if (timeDifference < 48 * HOUR_MILLIS) {
return context.getString(R.string.yesterday);
}
return context.getString(
R.string.time_ago,
res.getQuantityString(
R.plurals.days,
(int) timeDifference / DAY_MILLIS,
timeDifference / DAY_MILLIS
));
}
}

This file was deleted.

0 comments on commit 849549d

Please sign in to comment.