From 5c8d8a8f526dcfcdffca0943e0f4d6ee95e491f8 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 18 Nov 2017 14:11:50 +0100 Subject: [PATCH] Friend status displayed in friends list. --- .../comunic/client/data/Utilities.java | 27 +++++++++++++++++++ .../client/data/friendsList/Friend.java | 16 +++++++++++ .../data/friendsList/FriendsAdapter.java | 26 +++++++++++++++--- .../fragment_friends_list_friend_item.xml | 2 ++ app/src/main/res/values/colors.xml | 3 +++ app/src/main/res/values/strings.xml | 2 ++ 6 files changed, 73 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java index 0c035bf..31b0018 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java @@ -1,6 +1,8 @@ package org.communiquons.android.comunic.client.data; import android.content.Context; +import android.os.Build; +import android.text.Html; import android.text.TextUtils; import android.util.Log; import android.util.Patterns; @@ -13,6 +15,7 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Date; import java.util.Formatter; /** @@ -184,4 +187,28 @@ public class Utilities { return count; } + + /** + * Prepare a string sent through the API to be shown in a TextView element + * + * @param input The string to prepare + * @return The string ready to be shown + */ + public static String prepareStringTextView(String input){ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return Html.fromHtml(input, Html.FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH).toString(); + } + else + return Html.fromHtml(input).toString(); + } + + /** + * Get current timestamp + * + * @return The current timestamp + */ + public static int time(){ + Date date = new Date(); + return (int) Math.ceil(date.getTime()/1000); + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java index 0506ca2..2bf1e67 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java @@ -1,5 +1,7 @@ package org.communiquons.android.comunic.client.data.friendsList; +import org.communiquons.android.comunic.client.data.Utilities; + /** * Friend object * @@ -9,6 +11,11 @@ package org.communiquons.android.comunic.client.data.friendsList; public class Friend { + /** + * The minimal time of activity required to consider a user as signed in + */ + private static final int USER_INACTIVE_AFTER = 35; + /** * The ID of the friend */ @@ -105,4 +112,13 @@ public class Friend { public int getLast_activity() { return last_activity; } + + /** + * Determine whether user is signed in or not + * + * @return True if user is signed in / false else + */ + public boolean signed_in(){ + return (Utilities.time()-USER_INACTIVE_AFTER) < last_activity; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java index 75baa13..78c1fbe 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java @@ -1,7 +1,6 @@ package org.communiquons.android.comunic.client.data.friendsList; import android.app.Activity; -import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; @@ -11,9 +10,10 @@ import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; +import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; -import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadTask; +import org.communiquons.android.comunic.client.data.Utilities; import java.util.ArrayList; @@ -58,7 +58,27 @@ public class FriendsAdapter extends ArrayAdapter { //Update user name TextView user_name = listItemView.findViewById(R.id.fragment_friendslist_item_fullname); - user_name.setText(friendUser.getUserInfo().getFullName()); + user_name.setText(Utilities.prepareStringTextView(friendUser.getUserInfo().getFullName())); + + //Update user status + boolean signed_in = friendUser.getFriend().signed_in(); + TextView statusView = listItemView.findViewById(R.id.fragment_friendslist_item_status); + + //Set the text + statusView.setText(signed_in ? + getContext().getText(R.string.user_status_online) : + getContext().getText(R.string.user_status_offline) + ); + + //Set the color + int status_color = 0; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { + status_color = getContext().getResources().getColor(signed_in ? R.color.holo_green_dark : R.color.darker_gray, null); + } + else { + status_color = getContext().getResources().getColor(signed_in ? R.color.holo_green_dark : R.color.darker_gray); + } + statusView.setTextColor(status_color); return listItemView; } diff --git a/app/src/main/res/layout/fragment_friends_list_friend_item.xml b/app/src/main/res/layout/fragment_friends_list_friend_item.xml index f31c1c4..5eee0e9 100644 --- a/app/src/main/res/layout/fragment_friends_list_friend_item.xml +++ b/app/src/main/res/layout/fragment_friends_list_friend_item.xml @@ -32,8 +32,10 @@ android:textSize="16sp"/> diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9c..1eb00e8 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,7 @@ #3F51B5 #303F9F #FF4081 + + #ff669900 + #aaa diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bf9ce7f..5d79dd0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -18,4 +18,6 @@ Me Couldn\'t refresh friends list ! Couldn\'t get information about your friends! + Online + Offline