|
@@ -24,20 +24,19 @@
|
|
|
|
|
|
package com.owncloud.android.ui.dialog;
|
|
|
|
|
|
-import android.annotation.SuppressLint;
|
|
|
import android.app.Activity;
|
|
|
import android.app.Dialog;
|
|
|
import android.content.Context;
|
|
|
-import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
-import android.graphics.PorterDuff;
|
|
|
import android.os.AsyncTask;
|
|
|
import android.os.Bundle;
|
|
|
+import android.text.Editable;
|
|
|
+import android.text.TextWatcher;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.Window;
|
|
|
import android.view.WindowManager.LayoutParams;
|
|
|
-import android.widget.EditText;
|
|
|
+import android.widget.Button;
|
|
|
|
|
|
import com.nextcloud.android.lib.resources.directediting.DirectEditingCreateFileRemoteOperation;
|
|
|
import com.nextcloud.android.lib.resources.directediting.DirectEditingObtainListOfTemplatesRemoteOperation;
|
|
@@ -47,6 +46,7 @@ import com.nextcloud.client.di.Injectable;
|
|
|
import com.nextcloud.client.network.ClientFactory;
|
|
|
import com.owncloud.android.MainApp;
|
|
|
import com.owncloud.android.R;
|
|
|
+import com.owncloud.android.databinding.ChooseTemplateBinding;
|
|
|
import com.owncloud.android.datamodel.FileDataStorageManager;
|
|
|
import com.owncloud.android.datamodel.OCFile;
|
|
|
import com.owncloud.android.lib.common.Creator;
|
|
@@ -72,26 +72,27 @@ import androidx.annotation.NonNull;
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
import androidx.fragment.app.DialogFragment;
|
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
|
|
-import androidx.recyclerview.widget.RecyclerView;
|
|
|
-import butterknife.BindView;
|
|
|
-import butterknife.ButterKnife;
|
|
|
|
|
|
/**
|
|
|
* Dialog to show templates for new documents/spreadsheets/presentations.
|
|
|
*/
|
|
|
-public class ChooseTemplateDialogFragment extends DialogFragment implements DialogInterface.OnClickListener,
|
|
|
+public class ChooseTemplateDialogFragment extends DialogFragment implements View.OnClickListener,
|
|
|
TemplateAdapter.ClickListener, Injectable {
|
|
|
|
|
|
private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
|
|
|
private static final String ARG_CREATOR = "CREATOR";
|
|
|
+ private static final String ARG_HEADLINE = "HEADLINE";
|
|
|
private static final String TAG = ChooseTemplateDialogFragment.class.getSimpleName();
|
|
|
private static final String DOT = ".";
|
|
|
+ public static final int SINGLE_TEMPLATE = 1;
|
|
|
|
|
|
private TemplateAdapter adapter;
|
|
|
private OCFile parentFolder;
|
|
|
+ private String title;
|
|
|
@Inject ClientFactory clientFactory;
|
|
|
private Creator creator;
|
|
|
@Inject CurrentAccountProvider currentAccount;
|
|
|
+ private Button positiveButton;
|
|
|
|
|
|
public enum Type {
|
|
|
DOCUMENT,
|
|
@@ -99,20 +100,16 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
PRESENTATION
|
|
|
}
|
|
|
|
|
|
- @BindView(R.id.list)
|
|
|
- RecyclerView listView;
|
|
|
+ ChooseTemplateBinding binding;
|
|
|
|
|
|
- @BindView(R.id.filename)
|
|
|
- EditText fileName;
|
|
|
-
|
|
|
- public static ChooseTemplateDialogFragment newInstance(OCFile parentFolder, Creator creator) {
|
|
|
+ public static ChooseTemplateDialogFragment newInstance(OCFile parentFolder, Creator creator, String headline) {
|
|
|
ChooseTemplateDialogFragment frag = new ChooseTemplateDialogFragment();
|
|
|
Bundle args = new Bundle();
|
|
|
args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
|
|
|
args.putParcelable(ARG_CREATOR, creator);
|
|
|
+ args.putString(ARG_HEADLINE, headline);
|
|
|
frag.setArguments(args);
|
|
|
return frag;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -123,8 +120,14 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
|
|
|
AlertDialog alertDialog = (AlertDialog) getDialog();
|
|
|
|
|
|
- alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
|
|
|
- alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
|
|
|
+ positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
|
|
+ ThemeUtils.themeBorderlessButton(positiveButton, color);
|
|
|
+ positiveButton.setOnClickListener(this);
|
|
|
+ positiveButton.setEnabled(false);
|
|
|
+
|
|
|
+ ThemeUtils.themeBorderlessButton(alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL), color);
|
|
|
+
|
|
|
+ checkEnablingCreateButton();
|
|
|
}
|
|
|
|
|
|
@NonNull
|
|
@@ -140,18 +143,45 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
throw new IllegalArgumentException("Activity may not be null");
|
|
|
}
|
|
|
|
|
|
- int accentColor = ThemeUtils.primaryAccentColor(getContext());
|
|
|
-
|
|
|
parentFolder = arguments.getParcelable(ARG_PARENT_FOLDER);
|
|
|
creator = arguments.getParcelable(ARG_CREATOR);
|
|
|
+ title = arguments.getString(ARG_HEADLINE, getString(R.string.select_template));
|
|
|
+
|
|
|
+ if (savedInstanceState == null) {
|
|
|
+ title = arguments.getString(ARG_HEADLINE);
|
|
|
+ } else {
|
|
|
+ title = savedInstanceState.getString(ARG_HEADLINE);
|
|
|
+ }
|
|
|
|
|
|
// Inflate the layout for the dialog
|
|
|
- LayoutInflater inflater = activity.getLayoutInflater();
|
|
|
- @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.choose_template, null);
|
|
|
- ButterKnife.bind(this, view);
|
|
|
+ LayoutInflater inflater = requireActivity().getLayoutInflater();
|
|
|
+ binding = ChooseTemplateBinding.inflate(inflater, null, false);
|
|
|
+ View view = binding.getRoot();
|
|
|
+
|
|
|
+ binding.filename.requestFocus();
|
|
|
+ ThemeUtils.colorTextInput(binding.filenameContainer, binding.filename, ThemeUtils.primaryColor(getContext()));
|
|
|
+
|
|
|
+ binding.filename.setOnKeyListener((v, keyCode, event) -> {
|
|
|
+ checkEnablingCreateButton();
|
|
|
+ return false;
|
|
|
+ });
|
|
|
+
|
|
|
+ binding.filename.addTextChangedListener(new TextWatcher() {
|
|
|
+ @Override
|
|
|
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
+ // generated method stub
|
|
|
+ }
|
|
|
|
|
|
- fileName.requestFocus();
|
|
|
- fileName.getBackground().setColorFilter(accentColor, PorterDuff.Mode.SRC_ATOP);
|
|
|
+ @Override
|
|
|
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
+ // generated method stub
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterTextChanged(Editable s) {
|
|
|
+ checkEnablingCreateButton();
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
try {
|
|
|
User user = currentAccount.getUser();
|
|
@@ -160,16 +190,17 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
Log_OC.e(TAG, "Loading stream url not possible: " + e);
|
|
|
}
|
|
|
|
|
|
- listView.setHasFixedSize(true);
|
|
|
- listView.setLayoutManager(new GridLayoutManager(activity, 2));
|
|
|
+ binding.list.setHasFixedSize(true);
|
|
|
+ binding.list.setLayoutManager(new GridLayoutManager(activity, 2));
|
|
|
adapter = new TemplateAdapter(creator.getMimetype(), this, getContext(), currentAccount, clientFactory);
|
|
|
- listView.setAdapter(adapter);
|
|
|
+ binding.list.setAdapter(adapter);
|
|
|
|
|
|
// Build the dialog
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
|
|
builder.setView(view)
|
|
|
- .setNegativeButton(R.string.common_cancel, this)
|
|
|
- .setTitle(R.string.select_template);
|
|
|
+ .setPositiveButton(R.string.create, null)
|
|
|
+ .setNeutralButton(R.string.common_cancel, null)
|
|
|
+ .setTitle(title);
|
|
|
Dialog dialog = builder.create();
|
|
|
|
|
|
Window window = dialog.getWindow();
|
|
@@ -181,6 +212,18 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
return dialog;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onDestroyView() {
|
|
|
+ super.onDestroyView();
|
|
|
+ binding = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
|
|
|
+ super.onSaveInstanceState(savedInstanceState);
|
|
|
+ savedInstanceState.putString(ARG_HEADLINE, title);
|
|
|
+ }
|
|
|
+
|
|
|
private void createFromTemplate(Template template, String path) {
|
|
|
new CreateFileFromTemplateTask(this, clientFactory, currentAccount.getUser(), template, path, creator).execute();
|
|
|
}
|
|
@@ -192,21 +235,47 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
|
|
|
@Override
|
|
|
public void onClick(Template template) {
|
|
|
- String name = fileName.getText().toString();
|
|
|
- String path = parentFolder.getRemotePath() + name;
|
|
|
+ onTemplateChosen(template);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void onTemplateChosen(Template template) {
|
|
|
+ adapter.setTemplateAsActive(template);
|
|
|
+ prefillFilenameIfEmpty(template);
|
|
|
+ checkEnablingCreateButton();
|
|
|
+ }
|
|
|
|
|
|
+ private void prefillFilenameIfEmpty(Template template) {
|
|
|
+ String name = binding.filename.getText().toString();
|
|
|
if (name.isEmpty() || name.equalsIgnoreCase(DOT + template.getExtension())) {
|
|
|
- DisplayUtils.showSnackMessage(listView, R.string.enter_filename);
|
|
|
- } else if (!name.endsWith(template.getExtension())) {
|
|
|
- createFromTemplate(template, path + DOT + template.getExtension());
|
|
|
- } else {
|
|
|
- createFromTemplate(template, path);
|
|
|
+ binding.filename.setText(String.format("%s.%s", template.title, template.extension));
|
|
|
}
|
|
|
+ binding.filename.setSelection(binding.filename.getText().toString().lastIndexOf('.'));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void onClick(DialogInterface dialog, int which) {
|
|
|
- // cancel is handled by dialog itself, no other button available
|
|
|
+ public void onClick(View v) {
|
|
|
+ String name = binding.filename.getText().toString();
|
|
|
+ String path = parentFolder.getRemotePath() + name;
|
|
|
+
|
|
|
+ Template selectedTemplate = adapter.getSelectedTemplate();
|
|
|
+
|
|
|
+ if (selectedTemplate == null) {
|
|
|
+ DisplayUtils.showSnackMessage(binding.list, R.string.select_one_template);
|
|
|
+ } else if (name.isEmpty() || name.equalsIgnoreCase(DOT + selectedTemplate.getExtension())) {
|
|
|
+ DisplayUtils.showSnackMessage(binding.list, R.string.enter_filename);
|
|
|
+ } else if (!name.endsWith(selectedTemplate.getExtension())) {
|
|
|
+ createFromTemplate(selectedTemplate, path + DOT + selectedTemplate.getExtension());
|
|
|
+ } else {
|
|
|
+ createFromTemplate(selectedTemplate, path);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkEnablingCreateButton() {
|
|
|
+ Template selectedTemplate = adapter.getSelectedTemplate();
|
|
|
+ String name = binding.filename.getText().toString();
|
|
|
+
|
|
|
+ positiveButton.setEnabled(selectedTemplate != null && !name.isEmpty() &&
|
|
|
+ !name.equalsIgnoreCase(DOT + selectedTemplate.getExtension()));
|
|
|
}
|
|
|
|
|
|
private static class CreateFileFromTemplateTask extends AsyncTask<Void, Void, String> {
|
|
@@ -284,7 +353,7 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
|
|
|
if (fragment != null && fragment.isAdded()) {
|
|
|
if (url.isEmpty()) {
|
|
|
- DisplayUtils.showSnackMessage(fragment.listView, "Error creating file from template");
|
|
|
+ DisplayUtils.showSnackMessage(fragment.binding.list, "Error creating file from template");
|
|
|
} else {
|
|
|
Intent editorWebView = new Intent(MainApp.getAppContext(), TextEditorWebView.class);
|
|
|
editorWebView.putExtra(ExternalSiteWebView.EXTRA_TITLE, "Text");
|
|
@@ -345,12 +414,18 @@ public class ChooseTemplateDialogFragment extends DialogFragment implements Dial
|
|
|
|
|
|
if (fragment != null && fragment.isAdded()) {
|
|
|
if (templateList.templates.isEmpty()) {
|
|
|
- DisplayUtils.showSnackMessage(fragment.listView, R.string.error_retrieving_templates);
|
|
|
+ DisplayUtils.showSnackMessage(fragment.binding.list, R.string.error_retrieving_templates);
|
|
|
} else {
|
|
|
- fragment.setTemplateList(templateList);
|
|
|
+ if (templateList.templates.size() == SINGLE_TEMPLATE) {
|
|
|
+ fragment.onTemplateChosen(templateList.templates.values().iterator().next());
|
|
|
+ fragment.binding.list.setVisibility(View.GONE);
|
|
|
+ } else {
|
|
|
+ String name = DOT + templateList.templates.values().iterator().next().getExtension();
|
|
|
+ fragment.binding.filename.setText(name);
|
|
|
+ fragment.binding.helperText.setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
|
|
|
- String name = DOT + templateList.templates.values().iterator().next().getExtension();
|
|
|
- fragment.fileName.setText(name);
|
|
|
+ fragment.setTemplateList(templateList);
|
|
|
}
|
|
|
} else {
|
|
|
Log_OC.e(TAG, "Error streaming file: no previewMediaFragment!");
|