Allow to edit custom fields in profile

This commit is contained in:
stom79 2018-08-18 17:37:53 +02:00
parent ce3ac046a8
commit 91db484928
5 changed files with 169 additions and 13 deletions

View File

@ -62,6 +62,9 @@ import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountInfoAsyncTask;
@ -105,6 +108,7 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_HEADER = 754;
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE_PICTURE = 755;
private String avatarName, headerName;
private HashMap<String, String> fields;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -262,6 +266,48 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
}
}
});
if ( account.getFields() != null && account.getFields().size() > 0){
fields = account.getFields();
Iterator it = fields.entrySet().iterator();
int i = 1;
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
String label = (String)pair.getKey();
String value = (String)pair.getValue();
EditText labelView;
EditText valueView;
switch(i){
case 1:
labelView = findViewById(R.id.cf_key_1);
valueView = findViewById(R.id.cf_val_1);
break;
case 2:
labelView = findViewById(R.id.cf_key_2);
valueView = findViewById(R.id.cf_val_2);
break;
case 3:
labelView = findViewById(R.id.cf_key_3);
valueView = findViewById(R.id.cf_val_3);
break;
case 4:
labelView = findViewById(R.id.cf_key_4);
valueView = findViewById(R.id.cf_val_4);
break;
default:
labelView = findViewById(R.id.cf_key_1);
valueView = findViewById(R.id.cf_val_1);
break;
}
labelView.setText(label);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
value = Html.fromHtml(value, Html.FROM_HTML_MODE_LEGACY).toString();
else
//noinspection deprecation
value = Html.fromHtml(value).toString();
valueView.setText(value);
i++;
}
}
set_profile_name.addTextChangedListener(new TextWatcher() {
@Override
@ -411,8 +457,25 @@ public class EditProfileActivity extends BaseActivity implements OnRetrieveAccou
}
}).start();
GlideApp.get(getApplicationContext()).clearMemory();
HashMap<String, String> newCustomFields = new HashMap<>();
new UpdateCredentialAsyncTask(getApplicationContext(), profile_username, profile_note, profile_picture, avatarName, header_picture, headerName, profile_privacy, EditProfileActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
String key1, key2, key3, key4, val1, val2, val3, val4;
key1 = ((EditText)findViewById(R.id.cf_key_1)).getText().toString();
key2 = ((EditText)findViewById(R.id.cf_key_2)).getText().toString();
key3 = ((EditText)findViewById(R.id.cf_key_3)).getText().toString();
key4 = ((EditText)findViewById(R.id.cf_key_4)).getText().toString();
val1 = ((EditText)findViewById(R.id.cf_val_1)).getText().toString();
val2 = ((EditText)findViewById(R.id.cf_val_2)).getText().toString();
val3 = ((EditText)findViewById(R.id.cf_val_3)).getText().toString();
val4 = ((EditText)findViewById(R.id.cf_val_4)).getText().toString();
newCustomFields.put(key1,val1);
newCustomFields.put(key2,val2);
newCustomFields.put(key3,val3);
newCustomFields.put(key4,val4);
new UpdateCredentialAsyncTask(getApplicationContext(), newCustomFields, profile_username, profile_note, profile_picture, avatarName, header_picture, headerName, profile_privacy, EditProfileActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
dialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

View File

@ -20,6 +20,7 @@ import android.os.AsyncTask;
import java.io.ByteArrayInputStream;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
@ -38,8 +39,9 @@ public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
private APIResponse apiResponse;
private OnUpdateCredentialInterface listener;
private WeakReference<Context> contextReference;
private HashMap<String, String> customFields;
public UpdateCredentialAsyncTask(Context context, String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
public UpdateCredentialAsyncTask(Context context, HashMap<String, String> customFields, String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, API.accountPrivacy privacy, OnUpdateCredentialInterface onUpdateCredentialInterface){
this.contextReference = new WeakReference<>(context);
this.display_name = display_name;
this.note = note;
@ -49,11 +51,12 @@ public class UpdateCredentialAsyncTask extends AsyncTask<Void, Void, Void> {
this.privacy = privacy;
this.avatarName = avatarName;
this.headerName = headerName;
this.customFields = customFields;
}
@Override
protected Void doInBackground(Void... params) {
apiResponse = new API(this.contextReference.get()).updateCredential(display_name, note, avatar, avatarName, header, headerName, privacy);
apiResponse = new API(this.contextReference.get()).updateCredential(display_name, note, avatar, avatarName, header, headerName, privacy, customFields);
return null;
}

View File

@ -30,7 +30,9 @@ import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.client.Entities.*;
@ -152,7 +154,7 @@ public class API {
* Update credential of the authenticated user *synchronously*
* @return APIResponse
*/
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy) {
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy, HashMap<String, String> customFields) {
HashMap<String, String> requestParams = new HashMap<>();
if( display_name != null)
@ -169,7 +171,17 @@ public class API {
}
if( privacy != null)
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
int i = 0;
if( customFields != null && customFields.size() > 0){
Iterator it = customFields.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
requestParams.put("fields_attributes["+i+"][name]",(String)pair.getKey());
requestParams.put("fields_attributes["+i+"][value]",(String)pair.getValue());
it.remove();
i++;
}
}
try {
new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, avatar, avatarName, header, headerName, prefKeyOauthTokenT);
} catch (HttpsConnection.HttpsConnectionException e) {

View File

@ -19,6 +19,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.text.Html;
import android.text.SpannableString;
import com.google.common.io.ByteStreams;
import org.json.JSONObject;
import java.io.BufferedInputStream;
@ -961,7 +962,6 @@ public class HttpsConnection {
try {
httpsURLConnection.getInputStream().close();
}catch (Exception ignored){}
throw new HttpsConnectionException(responseCode, error);
}
httpsURLConnection.getInputStream().close();

View File

@ -111,13 +111,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<!-- Lock the account -->
<CheckBox
android:layout_marginTop="10dp"
android:id="@+id/set_lock_account"
android:layout_width="wrap_content"
android:text="@string/set_lock_account"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="5dp"
@ -130,6 +124,90 @@
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/cf_key_1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:id="@+id/cf_val_1"
android:inputType="text"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/cf_key_2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:id="@+id/cf_val_2"
android:inputType="text"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/cf_key_3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:id="@+id/cf_val_3"
android:inputType="text"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/cf_key_4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:id="@+id/cf_val_4"
android:inputType="text"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<!-- Lock the account -->
<CheckBox
android:layout_marginTop="10dp"
android:id="@+id/set_lock_account"
android:layout_width="wrap_content"
android:text="@string/set_lock_account"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="5dp"
android:id="@+id/set_profile_save"