Skip to content

Commit

Permalink
Merge pull request #273 from FirstWithThisName/android-image-cropper-…
Browse files Browse the repository at this point in the history
…update

Migrated to android-image-cropper version 4.5.0
  • Loading branch information
brodeurlv authored Feb 24, 2024
2 parents fdf96e9 + 81ff734 commit 22eab01
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 250 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ dependencies {
implementation 'com.mikhaellopez:circularimageview:3.2.0'
implementation 'com.github.f0ris.sweetalert:library:1.6.2'
implementation 'com.github.onurkagan:ktoast:1.0.3'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
implementation 'com.vanniktech:android-image-cropper:4.5.0'
implementation 'com.github.ivbaranov:materialfavoritebutton:0.1.5'
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
android:name=".intro.MainIntroActivity"
android:theme="@style/Theme.Intro" />
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:name="com.canhub.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />

<meta-data
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/easyfitness/CustomDrawerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.easyfitness.utils.ImageUtil;
import com.mikhaellopez.circularimageview.CircularImageView;

Expand Down Expand Up @@ -107,9 +109,8 @@ public void onNothingSelected(AdapterView<?> arg0) {

//drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(dItem.getImgResID()));

ImageUtil imgUtil = new ImageUtil();
// Check if path is pointing to a thumb else create it and use it.
String thumbPath = imgUtil.getThumbPath(dItem.getImg());
String thumbPath = ImageUtil.getThumbPath(dItem.getImg());
if (thumbPath != null)
ImageUtil.setPic(drawerHolder.roundProfile, thumbPath);
else
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/com/easyfitness/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
Expand Down Expand Up @@ -883,10 +884,8 @@ public void setCurrentProfile(Profile newProfil) {
}

private void setPhotoProfile(String path) {
ImageUtil imgUtil = new ImageUtil();

// Check if path is pointing to a thumb else create it and use it.
String thumbPath = imgUtil.getThumbPath(path);
String thumbPath = ImageUtil.getThumbPath(path);
boolean success = false;
if (thumbPath != null) {
success = ImageUtil.setPic(roundProfile, thumbPath);
Expand Down
57 changes: 14 additions & 43 deletions app/src/main/java/com/easyfitness/ProfileFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class ProfileFragment extends Fragment {
private DAOBodyMeasure daoBodyMeasure = null;
private DAOBodyPart daoBodyPart = null;
private ImageUtil imgUtil = null;
private final OnClickListener onClickMachinePhoto = v -> CreatePhotoSourceDialog();
private final OnClickListener onClickMachinePhoto = v -> imgUtil.createPhotoSourceDialog();
private AppViMo appViMo;
private ProfileViMo profileViMo;
private final OnClickListener mOnClickListener = view -> {
Expand Down Expand Up @@ -123,11 +123,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,


/* Initialisation des valeurs */
imgUtil = new ImageUtil(roundProfile);
imgUtil = new ImageUtil(this, roundProfile);
// ImageView must be set in OnStart. Not in OnCreateView

/* Initialisation des boutons */

genderEdit.setCustomDialogBuilder(view1 -> {
SweetAlertDialog dlg = new SweetAlertDialog(view1.getContext(), SweetAlertDialog.NORMAL_TYPE)
.setTitleText(getContext().getString(R.string.edit_value))
Expand Down Expand Up @@ -190,6 +188,18 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
requestForSave();
});

imgUtil.setOnPicTakenListener(uriFilePath -> {
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
try {
File newFile = ImageUtil.moveFile(new File(uriFilePath), storageDir, "newfile.jpg");
ImageUtil.saveThumb(newFile.getAbsolutePath());
profileViMo.setPhoto(newFile.getAbsolutePath());
requestForSave();
} catch (Exception e) {
e.printStackTrace();
}
});

profileViMo = new ViewModelProvider(this).get(ProfileViMo.class);
profileViMo.getBirthday().observe(getViewLifecycleOwner(), birthday -> {
if (birthday.getTime() == 0) {
Expand Down Expand Up @@ -307,43 +317,4 @@ private void requestForSave() {

isSaving = false;
}

public Fragment getFragment() {
return this;
}

private boolean CreatePhotoSourceDialog() {
if (imgUtil == null)
imgUtil = new ImageUtil();

return imgUtil.CreatePhotoSourceDialog(this);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case ImageUtil.REQUEST_TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
profileViMo.setPhoto(imgUtil.getFilePath());
requestForSave();
imgUtil.saveThumb(imgUtil.getFilePath());
}
break;
case ImageUtil.REQUEST_PICK_GALERY_PHOTO:
if (resultCode == Activity.RESULT_OK) {
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Uri selectedImageUri = data.getData();
try {
File newFile = imgUtil.copyFileFromUri(getContext(), selectedImageUri, storageDir, "newfile.jpg");
imgUtil.saveThumb(newFile.getAbsolutePath());
profileViMo.setPhoto(newFile.getAbsolutePath());
requestForSave();
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
}
}
78 changes: 25 additions & 53 deletions app/src/main/java/com/easyfitness/ProgressImagesFragment.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.easyfitness;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -43,7 +40,7 @@ public class ProgressImagesFragment extends Fragment {
private int imageCount = 0;


private final View.OnClickListener onClickAddProgressImage = v -> createPhotoSourceDialog();
private final View.OnClickListener onClickAddProgressImage = v -> imgUtil.createPhotoSourceDialog();


/**
Expand All @@ -62,17 +59,6 @@ public static ProgressImagesFragment newInstance(String name, int id) {
return f;
}

private boolean createPhotoSourceDialog() {
if (imgUtil == null) {
imgUtil = new ImageUtil();
imgUtil.setOnDeleteImageListener(u -> {
deleteCurrentImage();
});
}

return imgUtil.CreatePhotoSourceDialog(this);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
Expand All @@ -91,6 +77,27 @@ public View onCreateView(@NonNull LayoutInflater inflater,
olderImageButton.setOnClickListener(v -> showOlderImage());
photoButton.setOnClickListener(onClickAddProgressImage);

imgUtil = new ImageUtil(this);
imgUtil.setOnDeleteImageListener(u -> {
deleteCurrentImage();
});
imgUtil.setOnPicTakenListener(uriFilePath -> {
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
try {
File newFile = ImageUtil.moveFile(new File(uriFilePath), storageDir, generateFileName());
daoProgressImage.addProgressImage(
new Date(System.currentTimeMillis()), // maybe user input
newFile,
appViMo.getProfile().getValue().getId()
);
imageOffset = 0;
showProgressImage();
updateImageNavigation();
} catch (Exception e) {
e.printStackTrace();
}
});

updateImageNavigation();
return view;
}
Expand All @@ -99,7 +106,7 @@ private void deleteCurrentImage() {
daoProgressImage.deleteImage(image.getId());
new File(image.getFile()).delete();

imageOffset = imageOffset >= (imageCount-1)? imageOffset-1 : imageOffset;
imageOffset = imageOffset >= (imageCount - 1) ? imageOffset - 1 : imageOffset;
updateImageNavigation();
}

Expand All @@ -116,15 +123,15 @@ private void showNewerImage() {
private void updateImageNavigation() {
showProgressImage();
imageCount = daoProgressImage.count(appViMo.getProfile().getValue().getId());
progressImageIndex.setText((imageOffset+1) + " / " + imageCount);
progressImageIndex.setText((imageOffset + 1) + " / " + imageCount);
if (image != null) {
progressImageDate.setText(
DateConverter.dateToLocalDateStr(image.getCreated(), getContext())
);
} else {
progressImageDate.setText(DateConverter.currentDate(getContext()));
}
if ((imageOffset+1) >= imageCount) {
if ((imageOffset + 1) >= imageCount) {
olderImageButton.setEnabled(false);
} else {
olderImageButton.setEnabled(true);
Expand Down Expand Up @@ -152,39 +159,4 @@ private String generateFileName() {
// TODO hard coded ending ?
return String.format("%10d.jpg", System.currentTimeMillis() / 1000);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case ImageUtil.REQUEST_TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
daoProgressImage.addProgressImage(
new Date(System.currentTimeMillis()), // maybe user input
new File(imgUtil.getFilePath()),
appViMo.getProfile().getValue().getId()
);
}
break;
case ImageUtil.REQUEST_PICK_GALERY_PHOTO:
if (resultCode == Activity.RESULT_OK) {
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Uri selectedImageUri = data.getData();
try {
File newFile = imgUtil.copyFileFromUri(getContext(), selectedImageUri, storageDir, generateFileName());
daoProgressImage.addProgressImage(
new Date(System.currentTimeMillis()), // maybe user input
newFile,
appViMo.getProfile().getValue().getId()
);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
imageOffset = 0;
showProgressImage();
updateImageNavigation();
}
}
Loading

0 comments on commit 22eab01

Please sign in to comment.