From 9cf7e86ac849fe5080ba9036a513af8dd7a238f2 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 3 Sep 2018 13:19:36 +0200 Subject: [PATCH] Internal users / groups references (@) are now supported --- .../data/enums/VirtualDirectoryType.java | 11 +++ .../data/helpers/VirtualDirectoryHelper.java | 77 ++++++++++++++++++ .../client/data/models/VirtualDirectory.java | 31 +++++++ .../client/ui/activities/MainActivity.java | 81 ++++++++++++++++++- .../asynctasks/FindVirtualDirectoryTask.java | 22 +++++ .../client/ui/views/ContentTextView.java | 26 +++++- app/src/main/res/values-fr/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + 8 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/enums/VirtualDirectoryType.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/helpers/VirtualDirectoryHelper.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/models/VirtualDirectory.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/FindVirtualDirectoryTask.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/VirtualDirectoryType.java b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/VirtualDirectoryType.java new file mode 100644 index 0000000..e2b0804 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/VirtualDirectoryType.java @@ -0,0 +1,11 @@ +package org.communiquons.android.comunic.client.data.enums; + +/** + * Kinds of virtual directory + * + * @author Pierre HUBERT + */ +public enum VirtualDirectoryType { + USER, + GROUP +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/VirtualDirectoryHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/VirtualDirectoryHelper.java new file mode 100644 index 0000000..63b32a3 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/VirtualDirectoryHelper.java @@ -0,0 +1,77 @@ +package org.communiquons.android.comunic.client.data.helpers; + +import android.content.Context; +import android.support.annotation.Nullable; + +import org.communiquons.android.comunic.client.data.enums.VirtualDirectoryType; +import org.communiquons.android.comunic.client.data.models.APIRequest; +import org.communiquons.android.comunic.client.data.models.APIResponse; +import org.communiquons.android.comunic.client.data.models.VirtualDirectory; +import org.json.JSONException; +import org.json.JSONObject; + +/** + * Virtual directory helper + * + * @author Pierre HUBERT + */ +public class VirtualDirectoryHelper extends BaseHelper { + + public VirtualDirectoryHelper(Context context) { + super(context); + } + + /** + * Find the information associated with a directory + * + * @param directory The directory to find + * @return Information about the directory / null in case of failure + */ + @Nullable + public VirtualDirectory find(String directory){ + + APIRequest request = new APIRequest(getContext(), "virtualDirectory/find"); + request.addString("directory", directory); + + try { + APIResponse response = new APIRequestHelper().exec(request); + if(response.getResponse_code() != 200) return null; + return APIToVirtualDirectory(response.getJSONObject()); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * Turn an API entry into a VirtualDirectory object + * + * @param obj The object to parse + * @return Generated VirtualDirectory object + * @throws JSONException in case of failure + */ + private static VirtualDirectory APIToVirtualDirectory(JSONObject obj) throws JSONException { + VirtualDirectory virtualDirectory = new VirtualDirectory(); + virtualDirectory.setKind(StringToVirtualDirectoryType(obj.getString("kind"))); + virtualDirectory.setId(obj.getInt("id")); + return virtualDirectory; + } + + /** + * Turn a string into a virtual directory type + * + * @param string The string to convert + * @return Matching value in enum + */ + private static VirtualDirectoryType StringToVirtualDirectoryType(String string) { + switch (string){ + case "user": + return VirtualDirectoryType.USER; + + case "group": + return VirtualDirectoryType.GROUP; + } + + throw new AssertionError(); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/VirtualDirectory.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/VirtualDirectory.java new file mode 100644 index 0000000..065b47a --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/VirtualDirectory.java @@ -0,0 +1,31 @@ +package org.communiquons.android.comunic.client.data.models; + +import org.communiquons.android.comunic.client.data.enums.VirtualDirectoryType; + +/** + * Virtual directory information + * + * @author Pierre HUBERT + */ +public class VirtualDirectory { + + //Private fields + private VirtualDirectoryType kind; + private int id; + + public VirtualDirectoryType getKind() { + return kind; + } + + public void setKind(VirtualDirectoryType kind) { + this.kind = kind; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index c9d93f4..5243a5f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -13,7 +13,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.FragmentTransaction; import android.support.v4.content.LocalBroadcastManager; -import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -24,15 +23,19 @@ import android.widget.Toast; import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.crashreporter.CrashReporter; +import org.communiquons.android.comunic.client.data.enums.VirtualDirectoryType; import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper; import org.communiquons.android.comunic.client.data.helpers.AccountHelper; import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; import org.communiquons.android.comunic.client.data.helpers.DebugHelper; import org.communiquons.android.comunic.client.data.models.NotificationsCount; +import org.communiquons.android.comunic.client.data.models.VirtualDirectory; import org.communiquons.android.comunic.client.data.runnables.FriendRefreshLoopRunnable; import org.communiquons.android.comunic.client.data.services.NotificationsService; import org.communiquons.android.comunic.client.data.utils.PreferencesUtils; +import org.communiquons.android.comunic.client.ui.asynctasks.FindVirtualDirectoryTask; +import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; import org.communiquons.android.comunic.client.ui.fragments.ConversationFragment; import org.communiquons.android.comunic.client.ui.fragments.ConversationsListFragment; import org.communiquons.android.comunic.client.ui.fragments.FriendsListFragment; @@ -91,6 +94,11 @@ public class MainActivity extends BaseActivity implements */ private NavigationBar mNavBar; + /** + * Tasks + */ + private FindVirtualDirectoryTask mFindVirtualDirectoryTask; + /** * Broadcast receiver */ @@ -197,9 +205,12 @@ public class MainActivity extends BaseActivity implements friendsListRefreshRunnable.interrupt(); } + @Override + protected void onDestroy() { + super.onDestroy(); - - + unsetFindVirtualDirectoryTask(); + } /** * Activity context menu @@ -421,6 +432,70 @@ public class MainActivity extends BaseActivity implements ((MainActivity)activity).getSupportFragmentManager().popBackStack(); } + + /** + * Follow a tag to open it + * + * @param activity Activity object + * @param tag The tag to follow + */ + public static void FollowTag(@NonNull Activity activity, String tag){ + if(!(activity instanceof MainActivity)) + throw new RuntimeException("Need a MainActivity!"); + + ((MainActivity)activity).followTag(tag); + } + + /** + * Unset FindVirtualDirectoryTask callback in order to prevent memory leaks + */ + private void unsetFindVirtualDirectoryTask(){ + if(mFindVirtualDirectoryTask != null) + mFindVirtualDirectoryTask.setOnPostExecuteListener(null); + } + + /** + * Follow a tag to open it + * + * @param tag The tag to find + */ + public void followTag(String tag){ + final AlertDialog dialog = UiUtils.create_loading_dialog(this); + + unsetFindVirtualDirectoryTask(); + mFindVirtualDirectoryTask = new FindVirtualDirectoryTask(this); + mFindVirtualDirectoryTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { + @Override + public void OnPostExecute(VirtualDirectory virtualDirectory) { + dialog.dismiss(); + openDirectory(virtualDirectory); + } + }); + mFindVirtualDirectoryTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, tag); + + } + + /** + * Open a directory + * + * @param directory The directory to open + */ + public void openDirectory(@Nullable VirtualDirectory directory){ + if(directory == null){ + Toast.makeText(this, R.string.err_find_tag, Toast.LENGTH_SHORT).show(); + return; + } + + if (directory.getKind() == VirtualDirectoryType.GROUP){ + Toast.makeText(this, R.string.err_groups_not_supported, + Toast.LENGTH_SHORT).show(); + } + + else if(directory.getKind() == VirtualDirectoryType.USER){ + openUserPage(directory.getId()); + } + } + /** * Open the friends list fragment */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/FindVirtualDirectoryTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/FindVirtualDirectoryTask.java new file mode 100644 index 0000000..6523038 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/FindVirtualDirectoryTask.java @@ -0,0 +1,22 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.helpers.VirtualDirectoryHelper; +import org.communiquons.android.comunic.client.data.models.VirtualDirectory; + +/** + * Find a virtual directory task + * + * @author Pierre HUBERT + */ +public class FindVirtualDirectoryTask extends SafeAsyncTask { + public FindVirtualDirectoryTask(Context context) { + super(context); + } + + @Override + protected VirtualDirectory doInBackground(String... strings) { + return new VirtualDirectoryHelper(getContext()).find(strings[0]); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ContentTextView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ContentTextView.java index c51f31e..e5dd878 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ContentTextView.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/ContentTextView.java @@ -15,6 +15,7 @@ import android.util.AttributeSet; import android.view.View; import org.communiquons.android.comunic.client.data.utils.StringsUtils; +import org.communiquons.android.comunic.client.ui.activities.MainActivity; import org.communiquons.android.comunic.client.ui.utils.UiUtils; /** @@ -76,7 +77,7 @@ public class ContentTextView extends android.support.v7.widget.AppCompatTextView int pos = 0; for (String part : parts) { - //Mark it as URL + //Process it as URL if possible if (StringsUtils.isURL(part)) { ClickableSpan clickableSpan = new URLClickableSpan(part); @@ -85,6 +86,12 @@ public class ContentTextView extends android.support.v7.widget.AppCompatTextView } + else if(part.length() > 0 && part.charAt(0) == '@'){ + ClickableSpan clickableSpan = new TAGClickableSpan(part); + ssb.setSpan(clickableSpan, pos, pos + part.length(), + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + pos += part.length() + 1; } @@ -123,4 +130,21 @@ public class ContentTextView extends android.support.v7.widget.AppCompatTextView getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mURL))); } } + + /** + * Clickable span for tag + */ + private class TAGClickableSpan extends BaseClickableSpan { + + private String mTag; + + private TAGClickableSpan(String tag){ + mTag = tag.substring(1); + } + + @Override + public void onClick(View widget) { + MainActivity.FollowTag(getActivity(), mTag); + } + } } diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 1ce52e9..59b58d7 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -289,4 +289,6 @@ Les utilisateurs connectés à Comunic Uniquement mes amis Une erreur a survenu lors de la mise à jour du niveau de visibilité de l\'image de compte ! + Le tag spécifié n\'a pas été trouvé ! + Les groupes ne sont pas encore supportés sur l\'application Android… \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ee4bff2..adf3091 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -288,4 +288,6 @@ Authenticated users Friends only Could not update account image visibility level! + Could not find requested tag! + Groups are not supported yet by the Android application!