-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
75 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
app/src/main/java/org/oppia/app/databinding/TextViewBindingAdapters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
)); | ||
} | ||
} |
75 changes: 0 additions & 75 deletions
75
app/src/main/java/org/oppia/app/databinding/TextViewBindingAdapters.kt
This file was deleted.
Oops, something went wrong.