Search in sources :

Example 6 with Entry

use of com.foobnix.opds.Entry in project LibreraReader by foobnix.

the class AddCatalogDialog method showDialog.

public static void showDialog(final Activity a, final Runnable onRefresh, final Entry e, final boolean validate) {
    AlertDialog.Builder builder = new AlertDialog.Builder(a);
    View dialog = LayoutInflater.from(a).inflate(R.layout.dialog_add_catalog, null, false);
    final EditText url = (EditText) dialog.findViewById(R.id.url);
    url.setText("http://");
    url.setSelection(url.getText().length());
    url.setEnabled(validate);
    final EditText name = (EditText) dialog.findViewById(R.id.name);
    final EditText description = (EditText) dialog.findViewById(R.id.description);
    final ProgressBar progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar);
    TintUtil.setDrawableTint(progressBar.getIndeterminateDrawable().getCurrent(), TintUtil.color);
    final ImageView image = (ImageView) dialog.findViewById(R.id.image);
    final CheckBox addAsWEb = (CheckBox) dialog.findViewById(R.id.addAsWEb);
    addAsWEb.setVisibility(View.GONE);
    final String editAppState = e != null ? e.appState : null;
    if (editAppState != null) {
        String[] line = e.appState.replace(";", "").split(",");
        url.setText(line[0]);
        name.setText(line[1]);
        description.setText(line[2]);
        ImageLoader.getInstance().displayImage(line[3], image, IMG.displayCacheMemoryDisc);
        if (e.logo != null) {
            image.setTag(e.logo);
        }
    }
    progressBar.setVisibility(View.GONE);
    image.setVisibility(View.GONE);
    builder.setView(dialog);
    builder.setTitle(R.string.add_catalog);
    builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
        }
    });
    builder.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            Keyboards.close(a);
        }
    });
    final AlertDialog infoDialog = builder.create();
    infoDialog.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            Keyboards.close(a);
        }
    });
    infoDialog.show();
    url.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        }
    });
    infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {

        AsyncTask asyncTask;

        @Override
        public void onClick(View v) {
            final String feedUrl = url.getText().toString();
            if (infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).getText().equals(a.getString(R.string.ok)) || addAsWEb.isChecked() || !validate) {
                Entry entry = new Entry();
                entry.setAppState(feedUrl, name.getText().toString(), description.getText().toString(), image.getTag().toString());
                if (editAppState != null) {
                    AppState.get().myOPDSLinks = AppState.get().myOPDSLinks.replace(editAppState, "");
                }
                AppState.get().myOPDSLinks = entry.appState + AppState.get().myOPDSLinks;
                onRefresh.run();
                infoDialog.dismiss();
                AppState.get().save(a);
                return;
            }
            if (AsyncTasks.isRunning(asyncTask)) {
                AsyncTasks.toastPleaseWait(a);
                return;
            }
            asyncTask = new AsyncTask() {

                @Override
                protected Object doInBackground(Object... params) {
                    return OPDS.getFeed(feedUrl);
                }

                @Override
                protected void onPreExecute() {
                    progressBar.setVisibility(View.VISIBLE);
                    image.setVisibility(View.GONE);
                }

                @Override
                protected void onPostExecute(Object result) {
                    try {
                        progressBar.setVisibility(View.GONE);
                        if (result == null || ((Feed) result).entries.isEmpty()) {
                            if (result != null && ((Feed) result).isNeedLoginPassword) {
                                AddCatalogDialog.showDialogLogin(a, new Runnable() {

                                    @Override
                                    public void run() {
                                    }
                                });
                            } else {
                                Toast.makeText(a, a.getString(R.string.incorrect_value) + " OPDS " + feedUrl, Toast.LENGTH_LONG).show();
                                infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
                                addAsWEb.setVisibility(View.VISIBLE);
                                name.setText(feedUrl);
                                image.setTag("assets://opds/web.png");
                            }
                            return;
                        }
                        Feed feed = (Feed) result;
                        name.setText(TxtUtils.nullToEmpty(feed.title));
                        if (TxtUtils.isNotEmpty(feed.subtitle)) {
                            description.setText(TxtUtils.nullToEmpty(feed.subtitle));
                        }
                        if (feed.icon != null) {
                            image.setVisibility(View.VISIBLE);
                            feed.icon = Hrefs.fixHref(feed.icon, feedUrl);
                            image.setTag(feed.icon);
                            ImageLoader.getInstance().displayImage(feed.icon, image, IMG.displayCacheMemoryDisc);
                        } else {
                            image.setTag("assets://opds/web.png");
                        }
                        infoDialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.ok);
                    } catch (Exception e) {
                        LOG.e(e);
                    }
                }
            }.execute();
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Entry(com.foobnix.opds.Entry) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) ImageView(android.widget.ImageView) ProgressBar(android.widget.ProgressBar) Feed(com.foobnix.opds.Feed) EditText(android.widget.EditText) OnDismissListener(android.content.DialogInterface.OnDismissListener) AsyncTask(android.os.AsyncTask) OnClickListener(android.view.View.OnClickListener) ImageView(android.widget.ImageView) View(android.view.View) CheckBox(android.widget.CheckBox)

Aggregations

Entry (com.foobnix.opds.Entry)6 View (android.view.View)3 OnClickListener (android.view.View.OnClickListener)3 EditText (android.widget.EditText)3 ImageView (android.widget.ImageView)3 Link (com.foobnix.opds.Link)3 AlertDialog (android.app.AlertDialog)2 DialogInterface (android.content.DialogInterface)2 RecyclerView (android.support.v7.widget.RecyclerView)2 OnLongClickListener (android.view.View.OnLongClickListener)2 CheckBox (android.widget.CheckBox)2 TextView (android.widget.TextView)2 Feed (com.foobnix.opds.Feed)2 ArrayList (java.util.ArrayList)2 Dialog (android.app.Dialog)1 Context (android.content.Context)1 OnDismissListener (android.content.DialogInterface.OnDismissListener)1 Drawable (android.graphics.drawable.Drawable)1 AsyncTask (android.os.AsyncTask)1 LayoutParams (android.support.v7.widget.StaggeredGridLayoutManager.LayoutParams)1