mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 13:59:29 +00:00
Friends list is automatically refreshed
This commit is contained in:
parent
2976212a0f
commit
c6cbdc20f0
@ -1,7 +1,6 @@
|
||||
package org.communiquons.android.comunic.client;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
@ -14,9 +13,10 @@ import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.communiquons.android.comunic.client.api.APIRequest;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestTask;
|
||||
import org.communiquons.android.comunic.client.data.Account.Account;
|
||||
import org.communiquons.android.comunic.client.data.Account.AccountUtils;
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.FriendRefreshLoopRunnable;
|
||||
import org.communiquons.android.comunic.client.fragments.FriendsListFragment;
|
||||
import org.communiquons.android.comunic.client.fragments.UserInfosFragment;
|
||||
|
||||
@ -38,6 +38,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
*/
|
||||
private AccountUtils aUtils;
|
||||
|
||||
/**
|
||||
* Friends list refresh thread
|
||||
*/
|
||||
private Thread friendsListRefreshThread;
|
||||
|
||||
/**
|
||||
* Database helper
|
||||
*/
|
||||
private DatabaseHelper dbHelper;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@ -63,7 +73,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
//If it is the first time the application is launched, started the user friends tab
|
||||
//Initialize DatabaseHelper
|
||||
dbHelper = new DatabaseHelper(getApplicationContext());
|
||||
|
||||
//If it is the first time the application is launched, start the user friends tab
|
||||
if(savedInstanceState == null){
|
||||
openFriendsFragment();
|
||||
}
|
||||
@ -77,7 +90,22 @@ public class MainActivity extends AppCompatActivity {
|
||||
if(!account.signed_in()){
|
||||
//Open the login activity
|
||||
startActivity(new Intent(this, LoginActivity.class));
|
||||
return;
|
||||
}
|
||||
|
||||
//Refresh friends list through a thread
|
||||
friendsListRefreshThread = new Thread(
|
||||
new FriendRefreshLoopRunnable(getApplicationContext(), dbHelper));
|
||||
friendsListRefreshThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
|
||||
//Stop the friends list refresh thread
|
||||
if(friendsListRefreshThread != null)
|
||||
friendsListRefreshThread.interrupt();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,61 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
|
||||
/**
|
||||
* Automatically refresh friends list thread
|
||||
*
|
||||
* Goal this thread : this thread is killed when it is not required anymore to refresh the friends
|
||||
* list.
|
||||
*
|
||||
* However, it launches periodically another thread, FriendsListRefreshRunnable, which is never
|
||||
* killed, in order not to get errors.
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/19/17.
|
||||
*/
|
||||
|
||||
public class FriendRefreshLoopRunnable implements Runnable {
|
||||
|
||||
private final Object object = new Object();
|
||||
|
||||
private Context context;
|
||||
private DatabaseHelper dbHelper;
|
||||
|
||||
public FriendRefreshLoopRunnable(Context context, DatabaseHelper dbHelper){
|
||||
this.context = context;
|
||||
this.dbHelper = dbHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perpetual loop that only stops when it get killed
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
synchronized (object) {
|
||||
|
||||
Thread thread;
|
||||
|
||||
while (true) {
|
||||
|
||||
//Create a refresh thread
|
||||
thread = new Thread(new FriendsListRefreshRunnable(context, dbHelper));
|
||||
thread.start();
|
||||
|
||||
try {
|
||||
thread.join();
|
||||
object.wait(15000);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return; //Stop the refresh loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Auto friends list refresh runnable
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/19/17.
|
||||
*/
|
||||
|
||||
class FriendsListRefreshRunnable implements Runnable {
|
||||
|
||||
/**
|
||||
* This runnable requires a context object in order to connect to the API
|
||||
*
|
||||
* The context of the application (or of the activity)
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Database object
|
||||
*/
|
||||
private DatabaseHelper dbHelper;
|
||||
|
||||
/**
|
||||
* Runnable constructor
|
||||
*
|
||||
* @param context The context of the application
|
||||
* @param dbHelper Database helper
|
||||
*/
|
||||
FriendsListRefreshRunnable(Context context, DatabaseHelper dbHelper){
|
||||
this.mContext = context;
|
||||
this.dbHelper = dbHelper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
GetFriendsList getFriendsList = new GetFriendsList(mContext);
|
||||
FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper);
|
||||
|
||||
//Get the latest version of the list
|
||||
ArrayList<Friend> friendsList = getFriendsList.get();
|
||||
|
||||
//Save it (only in case of success)
|
||||
if(friendsList != null)
|
||||
friendsDBHelper.update_list(friendsList);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.communiquons.android.comunic.client.api.APIRequest;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestParameters;
|
||||
@ -38,6 +39,7 @@ class GetFriendsList {
|
||||
*
|
||||
* @return The list of friend of the user
|
||||
*/
|
||||
@Nullable
|
||||
ArrayList<Friend> get(){
|
||||
|
||||
//Prepare the API request
|
||||
|
@ -3,6 +3,8 @@ package org.communiquons.android.comunic.client.data.friendsList;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
@ -17,15 +19,15 @@ public abstract class GetFriendsListTask extends AsyncTask<Void, Void, ArrayList
|
||||
/**
|
||||
* The context of the task
|
||||
*/
|
||||
private Context mContext;
|
||||
private DatabaseHelper dbHelper;
|
||||
|
||||
/**
|
||||
* Public constructor to the class
|
||||
*
|
||||
* @param context the context of execution of the refresh task
|
||||
* @param dbHelper Database helper
|
||||
*/
|
||||
public GetFriendsListTask(Context context){
|
||||
mContext = context;
|
||||
public GetFriendsListTask(DatabaseHelper dbHelper){
|
||||
this.dbHelper = dbHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -47,6 +49,6 @@ public abstract class GetFriendsListTask extends AsyncTask<Void, Void, ArrayList
|
||||
@Override
|
||||
protected ArrayList<Friend> doInBackground(Void... params) {
|
||||
//Create the GetFriendList object and use it to fetch the list
|
||||
return new GetFriendsList(mContext).get();
|
||||
return new FriendsListDbHelper(dbHelper).get_list();
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public class FriendsListFragment extends Fragment {
|
||||
//Display loading bar
|
||||
display_progress_bar(true);
|
||||
|
||||
new GetFriendsListTask(getActivity().getApplicationContext()){
|
||||
new GetFriendsListTask(mDbHelper){
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(ArrayList<Friend> friendsList) {
|
||||
|
Loading…
Reference in New Issue
Block a user