Compose design + media upload

This commit is contained in:
Grishka
2022-02-04 13:50:19 +03:00
parent 20d3a62747
commit cc06715aa6
24 changed files with 668 additions and 85 deletions

View File

@@ -0,0 +1,72 @@
package org.joinmastodon.android.api;
import android.database.Cursor;
import android.net.Uri;
import android.os.SystemClock;
import android.provider.OpenableColumns;
import org.joinmastodon.android.MastodonApp;
import org.joinmastodon.android.ui.utils.UiUtils;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;
import okio.Source;
public class ContentUriRequestBody extends RequestBody{
private final Uri uri;
private final long length;
private ProgressListener progressListener;
public ContentUriRequestBody(Uri uri, ProgressListener progressListener){
this.uri=uri;
this.progressListener=progressListener;
try(Cursor cursor=MastodonApp.context.getContentResolver().query(uri, new String[]{OpenableColumns.SIZE}, null, null, null)){
cursor.moveToFirst();
length=cursor.getInt(0);
}
}
@Override
public MediaType contentType(){
return MediaType.get(MastodonApp.context.getContentResolver().getType(uri));
}
@Override
public long contentLength() throws IOException{
return length;
}
@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();
}
}
private class CountingSink extends ForwardingSink{
private long bytesWritten=0;
private long lastCallbackTime;
public CountingSink(Sink delegate){
super(delegate);
}
@Override
public void write(Buffer source, long byteCount) throws IOException{
super.write(source, byteCount);
bytesWritten+=byteCount;
if(SystemClock.uptimeMillis()-lastCallbackTime>=100L || bytesWritten==length){
lastCallbackTime=SystemClock.uptimeMillis();
UiUtils.runOnUiThread(()->progressListener.onProgress(bytesWritten, length));
}
}
}
}

View File

@@ -0,0 +1,5 @@
package org.joinmastodon.android.api;
public interface ProgressListener{
void onProgress(long transferred, long total);
}

View File

@@ -0,0 +1,44 @@
package org.joinmastodon.android.api.requests.statuses;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import org.joinmastodon.android.MastodonApp;
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 okhttp3.MultipartBody;
import okhttp3.RequestBody;
public class UploadAttachment extends MastodonAPIRequest<Attachment>{
private Uri uri;
private ProgressListener progressListener;
public UploadAttachment(Uri uri){
super(HttpMethod.POST, "/media", Attachment.class);
this.uri=uri;
}
public UploadAttachment setProgressListener(ProgressListener progressListener){
this.progressListener=progressListener;
return this;
}
@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))
.build();
}
}