Added the option to update account image visibility level

This commit is contained in:
Pierre HUBERT 2018-09-02 09:32:01 +02:00
parent aef18b085a
commit 5b46e559af
8 changed files with 262 additions and 24 deletions

View File

@ -89,6 +89,24 @@ public class AccountSettingsHelper extends BaseHelper {
}
}
/**
* Update account image visibility
*
* @param visibility New account image visibility
* @return The result of the operation
*/
public boolean setAccountImageVisibility(AccountImageVisibility visibility){
APIRequest request = new APIRequest(getContext(), "settings/set_account_image_visibility");
request.addString("visibility", AccountImageVisibilityToString(visibility));
try {
return new APIRequestHelper().exec(request).getResponse_code() == 200;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* Parse an API entry into an AccountImageSettings entry
*
@ -127,4 +145,26 @@ public class AccountSettingsHelper extends BaseHelper {
throw new AssertionError();
}
}
/**
* Turn an account image visibility level into a string ready for the API
*
* @param visibility Visibility level to convert
* @return Matching string
*/
private static String AccountImageVisibilityToString(AccountImageVisibility visibility){
switch (visibility){
case OPEN:
return "open";
case PUBLIC:
return "public";
case FRIENDS:
return "friends";
default:
throw new AssertionError();
}
}
}

View File

@ -0,0 +1,22 @@
package org.communiquons.android.comunic.client.ui.asynctasks;
import android.content.Context;
import org.communiquons.android.comunic.client.data.enums.AccountImageVisibility;
import org.communiquons.android.comunic.client.data.helpers.AccountSettingsHelper;
/**
* Update account image visibility task
*
* @author Pierre HUBERT
*/
public class UpdateAccountImageVisibilityTask extends SafeAsyncTask<AccountImageVisibility, Void, Boolean> {
public UpdateAccountImageVisibilityTask(Context context) {
super(context);
}
@Override
protected Boolean doInBackground(AccountImageVisibility... accountImageVisibilities) {
return new AccountSettingsHelper(getContext()).setAccountImageVisibility(accountImageVisibilities[0]);
}
}

View File

@ -12,12 +12,15 @@ import android.support.v7.preference.Preference;
import android.widget.Toast;
import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.enums.AccountImageVisibility;
import org.communiquons.android.comunic.client.data.models.AccountImageSettings;
import org.communiquons.android.comunic.client.ui.asynctasks.DeleteUserAccountImageTask;
import org.communiquons.android.comunic.client.ui.asynctasks.GetAccountImageSettingsTask;
import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask;
import org.communiquons.android.comunic.client.ui.asynctasks.UpdateAccountImageVisibilityTask;
import org.communiquons.android.comunic.client.ui.asynctasks.UploadNewAccountImageTask;
import org.communiquons.android.comunic.client.ui.preference.AccountImagePreference;
import org.communiquons.android.comunic.client.ui.preference.AccountImageVisibilityPreference;
import org.communiquons.android.comunic.client.ui.utils.BitmapUtils;
import java.io.FileNotFoundException;
@ -27,19 +30,22 @@ import static org.communiquons.android.comunic.client.ui.Constants.IntentRequest
/**
* Account image settings fragment
*/
public class AccountImageSettingsFragment extends BaseAccountSettingsFragment implements Preference.OnPreferenceClickListener {
public class AccountImageSettingsFragment extends BaseAccountSettingsFragment
implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener {
private static final String TAG = AccountImageSettingsFragment.class.getSimpleName();
//Preferences
private static final String PREFERENCE_UPDATE_ACCOUNT_IMAGE = "update_account_image";
private static final String PREFERENCE_DELETE_ACCOUNT_IMAGE = "delete_account_image";
private static final String PREFERENCE_ACCOUNT_IMAGE_VISIBILITY = "account_image_visibility_level";
private AccountImageSettings mAccountImageSettings;
private GetAccountImageSettingsTask mGetAccountImageSettingsTask;
private UploadNewAccountImageTask mUploadNewAccountImageTask;
private DeleteUserAccountImageTask mDeleteUserAccountImageTask;
private UpdateAccountImageVisibilityTask mUpdateAccountImageVisibilityTask;
@Override
public void onCreatePreferences(Bundle bundle, String s) {
@ -52,6 +58,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
findPreference(PREFERENCE_UPDATE_ACCOUNT_IMAGE).setOnPreferenceClickListener(this);
findPreference(PREFERENCE_DELETE_ACCOUNT_IMAGE).setOnPreferenceClickListener(this);
findPreference(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY).setOnPreferenceChangeListener(this);
}
@Override
@ -59,7 +66,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
super.onResume();
getActivity().setTitle(R.string.preferences_account_image_title);
if(mAccountImageSettings == null)
if (mAccountImageSettings == null)
load_settings();
else
onGotAccountImageSettings(mAccountImageSettings);
@ -72,24 +79,26 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
removeLoadingDialog();
}
private void unset_loading_tasks(){
if(mGetAccountImageSettingsTask != null)
private void unset_loading_tasks() {
if (mGetAccountImageSettingsTask != null)
mGetAccountImageSettingsTask.setOnPostExecuteListener(null);
if(mUploadNewAccountImageTask != null)
if (mUploadNewAccountImageTask != null)
mUploadNewAccountImageTask.setOnPostExecuteListener(null);
if(mDeleteUserAccountImageTask != null)
if (mDeleteUserAccountImageTask != null)
mDeleteUserAccountImageTask.setOnPostExecuteListener(null);
}
if (mUpdateAccountImageVisibilityTask != null)
mUpdateAccountImageVisibilityTask.setOnPostExecuteListener(null);
}
/**
* Load account image settings. As soon as this method is called, cached information about
* account image are cleared, so on pause and resume of the fragment it can be loaded again
*/
private void load_settings(){
private void load_settings() {
unset_loading_tasks();
showLoadingDialog();
@ -105,12 +114,12 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
mGetAccountImageSettingsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void onGotAccountImageSettings(@Nullable AccountImageSettings accountImageSettings){
private void onGotAccountImageSettings(@Nullable AccountImageSettings accountImageSettings) {
removeLoadingDialog();
//Check for errors
if(accountImageSettings == null){
if (accountImageSettings == null) {
Toast.makeText(getActivity(), R.string.err_get_account_image_settings,
Toast.LENGTH_SHORT).show();
return;
@ -119,17 +128,21 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
mAccountImageSettings = accountImageSettings;
//Apply settings
((AccountImagePreference)findPreference(PREFERENCE_UPDATE_ACCOUNT_IMAGE))
((AccountImagePreference) findPreference(PREFERENCE_UPDATE_ACCOUNT_IMAGE))
.setImage_url(mAccountImageSettings.getImageURL());
findPreference(PREFERENCE_DELETE_ACCOUNT_IMAGE).setEnabled(
accountImageSettings.isHas_image());
findPreference(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY)
.setEnabled(accountImageSettings.isHas_image());
((AccountImageVisibilityPreference) findPreference(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY))
.setValue(mAccountImageSettings.getVisibility());
}
@Override
public boolean onPreferenceClick(Preference preference) {
switch (preference.getKey()){
switch (preference.getKey()) {
//Upload new account image
case PREFERENCE_UPDATE_ACCOUNT_IMAGE:
@ -138,27 +151,40 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
//Delete account image
case PREFERENCE_DELETE_ACCOUNT_IMAGE:
confirmDeleteAccountImage();
break;
confirmDeleteAccountImage();
break;
}
return false;
}
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
//Update account image visibility
if (preference.getKey().equals(PREFERENCE_ACCOUNT_IMAGE_VISIBILITY)) {
updateAccountImageVisibility(
AccountImageVisibilityPreference.StringToAccountImageVisibility((String)o));
return true;
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Check if the request was to choose a new account image
if(requestCode == ACCOUNT_IMAGE_SETTINGS_PICK_NEW_INTENT)
if (requestCode == ACCOUNT_IMAGE_SETTINGS_PICK_NEW_INTENT)
pickNewAccountImageCallback(resultCode, data);
}
/**
* Prompt the user to choose a new account image
*/
private void pickNewAccountImage(){
private void pickNewAccountImage() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, ACCOUNT_IMAGE_SETTINGS_PICK_NEW_INTENT);
@ -168,10 +194,10 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
* Pick new account image callback
*
* @param resultCode Result code of the operation
* @param data Associated data
* @param data Associated data
*/
private void pickNewAccountImageCallback(int resultCode, Intent data){
if(resultCode != Activity.RESULT_OK)
private void pickNewAccountImageCallback(int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
try {
@ -187,7 +213,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
*
* @param bitmap The new account image, as bitmap
*/
private void uploadNewAccountImage(Bitmap bitmap){
private void uploadNewAccountImage(Bitmap bitmap) {
showLoadingDialog();
unset_loading_tasks();
@ -195,7 +221,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
mUploadNewAccountImageTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<Boolean>() {
@Override
public void OnPostExecute(Boolean result) {
if(!result)
if (!result)
Toast.makeText(getActivity(), R.string.err_upload_account_image,
Toast.LENGTH_SHORT).show();
@ -208,7 +234,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
/**
* Prompt user confirmation to delete account image
*/
private void confirmDeleteAccountImage(){
private void confirmDeleteAccountImage() {
new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_delete_accountimage_title)
.setMessage(R.string.dialog_delete_accountimage_message)
@ -227,7 +253,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
/**
* Perform account image deletion
*/
private void deleteAccountImage(){
private void deleteAccountImage() {
showLoadingDialog();
unset_loading_tasks();
@ -236,7 +262,7 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
@Override
public void OnPostExecute(Boolean result) {
if(!result)
if (!result)
Toast.makeText(getActivity(), R.string.err_delete_account_image,
Toast.LENGTH_SHORT).show();
@ -245,4 +271,29 @@ public class AccountImageSettingsFragment extends BaseAccountSettingsFragment im
});
mDeleteUserAccountImageTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/**
* Update account image visibility level
*
* @param visibility New Visibility level
*/
private void updateAccountImageVisibility(AccountImageVisibility visibility) {
unset_loading_tasks();
showLoadingDialog();
mUpdateAccountImageVisibilityTask = new UpdateAccountImageVisibilityTask(getActivity());
mUpdateAccountImageVisibilityTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener<Boolean>() {
@Override
public void OnPostExecute(Boolean result) {
if (!result)
Toast.makeText(getActivity(), R.string.err_update_account_image_visibility,
Toast.LENGTH_SHORT).show();
load_settings();
}
});
mUpdateAccountImageVisibilityTask.executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, visibility);
}
}

View File

@ -0,0 +1,91 @@
package org.communiquons.android.comunic.client.ui.preference;
import android.content.Context;
import android.support.v7.preference.ListPreference;
import android.util.AttributeSet;
import org.communiquons.android.comunic.client.data.enums.AccountImageVisibility;
/**
* Account image visibility preference
*/
public class AccountImageVisibilityPreference extends ListPreference {
public AccountImageVisibilityPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public AccountImageVisibilityPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public AccountImageVisibilityPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AccountImageVisibilityPreference(Context context) {
super(context);
}
/**
* Set new selected value
*
* @param visibility Visibility level
*/
public void setValue(AccountImageVisibility visibility) {
setValue(AccountImageVisibilityToString(visibility));
}
/**
* Get the currently selected visibility
*
* @return Currently selected visibility level
*/
public AccountImageVisibility getVisibility() {
return StringToAccountImageVisibility(getValue());
}
/**
* Turn an AccountImageVisibility entry into a string
*
* @param visibility The visibility to convert
* @return Generated string
*/
private static String AccountImageVisibilityToString(AccountImageVisibility visibility) {
switch (visibility) {
case OPEN:
return "open";
case PUBLIC:
return "public";
case FRIENDS:
return "friends";
default:
throw new AssertionError();
}
}
/**
* Turn a string into an account image visibility level
*
* @param string The string to convert
* @return Matching account visibility level
*/
public static AccountImageVisibility StringToAccountImageVisibility(String string) {
switch (string) {
case "open":
return AccountImageVisibility.OPEN;
case "public":
return AccountImageVisibility.PUBLIC;
case "friends":
return AccountImageVisibility.FRIENDS;
default:
throw new AssertionError();
}
}
}

View File

@ -284,4 +284,9 @@
<string name="dialog_delete_accountimage_confirm">Supprimer</string>
<string name="err_delete_account_image">Une erreur a survenu lors de la suppression de votre image de compte !</string>
<string name="err_upload_account_image">Une erreur a survenu lors de la mise en ligne de votre nouvelle image de compte !</string>
<string name="preference_account_image_visibility_level">Niveau de visibilité de l\'image de compte</string>
<string name="array_everyone">Tout le monde</string>
<string name="array_authenticated_users">Les utilisateurs connectés à Comunic</string>
<string name="array_friends_only">Uniquement mes amis</string>
<string name="err_update_account_image_visibility">Une erreur a survenu lors de la mise à jour du niveau de visibilité de l\'image de compte !</string>
</resources>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Account image visibility levels -->
<string-array name="account_image_visibility_levels_entries">
<item>@string/array_everyone</item>
<item>@string/array_authenticated_users</item>
<item>@string/array_friends_only</item>
</string-array>
<string-array name="account_image_visibility_levels_values">
<item>open</item>
<item>public</item>
<item>friends</item>
</string-array>
</resources>

View File

@ -283,4 +283,9 @@
<string name="dialog_delete_accountimage_confirm">Delete</string>
<string name="err_delete_account_image">Could not delete your account image!</string>
<string name="err_upload_account_image">Could not upload new account image!</string>
<string name="preference_account_image_visibility_level">Account image visibility level</string>
<string name="array_everyone">Everyone</string>
<string name="array_authenticated_users">Authenticated users</string>
<string name="array_friends_only">Friends only</string>
<string name="err_update_account_image_visibility">Could not update account image visibility level!</string>
</resources>

View File

@ -8,5 +8,11 @@
android:key="delete_account_image"
android:title="@string/action_delete_account_image" />
<org.communiquons.android.comunic.client.ui.preference.AccountImageVisibilityPreference
android:entries="@array/account_image_visibility_levels_entries"
android:entryValues="@array/account_image_visibility_levels_values"
android:key="account_image_visibility_level"
android:persistent="false"
android:title="@string/preference_account_image_visibility_level" />
</PreferenceScreen>