Profile editing

This commit is contained in:
Grishka
2022-02-10 17:59:47 +03:00
parent b8e3426a1e
commit 82a8d0cc29
24 changed files with 718 additions and 42 deletions

View File

@@ -45,10 +45,16 @@ public class ContentUriRequestBody extends RequestBody{
@Override
public void writeTo(BufferedSink sink) throws IOException{
try(Source source=Okio.source(MastodonApp.context.getContentResolver().openInputStream(uri))){
BufferedSink wrappedSink=Okio.buffer(new CountingSink(sink));
wrappedSink.writeAll(source);
wrappedSink.flush();
if(progressListener!=null){
try(Source source=Okio.source(MastodonApp.context.getContentResolver().openInputStream(uri))){
BufferedSink wrappedSink=Okio.buffer(new CountingSink(sink));
wrappedSink.writeAll(source);
wrappedSink.flush();
}
}else{
try(Source source=Okio.source(MastodonApp.context.getContentResolver().openInputStream(uri))){
sink.writeAll(source);
}
}
}

View File

@@ -1,5 +1,8 @@
package org.joinmastodon.android.api;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.util.Pair;
@@ -17,6 +20,7 @@ import java.util.List;
import java.util.Map;
import androidx.annotation.CallSuper;
import androidx.annotation.StringRes;
import me.grishka.appkit.api.APIRequest;
import me.grishka.appkit.api.Callback;
import okhttp3.Call;
@@ -36,6 +40,7 @@ public abstract class MastodonAPIRequest<T> extends APIRequest<T>{
Token token;
boolean canceled;
Map<String, String> headers;
private ProgressDialog progressDialog;
public MastodonAPIRequest(HttpMethod method, String path, Class<T> respClass){
this.path=path;
@@ -82,6 +87,17 @@ public abstract class MastodonAPIRequest<T> extends APIRequest<T>{
return this;
}
public MastodonAPIRequest<T> wrapProgress(Activity activity, @StringRes int message, boolean cancelable){
progressDialog=new ProgressDialog(activity);
progressDialog.setMessage(activity.getString(message));
progressDialog.setCancelable(cancelable);
if(cancelable){
progressDialog.setOnCancelListener(dialog->cancel());
}
progressDialog.show();
return this;
}
protected void setRequestBody(Object body){
requestBody=body;
}
@@ -149,10 +165,18 @@ public abstract class MastodonAPIRequest<T> extends APIRequest<T>{
invokeSuccessCallback(resp);
}
@Override
protected void onRequestDone(){
if(progressDialog!=null){
progressDialog.dismiss();
}
}
public enum HttpMethod{
GET,
POST,
PUT,
DELETE
DELETE,
PATCH
}
}

View File

@@ -0,0 +1,55 @@
package org.joinmastodon.android.api.requests.accounts;
import android.net.Uri;
import org.joinmastodon.android.api.ContentUriRequestBody;
import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.AccountField;
import org.joinmastodon.android.ui.utils.UiUtils;
import java.util.List;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
public class UpdateAccountCredentials extends MastodonAPIRequest<Account>{
private String displayName, bio;
private Uri avatar, cover;
private List<AccountField> fields;
public UpdateAccountCredentials(String displayName, String bio, Uri avatar, Uri cover, List<AccountField> fields){
super(HttpMethod.PATCH, "/accounts/update_credentials", Account.class);
this.displayName=displayName;
this.bio=bio;
this.avatar=avatar;
this.cover=cover;
this.fields=fields;
}
@Override
public RequestBody getRequestBody(){
MultipartBody.Builder bldr=new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("display_name", displayName)
.addFormDataPart("note", bio);
if(avatar!=null){
bldr.addFormDataPart("avatar", UiUtils.getFileName(avatar), new ContentUriRequestBody(avatar, null));
}
if(cover!=null){
bldr.addFormDataPart("header", UiUtils.getFileName(cover), new ContentUriRequestBody(cover, null));
}
if(fields.isEmpty()){
bldr.addFormDataPart("fields_attributes[0][name]", "").addFormDataPart("fields_attributes[0][value]", "");
}else{
int i=0;
for(AccountField field:fields){
bldr.addFormDataPart("fields_attributes["+i+"][name]", field.name).addFormDataPart("fields_attributes["+i+"][value]", field.value);
i++;
}
}
return bldr.build();
}
}

View File

@@ -9,6 +9,7 @@ import org.joinmastodon.android.api.ContentUriRequestBody;
import org.joinmastodon.android.api.MastodonAPIRequest;
import org.joinmastodon.android.api.ProgressListener;
import org.joinmastodon.android.model.Attachment;
import org.joinmastodon.android.ui.utils.UiUtils;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
@@ -29,16 +30,9 @@ public class UploadAttachment extends MastodonAPIRequest<Attachment>{
@Override
public RequestBody getRequestBody(){
String fileName;
try(Cursor cursor=MastodonApp.context.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)){
cursor.moveToFirst();
fileName=cursor.getString(0);
}
if(fileName==null)
fileName=uri.getLastPathSegment();
return new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", fileName, new ContentUriRequestBody(uri, progressListener))
.addFormDataPart("file", UiUtils.getFileName(uri), new ContentUriRequestBody(uri, progressListener))
.build();
}
}

View File

@@ -311,6 +311,13 @@ public class AccountSessionManager{
return r==null ? Collections.emptyList() : r;
}
public void updateAccountInfo(String id, Account account){
AccountSession session=getAccount(id);
session.self=account;
session.infoLastUpdated=System.currentTimeMillis();
writeAccountsFile();
}
private static class SessionsStorageWrapper{
public List<AccountSession> accounts;
}