mirror of https://github.com/tasks/tasks
Convert AddAttachmentDialog to Kotlin
parent
65e8f90098
commit
81904e5888
@ -1,85 +0,0 @@
|
|||||||
package org.tasks.dialogs;
|
|
||||||
|
|
||||||
import static org.tasks.dialogs.RecordAudioDialog.newRecordAudioDialog;
|
|
||||||
import static org.tasks.files.FileHelper.newFilePickerIntent;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.Dialog;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.provider.MediaStore.Images.Media;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.fragment.app.DialogFragment;
|
|
||||||
import com.todoroo.astrid.files.FilesControlSet;
|
|
||||||
import dagger.hilt.android.AndroidEntryPoint;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import org.tasks.R;
|
|
||||||
import org.tasks.activities.CameraActivity;
|
|
||||||
import org.tasks.preferences.Device;
|
|
||||||
|
|
||||||
@AndroidEntryPoint
|
|
||||||
public class AddAttachmentDialog extends DialogFragment {
|
|
||||||
|
|
||||||
public static final int REQUEST_CAMERA = 12120;
|
|
||||||
public static final int REQUEST_GALLERY = 12121;
|
|
||||||
public static final int REQUEST_STORAGE = 12122;
|
|
||||||
public static final int REQUEST_AUDIO = 12123;
|
|
||||||
private static final String FRAG_TAG_RECORD_AUDIO = "frag_tag_record_audio";
|
|
||||||
@Inject Activity context;
|
|
||||||
@Inject DialogBuilder dialogBuilder;
|
|
||||||
@Inject Device device;
|
|
||||||
|
|
||||||
public static AddAttachmentDialog newAddAttachmentDialog(FilesControlSet target) {
|
|
||||||
AddAttachmentDialog dialog = new AddAttachmentDialog();
|
|
||||||
dialog.setTargetFragment(target, 0);
|
|
||||||
return dialog;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
||||||
List<String> entries = new ArrayList<>();
|
|
||||||
final List<Runnable> actions = new ArrayList<>();
|
|
||||||
if (device.hasCamera()) {
|
|
||||||
entries.add(getString(R.string.take_a_picture));
|
|
||||||
actions.add(this::takePicture);
|
|
||||||
}
|
|
||||||
entries.add(getString(R.string.premium_record_audio));
|
|
||||||
actions.add(this::recordNote);
|
|
||||||
if (device.hasGallery()) {
|
|
||||||
entries.add(getString(R.string.pick_from_gallery));
|
|
||||||
actions.add(this::pickFromGallery);
|
|
||||||
}
|
|
||||||
entries.add(getString(R.string.pick_from_storage));
|
|
||||||
actions.add(this::pickFromStorage);
|
|
||||||
return dialogBuilder
|
|
||||||
.newDialog()
|
|
||||||
.setItems(entries, (dialog, which) -> actions.get(which).run())
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void takePicture() {
|
|
||||||
getTargetFragment()
|
|
||||||
.startActivityForResult(new Intent(context, CameraActivity.class), REQUEST_CAMERA);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void recordNote() {
|
|
||||||
newRecordAudioDialog((FilesControlSet) getTargetFragment(), REQUEST_AUDIO)
|
|
||||||
.show(getParentFragmentManager(), FRAG_TAG_RECORD_AUDIO);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void pickFromGallery() {
|
|
||||||
Intent intent = new Intent(Intent.ACTION_PICK);
|
|
||||||
intent.setDataAndType(Media.EXTERNAL_CONTENT_URI, "image/*");
|
|
||||||
if (intent.resolveActivity(context.getPackageManager()) != null) {
|
|
||||||
getTargetFragment().startActivityForResult(intent, REQUEST_GALLERY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void pickFromStorage() {
|
|
||||||
getTargetFragment()
|
|
||||||
.startActivityForResult(newFilePickerIntent(getActivity(), null), REQUEST_STORAGE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,82 @@
|
|||||||
|
package org.tasks.dialogs
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.app.Dialog
|
||||||
|
import android.content.Intent
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.provider.MediaStore
|
||||||
|
import androidx.fragment.app.DialogFragment
|
||||||
|
import com.todoroo.astrid.files.FilesControlSet
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.activities.CameraActivity
|
||||||
|
import org.tasks.files.FileHelper.newFilePickerIntent
|
||||||
|
import org.tasks.preferences.Device
|
||||||
|
import java.util.*
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class AddAttachmentDialog : DialogFragment() {
|
||||||
|
@Inject lateinit var context: Activity
|
||||||
|
@Inject lateinit var dialogBuilder: DialogBuilder
|
||||||
|
@Inject lateinit var device: Device
|
||||||
|
|
||||||
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
|
val entries: MutableList<String> = ArrayList()
|
||||||
|
val actions: MutableList<Runnable> = ArrayList()
|
||||||
|
if (device.hasCamera()) {
|
||||||
|
entries.add(getString(R.string.take_a_picture))
|
||||||
|
actions.add(Runnable { takePicture() })
|
||||||
|
}
|
||||||
|
entries.add(getString(R.string.premium_record_audio))
|
||||||
|
actions.add(Runnable { recordNote() })
|
||||||
|
if (device.hasGallery()) {
|
||||||
|
entries.add(getString(R.string.pick_from_gallery))
|
||||||
|
actions.add(Runnable { pickFromGallery() })
|
||||||
|
}
|
||||||
|
entries.add(getString(R.string.pick_from_storage))
|
||||||
|
actions.add(Runnable { pickFromStorage() })
|
||||||
|
return dialogBuilder
|
||||||
|
.newDialog()
|
||||||
|
.setItems(entries) { _, which -> actions[which].run() }
|
||||||
|
.show()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun takePicture() {
|
||||||
|
targetFragment?.startActivityForResult(
|
||||||
|
Intent(context, CameraActivity::class.java),
|
||||||
|
REQUEST_CAMERA
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun recordNote() {
|
||||||
|
RecordAudioDialog.newRecordAudioDialog(targetFragment as FilesControlSet?, REQUEST_AUDIO)
|
||||||
|
.show(parentFragmentManager, FRAG_TAG_RECORD_AUDIO)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun pickFromGallery() {
|
||||||
|
val intent = Intent(Intent.ACTION_PICK)
|
||||||
|
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
|
||||||
|
if (intent.resolveActivity(context.packageManager) != null) {
|
||||||
|
targetFragment?.startActivityForResult(intent, REQUEST_GALLERY)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun pickFromStorage() {
|
||||||
|
targetFragment?.startActivityForResult(newFilePickerIntent(activity, null), REQUEST_STORAGE)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val REQUEST_CAMERA = 12120
|
||||||
|
const val REQUEST_GALLERY = 12121
|
||||||
|
const val REQUEST_STORAGE = 12122
|
||||||
|
const val REQUEST_AUDIO = 12123
|
||||||
|
private const val FRAG_TAG_RECORD_AUDIO = "frag_tag_record_audio"
|
||||||
|
|
||||||
|
fun newAddAttachmentDialog(target: FilesControlSet?): AddAttachmentDialog {
|
||||||
|
val dialog = AddAttachmentDialog()
|
||||||
|
dialog.setTargetFragment(target, 0)
|
||||||
|
return dialog
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue