diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index 4b09d3e..392f85f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -4,10 +4,13 @@ import android.app.AlertDialog; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.design.widget.BottomNavigationView; import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; @@ -18,6 +21,7 @@ import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; import org.communiquons.android.comunic.client.data.friendsList.FriendRefreshLoopRunnable; +import org.communiquons.android.comunic.client.data.utils.UiUtils; import org.communiquons.android.comunic.client.fragments.ConversationFragment; import org.communiquons.android.comunic.client.fragments.ConversationsListFragment; import org.communiquons.android.comunic.client.fragments.FriendsListFragment; @@ -34,6 +38,11 @@ public class MainActivity extends AppCompatActivity implements ConversationsListHelper.openConversationListener, ConversationsListHelper.updateConversationListener { + /** + * Debug tag + */ + private static final String TAG = "MainActivity"; + /** * Account object */ @@ -54,6 +63,11 @@ public class MainActivity extends AppCompatActivity */ private DatabaseHelper dbHelper; + /** + * Conversations list helper + */ + private ConversationsListHelper conversationsListHelper; + /** * Bottom navigation view */ @@ -87,6 +101,9 @@ public class MainActivity extends AppCompatActivity //Initialize DatabaseHelper dbHelper = DatabaseHelper.getInstance(this); + //Intialize conversation list helper + conversationsListHelper = new ConversationsListHelper(this, dbHelper); + //If it is the first time the application is launched, start the user friends tab if(savedInstanceState == null){ openFriendsFragment(); @@ -279,6 +296,37 @@ public class MainActivity extends AppCompatActivity } + @Override + public void openPrivateConversation(int userID) { + //Log action + Log.v(TAG, "Open private conversation with user ID " + userID); + + //Create a loading dialog + final AlertDialog dialog = UiUtils.create_loading_dialog(this); + + //Get conversation ID in the background + new AsyncTask(){ + + @Override + protected Integer doInBackground(Integer... params) { + return conversationsListHelper.getPrivate(params[0], true); + } + + @Override + protected void onPostExecute(@Nullable Integer integer) { + + //Close loading dialog + dialog.dismiss(); + + if(integer != null) + openConversation(integer); + else + Toast.makeText(MainActivity.this, R.string.err_get_private_conversation, + Toast.LENGTH_SHORT).show(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, userID); + } + @Override public void createConversation() { updateConversation(0); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java index 55f483a..a415784 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -103,6 +103,49 @@ public class ConversationsListHelper { return downloadSingle(convID); } + /** + * Search and return the ID of a private conversation with a specified user + * + * @param userID The ID of the target user + * @param allowCreate If set to true, then if the server does not find any matching conversation, + * it will automatically create a new one + * @return The ID of the conversation or null in case of failure + */ + @Nullable + public Integer getPrivate(int userID, boolean allowCreate){ + + //Prepare an API request + APIRequestParameters params = new APIRequestParameters(mContext, + "conversations/getPrivate"); + params.addParameter("otherUser", userID); + params.addParameter("allowCreate", allowCreate); + + //Try to perform request + try { + APIResponse response = new APIRequest().exec(params); + JSONObject object = response.getJSONObject(); + + //Check for conversations ID + if(!object.has("conversationsID")) + return null; + + //Get conversations ID + JSONArray conversations = object.getJSONArray("conversationsID"); + + //Check if the array is empty + if(conversations.length() == 0) + return null; + else + return conversations.getInt(0); + } + + //Catch errors + catch (Exception e) { + e.printStackTrace(); + return null; + } + } + /** * Get the display name of a conversation * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java index b4a9992..7f12f84 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/UiUtils.java @@ -1,9 +1,12 @@ package org.communiquons.android.comunic.client.data.utils; +import android.app.AlertDialog; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Build; +import org.communiquons.android.comunic.client.R; + /** * User Interface utilities * @@ -39,4 +42,23 @@ public class UiUtils { public static Drawable getDrawable(Context context, int drawable_id){ return context.getResources().getDrawable(drawable_id, context.getTheme()); } + + /** + * Create and display a loading dialog + * + * Use dismiss() to close this dialog + * + * @param context The context of the application + * @return The created alert dialog + */ + public static AlertDialog create_loading_dialog(Context context){ + + //Create alert dialog + AlertDialog.Builder builder = new AlertDialog.Builder(context) + .setView(R.layout.dialog_loading); + + //Display the dialog + return builder.show(); + } + } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java index 5a54628..c1d76f6 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java @@ -101,6 +101,16 @@ public class FriendsListFragment extends Fragment { //Create get user helper usersHelper = new GetUsersHelper(mContext, mDbHelper); + + //Cast activity to convOpener + try { + convOpener = (ConversationsListHelper.openConversationListener) getActivity(); + } catch (ClassCastException e){ + e.printStackTrace(); + + throw new RuntimeException(getActivity().getClass().getName() + " must implement" + + "ConversationsListHelper.openConversationListener !"); + } } @Nullable @@ -225,6 +235,11 @@ public class FriendsListFragment extends Fragment { switch (item.getItemId()){ + //To open a private conversation with the friend + case R.id.menu_fragment_friendslist_private_conversation: + convOpener.openPrivateConversation(friendsList.get(friendPos).getFriend().getId()); + return true; + //To delete the friend case R.id.menu_fragment_friendslist_delete_friend: delete_friend(friendPos); diff --git a/app/src/main/res/layout/dialog_loading.xml b/app/src/main/res/layout/dialog_loading.xml new file mode 100644 index 0000000..dffef8b --- /dev/null +++ b/app/src/main/res/layout/dialog_loading.xml @@ -0,0 +1,17 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_fragment_friendslist_item.xml b/app/src/main/res/menu/menu_fragment_friendslist_item.xml index 0cec786..5a627d7 100644 --- a/app/src/main/res/menu/menu_fragment_friendslist_item.xml +++ b/app/src/main/res/menu/menu_fragment_friendslist_item.xml @@ -1,6 +1,11 @@ + + + Remove Could not get information about the conversation ! Could not update conversation ! + Private conversation + Could not get a private conversation ! + Loading…