Skip to content

Commit

Permalink
Support uploading profile pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
grishka committed Feb 21, 2021
1 parent 0dbd6e5 commit 72dda8f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Houseclub/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "me.grishka.houseclub"
minSdkVersion 24
targetSdkVersion 30
versionCode 3
versionName "1.0.2"
versionCode 4
versionName "1.0.3"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void run(){
uri.appendQueryParameter(e.getKey(), e.getValue());
}
}
req.prepare();
RequestBody reqBody=null;
if(req.requestBody!=null){
reqBody=RequestBody.create(MediaType.get("application/json; charset=utf-8"), gson.toJson(req.requestBody));
Expand Down Expand Up @@ -140,7 +141,7 @@ public void run(){
if(DEBUG)
Log.i(TAG, "Raw response: "+respStr);
// T robj=gson.fromJson(body.charStream(), req.responseClass);
T robj=gson.fromJson(respStr, req.responseClass);
T robj=req.parse(respStr);
if(DEBUG)
Log.i(TAG, "Parsed response: "+robj);
req.onSuccess(robj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.net.Uri;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

Expand Down Expand Up @@ -74,6 +75,14 @@ private void dismissProgressDialog(){
progress=null;
}

public void prepare() throws Exception{

}

public T parse(String resp) throws Exception{
return ClubhouseAPIController.getInstance().getGson().fromJson(resp, responseClass);
}

void onSuccess(T result){
if(progress!=null)
uiThreadHandler.post(this::dismissProgressDialog);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.grishka.houseclub.api.methods;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.Uri;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

import me.grishka.houseclub.App;
import me.grishka.houseclub.api.ClubhouseAPIRequest;

public class UpdatePhoto extends ClubhouseAPIRequest<Bitmap>{

private Uri uri;
private Bitmap resizedBitmap;

public UpdatePhoto(Uri fileUri){
super("POST", "update_photo", Bitmap.class);
uri=fileUri;
}

@Override
public void prepare() throws IOException{
Bitmap orig;
try(InputStream in=App.applicationContext.getContentResolver().openInputStream(uri)){
orig=BitmapFactory.decodeStream(in);
}
int size=Math.min(512, Math.min(orig.getWidth(), orig.getHeight()));
resizedBitmap=Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Rect srcRect;
if(orig.getWidth()>orig.getHeight()){
int x=(orig.getWidth()-orig.getHeight())/2;
srcRect=new Rect(x, 0, x+orig.getHeight(), orig.getHeight());
}else{
int y=(orig.getHeight()-orig.getWidth())/2;
srcRect=new Rect(0, y, orig.getWidth(), y+orig.getWidth());
}
new Canvas(resizedBitmap).drawBitmap(orig, srcRect, new Rect(0, 0, size, size), new Paint(Paint.FILTER_BITMAP_FLAG));
File tmp=new File(App.applicationContext.getCacheDir(), "ava_tmp.jpg");
try(FileOutputStream out=new FileOutputStream(tmp)){
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
}
fileToUpload=tmp;
fileFieldName="file";
fileMimeType="image/jpeg";
}

@Override
public Bitmap parse(String resp) throws Exception{
return resizedBitmap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
Expand Down Expand Up @@ -38,10 +39,13 @@
import me.grishka.houseclub.api.methods.GetProfile;
import me.grishka.houseclub.api.methods.Unfollow;
import me.grishka.houseclub.api.methods.UpdateBio;
import me.grishka.houseclub.api.methods.UpdatePhoto;
import me.grishka.houseclub.api.model.FullUser;

public class ProfileFragment extends LoaderFragment{

private static final int PICK_PHOTO_RESULT=468;

private FullUser user;

private TextView name, username, followers, following, followsYou, bio, inviteInfo, twitter, instagram;
Expand Down Expand Up @@ -83,8 +87,10 @@ public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bu
followers.setOnClickListener(this::onFollowersClick);
following.setOnClickListener(this::onFollowingClick);
v.findViewById(R.id.inviter_btn).setOnClickListener(this::onInviterClick);
if(self)
if(self){
bio.setOnClickListener(this::onBioClick);
photo.setOnClickListener(this::onPhotoClick);
}

return v;
}
Expand Down Expand Up @@ -184,6 +190,26 @@ public boolean onOptionsItemSelected(MenuItem item){
return true;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode==PICK_PHOTO_RESULT && resultCode==Activity.RESULT_OK){
new UpdatePhoto(data.getData())
.wrapProgress(getActivity())
.setCallback(new Callback<Bitmap>(){
@Override
public void onSuccess(Bitmap result){
photo.setImageBitmap(result);
}

@Override
public void onError(ErrorResponse error){
error.showToast(getActivity());
}
})
.exec();
}
}

private void onFollowClick(View v){
if(user.isFollowed()){
new AlertDialog.Builder(getActivity())
Expand Down Expand Up @@ -295,4 +321,10 @@ public void onError(ErrorResponse error){
.setNegativeButton(R.string.cancel, null)
.show();
}

private void onPhotoClick(View v){
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_RESULT);
}
}

0 comments on commit 72dda8f

Please sign in to comment.