mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-12-30 15:38:59 +00:00
Can create private conversations from friends list
This commit is contained in:
parent
00de759caf
commit
f5c8232b14
@ -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<Integer, Void, Integer>(){
|
||||
|
||||
@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);
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
17
app/src/main/res/layout/dialog_loading.xml
Normal file
17
app/src/main/res/layout/dialog_loading.xml
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="horizontal" android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ProgressBar
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/dialog_loading_msg"/>
|
||||
|
||||
</LinearLayout>
|
@ -1,6 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<!-- Start private conversation -->
|
||||
<item
|
||||
android:id="@+id/menu_fragment_friendslist_private_conversation"
|
||||
android:title="@string/action_friends_start_private_conversation" />
|
||||
|
||||
<!-- Delete the friend -->
|
||||
<item
|
||||
android:id="@+id/menu_fragment_friendslist_delete_friend"
|
||||
|
@ -92,4 +92,7 @@
|
||||
<string name="fragment_update_conversation_members_menu_delete">Remove</string>
|
||||
<string name="err_get_conversation_info">Could not get information about the conversation !</string>
|
||||
<string name="err_conversation_update">Could not update conversation !</string>
|
||||
<string name="action_friends_start_private_conversation">Private conversation</string>
|
||||
<string name="err_get_private_conversation">Could not get a private conversation !</string>
|
||||
<string name="dialog_loading_msg">Loading…</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user