Skip to content

Commit

Permalink
#289 πŸ“Ž sources - Introduce bottom sheet dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-niedermann committed Jun 24, 2020
1 parent f828c71 commit f706cc1
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -41,6 +42,8 @@
import it.niedermann.nextcloud.deck.ui.branding.BrandedFragment;
import it.niedermann.nextcloud.deck.ui.branding.BrandedSnackbar;
import it.niedermann.nextcloud.deck.ui.card.EditCardViewModel;
import it.niedermann.nextcloud.deck.ui.card.attachments.picker.CardAttachmentPicker;
import it.niedermann.nextcloud.deck.ui.card.attachments.picker.CardAttachmentPickerListener;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionDialogFragment;

import static it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.LiveDataHelper.observeOnce;
Expand All @@ -50,13 +53,14 @@
import static it.niedermann.nextcloud.deck.util.AttachmentUtil.copyContentUriToTempFile;
import static java.net.HttpURLConnection.HTTP_CONFLICT;

public class CardAttachmentsFragment extends BrandedFragment implements AttachmentDeletedListener, AttachmentClickedListener {
public class CardAttachmentsFragment extends BrandedFragment implements AttachmentDeletedListener, AttachmentClickedListener, CardAttachmentPickerListener {

private FragmentCardEditTabAttachmentsBinding binding;
private EditCardViewModel viewModel;

private static final int REQUEST_CODE_ADD_ATTACHMENT = 1;
private static final int REQUEST_PERMISSION = 2;
private static final int REQUEST_CODE_ADD_FILE = 1;
private static final int REQUEST_CODE_ADD_FILE_PERMISSION = 2;
private static final int REQUEST_CODE_CAPTURE_IMAGE = 3;

private SyncManager syncManager;
private CardAttachmentAdapter adapter;
Expand Down Expand Up @@ -123,15 +127,8 @@ public void onMapSharedElements(List<String> names, Map<String, View> sharedElem
updateEmptyContentView();
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && viewModel.canEdit()) {
binding.fab.setOnClickListener(v -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSION);
} else {
startFilePickerIntent();
}
});
if (viewModel.canEdit()) {
binding.fab.setOnClickListener(v -> new CardAttachmentPicker().show(getChildFragmentManager(), CardAttachmentPicker.class.getSimpleName()));
binding.fab.show();
binding.attachmentsList.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
Expand All @@ -149,18 +146,10 @@ else if (dy < 0)
return binding.getRoot();
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void startFilePickerIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_CODE_ADD_ATTACHMENT);
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_ADD_ATTACHMENT && resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE_ADD_FILE && resultCode == Activity.RESULT_OK) {
if (data == null) {
ExceptionDialogFragment.newInstance(new UploadAttachmentFailedException("Intent data is null"), viewModel.getAccount()).show(getChildFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
return;
Expand Down Expand Up @@ -235,7 +224,7 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSION) {
if (requestCode == REQUEST_CODE_ADD_FILE_PERMISSION) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
startFilePickerIntent();
}
Expand Down Expand Up @@ -263,7 +252,6 @@ public void onAttachmentClicked(int position) {
this.clickedItemPosition = position;
}


private void updateEmptyContentView() {
if (this.adapter == null || this.adapter.getItemCount() == 0) {
this.binding.emptyContentView.setVisibility(View.VISIBLE);
Expand All @@ -278,4 +266,36 @@ private void updateEmptyContentView() {
public void applyBrand(int mainColor, int textColor) {
applyBrandToFAB(mainColor, textColor, binding.fab);
}

@Override
public void pickCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(requireContext().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_CODE_CAPTURE_IMAGE);
}
}

@Override
public void pickContact() {

}

@Override
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void pickFile() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_ADD_FILE_PERMISSION);
} else {
startFilePickerIntent();
}
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void startFilePickerIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_CODE_ADD_FILE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package it.niedermann.nextcloud.deck.ui.card.attachments.picker;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import it.niedermann.nextcloud.deck.databinding.DialogAttachmentPickerBinding;

public class CardAttachmentPicker extends BottomSheetDialogFragment {

private DialogAttachmentPickerBinding binding;
private CardAttachmentPickerListener listener;

public CardAttachmentPicker() {

}

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);

if (getParentFragment() instanceof CardAttachmentPickerListener) {
this.listener = (CardAttachmentPickerListener) getParentFragment();
} else {
throw new IllegalArgumentException("Caller must implement " + CardAttachmentPickerListener.class.getSimpleName());
}
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DialogAttachmentPickerBinding.inflate(inflater, container, false);
return binding.getRoot();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
binding.pickCamera.setOnClickListener((v) -> listener.pickCamera());
binding.pickContact.setOnClickListener((v) -> listener.pickContact());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
binding.pickFile.setOnClickListener((v) -> listener.pickFile());
} else {
binding.pickFile.setVisibility(View.GONE);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.niedermann.nextcloud.deck.ui.card.attachments.picker;

import android.os.Build;

import androidx.annotation.RequiresApi;

public interface CardAttachmentPickerListener {

void pickCamera();
void pickContact();
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
void pickFile();
}
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/ic_baseline_photo_camera_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#757575" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
<path android:fillColor="@android:color/white" android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>
76 changes: 76 additions & 0 deletions app/src/main/res/layout/dialog_attachment_picker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/spacer_3x"
app:behavior_hideable="false"
app:behavior_peekHeight="90dp"
app:justifyContent="space_around"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<LinearLayout
android:id="@+id/pickCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:contentDescription="@null"
android:src="@drawable/ic_baseline_photo_camera_24" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacer_1x"
android:text="@string/simple_camera"
android:textAppearance="?attr/textAppearanceCaption" />
</LinearLayout>

<LinearLayout
android:id="@+id/pickContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:contentDescription="@null"
android:src="@drawable/ic_baseline_contact_mail_24" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacer_1x"
android:text="@string/simple_contact"
android:textAppearance="?attr/textAppearanceCaption" />
</LinearLayout>

<LinearLayout
android:id="@+id/pickFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:layout_width="36dp"
android:layout_height="36dp"
android:contentDescription="@null"
android:src="@drawable/ic_attach_file_grey600_24dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacer_1x"
android:text="@string/simple_file"
android:textAppearance="?attr/textAppearanceCaption" />
</LinearLayout>
</com.google.android.flexbox.FlexboxLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,7 @@
<item quantity="other">%1$d errors while uploading</item>
</plurals>
<string name="simple_report">Report</string>
<string name="simple_contact">Contact</string>
<string name="simple_file">File</string>
<string name="simple_camera">Camera</string>
</resources>

0 comments on commit f706cc1

Please sign in to comment.