From 8a309dd508bbd3e4d7b69d9e0f6d34bbf5d71c10 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 2 Mar 2019 09:57:57 +0100 Subject: [PATCH] Simplied GetUserHelper constructors --- .../client/data/helpers/GetUsersHelper.java | 40 ++++--------------- .../data/helpers/UsersInfoDbHelper.java | 12 ++++-- .../ui/activities/SearchUserActivity.java | 2 +- .../fragments/AbstractPostsListFragment.java | 2 +- .../ui/fragments/ConversationFragment.java | 2 +- .../fragments/ConversationsListFragment.java | 2 +- .../ui/fragments/FriendsListFragment.java | 2 +- .../fragments/UpdateConversationFragment.java | 3 +- .../fragments/userpage/UserPageFragment.java | 2 +- 9 files changed, 24 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java index a01c6f0..a726005 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/GetUsersHelper.java @@ -23,17 +23,12 @@ import java.util.ArrayList; * Created by pierre on 12/10/17. */ -public class GetUsersHelper { +public class GetUsersHelper extends BaseHelper { /** * Debug tag */ - private final String TAG = "GetUsersHelper"; - - /** - * The context of the application - */ - private Context mContext; + private final String TAG = GetUsersHelper.class.getSimpleName(); /** * User information database helper @@ -46,30 +41,11 @@ public class GetUsersHelper { * @param context The context of the application */ public GetUsersHelper(@NonNull Context context){ - this(context, DatabaseHelper.getInstance(context)); + super(context); + + this.udbHelper = new UsersInfoDbHelper(context); } - /** - * Public constructor of the class - * - * @param context The context of execution of the application - * @param udbHelper User database helper - */ - public GetUsersHelper(@NonNull Context context, @NonNull UsersInfoDbHelper udbHelper){ - mContext = context.getApplicationContext(); - this.udbHelper = udbHelper; - } - - /** - * Public constructor of the class - * - * @param context The context of execution of the application - * @param dbHelper DatabaseHelper - */ - public GetUsersHelper(@NonNull Context context, @NonNull DatabaseHelper dbHelper){ - mContext = context; - this.udbHelper = new UsersInfoDbHelper(dbHelper); - } /** * Get information about a single user from the server @@ -127,7 +103,7 @@ public class GetUsersHelper { public AdvancedUserInfo get_advanced_infos(int userID){ //Perform an API request - APIRequest params = new APIRequest(mContext, + APIRequest params = new APIRequest(getContext(), "user/getAdvancedUserInfos"); params.setTryContinueOnError(true); params.addInt("userID", userID); @@ -263,7 +239,7 @@ public class GetUsersHelper { private ArrayList search_users_online(String query, int limit){ //Make an API request - APIRequest params = new APIRequest(mContext, "search/user"); + APIRequest params = new APIRequest(getContext(), "search/user"); params.addString("query", query); params.addString("searchLimit", ""+limit); @@ -299,7 +275,7 @@ public class GetUsersHelper { //Perform a request on the API server //Setup the request - APIRequest requestParameters = new APIRequest(mContext, + APIRequest requestParameters = new APIRequest(getContext(), "user/getInfosMultiple"); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/UsersInfoDbHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/UsersInfoDbHelper.java index 451fd58..6172d0f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/UsersInfoDbHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/UsersInfoDbHelper.java @@ -1,6 +1,7 @@ package org.communiquons.android.comunic.client.data.helpers; import android.content.ContentValues; +import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; @@ -35,11 +36,14 @@ class UsersInfoDbHelper { /** * Class constructor * - * @param dbHelper Database helper object + * @param context Application context */ - UsersInfoDbHelper(DatabaseHelper dbHelper){ - this.dbHelper = dbHelper; - clean(); + UsersInfoDbHelper(Context context){ + this.dbHelper = DatabaseHelper.getInstance(context); + + //Clean user information if we are connected to the Internet + if(APIRequestHelper.isAPIavailable(context)) + clean(); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/SearchUserActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/SearchUserActivity.java index 7f5474c..d468a13 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/SearchUserActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/SearchUserActivity.java @@ -63,7 +63,7 @@ public class SearchUserActivity extends AppCompatActivity setContentView(R.layout.activity_search_user); //Create a get user helper object - getUsersHelper = new GetUsersHelper(this, DatabaseHelper.getInstance(this)); + getUsersHelper = new GetUsersHelper(this); //Get view searchField = findViewById(R.id.activity_search_user_field); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java index 95a8e62..3542a96 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java @@ -186,7 +186,7 @@ public abstract class AbstractPostsListFragment extends AbstractFragment mCommentsHelper = new CommentsHelper(getActivity()); //Create user helper - mUserHelper = new GetUsersHelper(getActivity(), DatabaseHelper.getInstance(getActivity())); + mUserHelper = new GetUsersHelper(getActivity()); //Create likes helper mLikesHelper = new LikesHelper(getActivity()); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java index a2a3b81..8eefb07 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java @@ -222,7 +222,7 @@ public class ConversationFragment extends Fragment conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); //Get user helper - getUsersHelper = new GetUsersHelper(getActivity(), dbHelper); + getUsersHelper = new GetUsersHelper(Objects.requireNonNull(getActivity())); if(conversation_id < 1){ throw new RuntimeException(TAG + " requires a valid conversation ID when created !"); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java index a103221..59198b7 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationsListFragment.java @@ -112,7 +112,7 @@ public class ConversationsListFragment extends AbstractFragment implements Adapt DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); //Instantiate the user information helper - userHelper = new GetUsersHelper(Objects.requireNonNull(getActivity()), dbHelper); + userHelper = new GetUsersHelper(Objects.requireNonNull(getActivity())); //Create the conversation list helper conversationsListHelper = new ConversationsListHelper(getActivity(), dbHelper); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java index 47b0f3c..7fbd0b3 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/FriendsListFragment.java @@ -127,7 +127,7 @@ public class FriendsListFragment extends Fragment implements OnFriendListActionL mFriendsHelper = new FriendsListHelper(mDbHelper, mContext); //Create get user helper - mUsersHelper = new GetUsersHelper(mContext, mDbHelper); + mUsersHelper = new GetUsersHelper(mContext); //Cast activity to mConvOpener try { diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UpdateConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UpdateConversationFragment.java index abdcfe2..bd7ace8 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UpdateConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/UpdateConversationFragment.java @@ -36,6 +36,7 @@ import org.communiquons.android.comunic.client.ui.listeners.openConversationList import org.communiquons.android.comunic.client.ui.views.AppBarLayout; import java.util.ArrayList; +import java.util.Objects; import static org.communiquons.android.comunic.client.ui.Constants.IntentRequestCode.CONVERSATION_UPDATE_FIND_USER_ID_INTENT; @@ -171,7 +172,7 @@ public class UpdateConversationFragment extends Fragment { DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); //Get User helper - usersHelper = new GetUsersHelper(getActivity(), dbHelper); + usersHelper = new GetUsersHelper(Objects.requireNonNull(getActivity())); //Get conversation list helper convListHelper = new ConversationsListHelper(getActivity(), dbHelper); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/userpage/UserPageFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/userpage/UserPageFragment.java index 3100d3d..6d35194 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/userpage/UserPageFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/userpage/UserPageFragment.java @@ -91,7 +91,7 @@ public class UserPageFragment extends Fragment { DatabaseHelper dbHelper = DatabaseHelper.getInstance(getActivity()); //Create getUserHelper instance - getUsersHelper = new GetUsersHelper(getActivity(), dbHelper); + getUsersHelper = new GetUsersHelper(getActivity()); //Get the open user page listener mOpenUsersPageListener = (onOpenUsersPageListener) getActivity();