From 4f4dc821d4daaf332c16d970256999c9532fcb3c Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 16 Jul 2021 08:40:39 +0800 Subject: Implement SelectOrCaptureImageBottomSheetFragment. --- .../activities/PostGalleryActivity.java | 28 +++++++++++- .../activities/PostImageActivity.java | 19 ++++---- .../SelectOrCaptureImageBottomSheetFragment.java | 52 +++++++++++++++++++++ .../ic_outline_add_a_photo_day_night_24dp.xml | 9 ++++ .../ic_outline_select_photo_day_night_24dp.xml | 9 ++++ .../ic_outline_add_a_photo_day_night_24dp.xml | 9 ++++ .../ic_outline_select_photo_day_night_24dp.xml | 9 ++++ ...agment_select_or_capture_image_bottom_sheet.xml | 53 ++++++++++++++++++++++ .../fragment_uploaded_images_bottom_sheet.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 10 files changed, 179 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SelectOrCaptureImageBottomSheetFragment.java create mode 100644 app/src/main/res/drawable-night/ic_outline_add_a_photo_day_night_24dp.xml create mode 100644 app/src/main/res/drawable-night/ic_outline_select_photo_day_night_24dp.xml create mode 100644 app/src/main/res/drawable/ic_outline_add_a_photo_day_night_24dp.xml create mode 100644 app/src/main/res/drawable/ic_outline_select_photo_day_night_24dp.xml create mode 100644 app/src/main/res/layout/fragment_select_or_capture_image_bottom_sheet.xml (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java index 50786e1b..9b80db16 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -1,12 +1,15 @@ package ml.docilealligator.infinityforreddit.activities; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.Environment; import android.os.Handler; +import android.provider.MediaStore; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -19,6 +22,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.content.ContextCompat; +import androidx.core.content.FileProvider; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; @@ -34,6 +38,8 @@ import com.libRG.CustomTextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.Executor; @@ -50,6 +56,7 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.RedditGallerySubmissionRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.asynctasks.LoadSubredditIcon; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.SelectOrCaptureImageBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.services.SubmitPostService; @@ -194,7 +201,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee adapter = new RedditGallerySubmissionRecyclerViewAdapter(this, mCustomThemeWrapper, new RedditGallerySubmissionRecyclerViewAdapter.ItemClickListener() { @Override public void onAddImageClicked() { - + SelectOrCaptureImageBottomSheetFragment fragment = new SelectOrCaptureImageBottomSheetFragment(); + fragment.show(getSupportFragmentManager(), fragment.getTag()); } }); imagesRecyclerView.setAdapter(adapter); @@ -369,6 +377,24 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee titleEditText.setHintTextColor(secondaryTextColor); } + public void selectImage() { + + } + + public void captureImage() { + Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + try { + captureImageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider", + File.createTempFile("temp_img", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES))); + pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, captureImageUri); + startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE); + } catch (IOException ex) { + Snackbar.make(coordinatorLayout, R.string.error_creating_temp_file, Snackbar.LENGTH_SHORT).show(); + } catch (ActivityNotFoundException e) { + Snackbar.make(coordinatorLayout, R.string.no_camera_available, Snackbar.LENGTH_SHORT).show(); + } + } + private void displaySubredditIcon() { if (iconUrl != null && !iconUrl.equals("")) { mGlide.load(iconUrl) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java index a1b0281c..573617f6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java @@ -1,5 +1,6 @@ package ml.docilealligator.infinityforreddit.activities; +import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Resources; @@ -346,16 +347,14 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF captureFab.setOnClickListener(view -> { Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); - if (pictureIntent.resolveActivity(getPackageManager()) != null) { - try { - imageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider", - File.createTempFile("temp_img", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES))); - pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); - startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE); - } catch (IOException ex) { - Snackbar.make(coordinatorLayout, R.string.error_creating_temp_file, Snackbar.LENGTH_SHORT).show(); - } - } else { + try { + imageUri = FileProvider.getUriForFile(this, "ml.docilealligator.infinityforreddit.provider", + File.createTempFile("temp_img", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES))); + pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); + startActivityForResult(pictureIntent, CAPTURE_IMAGE_REQUEST_CODE); + } catch (IOException ex) { + Snackbar.make(coordinatorLayout, R.string.error_creating_temp_file, Snackbar.LENGTH_SHORT).show(); + } catch (ActivityNotFoundException e) { Snackbar.make(coordinatorLayout, R.string.no_camera_available, Snackbar.LENGTH_SHORT).show(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SelectOrCaptureImageBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SelectOrCaptureImageBottomSheetFragment.java new file mode 100644 index 00000000..4be9b51f --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/SelectOrCaptureImageBottomSheetFragment.java @@ -0,0 +1,52 @@ +package ml.docilealligator.infinityforreddit.bottomsheetfragments; + +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; + +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.activities.PostGalleryActivity; + +public class SelectOrCaptureImageBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + private PostGalleryActivity mActivity; + + public SelectOrCaptureImageBottomSheetFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_select_or_capture_image_bottom_sheet, container, false); + + TextView selectImageTextView = rootView.findViewById(R.id.select_image_text_view_select_or_capture_image_bottom_sheet_fragment); + TextView captureImageTextView = rootView.findViewById(R.id.capture_image_text_view_select_or_capture_image_bottom_sheet_fragment); + + selectImageTextView.setOnClickListener(view -> { + mActivity.selectImage(); + dismiss(); + }); + + captureImageTextView.setOnClickListener(view -> { + mActivity.captureImage(); + dismiss(); + }); + + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + mActivity = (PostGalleryActivity) context; + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-night/ic_outline_add_a_photo_day_night_24dp.xml b/app/src/main/res/drawable-night/ic_outline_add_a_photo_day_night_24dp.xml new file mode 100644 index 00000000..57869fc5 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_outline_add_a_photo_day_night_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-night/ic_outline_select_photo_day_night_24dp.xml b/app/src/main/res/drawable-night/ic_outline_select_photo_day_night_24dp.xml new file mode 100644 index 00000000..136ace9f --- /dev/null +++ b/app/src/main/res/drawable-night/ic_outline_select_photo_day_night_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_outline_add_a_photo_day_night_24dp.xml b/app/src/main/res/drawable/ic_outline_add_a_photo_day_night_24dp.xml new file mode 100644 index 00000000..98c1ac80 --- /dev/null +++ b/app/src/main/res/drawable/ic_outline_add_a_photo_day_night_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_outline_select_photo_day_night_24dp.xml b/app/src/main/res/drawable/ic_outline_select_photo_day_night_24dp.xml new file mode 100644 index 00000000..9551f621 --- /dev/null +++ b/app/src/main/res/drawable/ic_outline_select_photo_day_night_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_select_or_capture_image_bottom_sheet.xml b/app/src/main/res/layout/fragment_select_or_capture_image_bottom_sheet.xml new file mode 100644 index 00000000..55f60691 --- /dev/null +++ b/app/src/main/res/layout/fragment_select_or_capture_image_bottom_sheet.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_uploaded_images_bottom_sheet.xml b/app/src/main/res/layout/fragment_uploaded_images_bottom_sheet.xml index 79b6fa82..f3e96ef5 100644 --- a/app/src/main/res/layout/fragment_uploaded_images_bottom_sheet.xml +++ b/app/src/main/res/layout/fragment_uploaded_images_bottom_sheet.xml @@ -40,7 +40,7 @@ android:layout_margin="16dp" android:backgroundTint="@color/colorPrimary" android:textColor="#FFFFFF" - android:text="@string/upload" /> + android:text="@string/select_image" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 58fe7e6b..3742cc52 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1125,7 +1125,7 @@ Reply Uploaded Images - Upload + Select an Image Capture Uploading Upload image successfully. Click the image button again to see the uploaded images. -- cgit v1.2.3