com.slim.turboeditor.dialogfragment.SaveFileDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.slim.turboeditor.dialogfragment.SaveFileDialog.java

Source

/*
 * Copyright (C) 2014 Vlad Mihalachi
 *
 * This file is part of Turbo Editor.
 *
 * Turbo Editor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Turbo Editor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.slim.turboeditor.dialogfragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.slim.slimfilemanager.R;
import com.slim.turboeditor.activity.MainActivity;
import com.slim.turboeditor.views.DialogHelper;

import java.io.File;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

@SuppressLint("ValidFragment")
public class SaveFileDialog extends DialogFragment {

    File mFile;
    String mText;
    String mEncoding;
    boolean mOpenNewFileAfter;

    @SuppressLint("ValidFragment")
    public SaveFileDialog(File file, String text, String encoding) {
        mFile = file;
        mText = text;
        mEncoding = encoding;
        mOpenNewFileAfter = false;
    }

    @SuppressLint("ValidFragment")
    public SaveFileDialog(File file, String text, String encoding, boolean openNewFileAfter) {
        mFile = file;
        mText = text;
        mEncoding = encoding;
        mOpenNewFileAfter = openNewFileAfter;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        View view = new DialogHelper.Builder(getActivity())
                .setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_action_save))
                .setTitle(R.string.salva)
                .setMessage(String.format(getString(R.string.save_changes), mFile.getName())).createCommonView();

        return new AlertDialog.Builder(getActivity()).setView(view)
                .setPositiveButton(R.string.salva, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (getActivity() != null) {
                            ((MainActivity) getActivity())
                                    .getSaveFileObservable(getActivity(), mFile, mText, mEncoding)
                                    .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1<Boolean>() {
                                        @Override
                                        public void call(Boolean aBoolean) {
                                            ((MainActivity) getActivity()).savedAFile(aBoolean);
                                        }
                                    });
                        }
                    }
                }).setNeutralButton(android.R.string.cancel, null)
                .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ISaveDialog target = (ISaveDialog) getTargetFragment();
                        if (target == null) {
                            target = (ISaveDialog) getActivity();
                        }
                        target.userDoesntWantToSave(mOpenNewFileAfter, null);
                    }
                }).create();
    }

    public interface ISaveDialog {
        void userDoesntWantToSave(boolean openNewFile, File newFile);
    }
}