com.zhongzilu.bit100.view.fragment.EditorFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.zhongzilu.bit100.view.fragment.EditorFragment.java

Source

/*
 * Copyright 2016. SHENQINCI(?)<946736079@qq.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhongzilu.bit100.view.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zhongzilu.bit100.R;
import com.zhongzilu.bit100.application.App;
import com.zhongzilu.bit100.application.engine.PerformEditable;
import com.zhongzilu.bit100.application.engine.PerformInputAfter;
import com.zhongzilu.bit100.application.event.RxEvent;
import com.zhongzilu.bit100.application.event.RxEventBus;
import com.zhongzilu.bit100.application.util.SystemUtils;
import com.zhongzilu.bit100.presenter.EditorFragmentPresenter;
import com.zhongzilu.bit100.presenter.IEditorFragmentView;
import com.zhongzilu.bit100.presenter.IMvpView;
import com.zhongzilu.bit100.view.base.BaseFragment;

import java.io.File;

import butterknife.Bind;
import de.mrapp.android.bottomsheet.BottomSheet;
import ren.qinc.edit.PerformEdit;

/**
 * ?
 * Created by ? on 16/1/21.
 */
public class EditorFragment extends BaseFragment implements IEditorFragmentView, View.OnClickListener {
    public static final String FILE_PATH_KEY = "FILE_PATH_KEY";
    @Bind(R.id.title)
    protected EditText mName;
    @Bind(R.id.content)
    protected EditText mContent;

    private EditorFragmentPresenter mPresenter;

    private PerformEditable mPerformEditable;
    private PerformEdit mPerformEdit;
    private PerformEdit mPerformNameEdit;

    public EditorFragment() {
    }

    public static EditorFragment getInstance(String filePath) {
        EditorFragment editorFragment = new EditorFragment();
        Bundle bundle = new Bundle();
        bundle.putString(FILE_PATH_KEY, filePath);
        editorFragment.setArguments(bundle);
        return editorFragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_editor;
    }

    @Override
    public void onCreateAfter(Bundle savedInstanceState) {

        Bundle arguments = getArguments();
        String fileTemp = arguments.getString(FILE_PATH_KEY);
        if (fileTemp == null) {
            Toast.makeText(App.getAppContext(), "??", Toast.LENGTH_SHORT).show();
            return;
        }

        File file = new File(fileTemp);
        //
        mPresenter = new EditorFragmentPresenter(file);
        mPresenter.attachView(this);

        //????
        mPerformEditable = new PerformEditable(mContent);

        //???
        mPerformEdit = new PerformEdit(mContent) {
            @Override
            protected void onTextChanged(Editable s) {
                //?
                mPresenter.textChange();
            }
        };

        mPerformNameEdit = new PerformEdit(mName) {
            @Override
            protected void onTextChanged(Editable s) {
                //?
                mPresenter.textChange();
            }
        };

        //?()
        PerformInputAfter.start(mContent);

        //?
        if (file.isFile())
            mPresenter.loadFile();
    }

    @Override
    public void initData() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null)
            mPresenter.detachView();//VP
        mPresenter = null;
    }

    @Override
    public void otherSuccess(int flag) {
        switch (flag) {
        case CALL_EXIT:
            getActivity().finish();
            break;
        case CALL_NO_SAVE:
            noSave();
            break;
        case CALL_SAVE:
            saved();
            break;
        }
    }

    @Override
    public void onFailure(int errorCode, String message, int flag) {
        switch (flag) {
        case CALL_SAVE:
        case CALL_LOAOD_FILE:
            App.showSnackbar(mContent, message);
            break;
        default:
            App.showSnackbar(mContent, message);
            break;
        }
    }

    @Override
    public void showWait(String message, boolean canBack, int flag) {
        FragmentActivity activity = getActivity();
        if (activity == null) {
            return;
        }
        if (!(activity instanceof IMvpView)) {
            return;
        }
        ((IMvpView) activity).showWait(message, canBack, flag);

    }

    @Override
    public void hideWait(int flag) {
        FragmentActivity activity = getActivity();
        if (activity == null) {
            return;
        }
        if (!(activity instanceof IMvpView)) {
            return;
        }
        ((IMvpView) activity).hideWait(flag);
    }

    public PerformEditable getPerformEditable() {
        return mPerformEditable;
    }

    @Override
    public boolean hasMenu() {
        return true;
    }

    //??
    private MenuItem mActionSave;

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_editor_frag, menu);
        mActionSave = menu.findItem(R.id.action_save);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case R.id.action_share://
            shareMenu();
            return true;
        case R.id.action_undo://
            mPerformEdit.undo();
            return true;
        case R.id.action_redo://??
            mPerformEdit.redo();
            return true;
        case R.id.action_save://?
            mPresenter.save(mName.getText().toString().trim(), mContent.getText().toString().trim());
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void shareMenu() {
        SystemUtils.hideSoftKeyboard(mContent);
        if (mName.getText().toString().isEmpty()) {
            App.showSnackbar(mContent, "?");
            return;
        }
        if (mContent.getText().toString().isEmpty()) {
            App.showSnackbar(mContent, "?");
            return;
        }

        mPresenter.save(mName.getText().toString(), mContent.getText().toString());

        BottomSheet.Builder builder = new BottomSheet.Builder(getActivity());
        //        builder.setTitle(R.string.bottom_sheet_title);
        builder.addItem(0, R.string.share_copy_text);
        builder.addItem(1, R.string.share_text);
        builder.addItem(2, R.string.share_md);
        //        builder.addDivider();
        //        builder.addItem(3, R.string.share_html);
        //        builder.addItem(4, R.string.share_png);
        builder.setOnItemClickListener((parent, view, position, id) -> {
            switch ((int) id) {
            case 0://?
                shareCopyText();
                break;
            case 1://
                shareText();
                break;
            case 2://md
                shareMD();
                break;
            }

        });
        BottomSheet bottomSheet = builder.create();
        bottomSheet.show();
    }

    private void shareCopyText() {
        SystemUtils.copyToClipBoard(getActivity(), mContent.getText().toString());
    }

    private void shareText() {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, mContent.getText().toString());
        shareIntent.setType("text/plain");

        BottomSheet.Builder builder = new BottomSheet.Builder(getActivity(), R.style.AppTheme);
        builder.setIntent(getActivity(), shareIntent);
        BottomSheet bottomSheet = builder.create();
        bottomSheet.show();
    }

    private void shareMD() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mPresenter.getMDFile()));
        shareIntent.setType("*/*");

        //        startActivity(Intent.createChooser(share,"Share Image"));
        BottomSheet.Builder builder = new BottomSheet.Builder(getActivity());
        builder.setIntent(getActivity(), shareIntent);
        BottomSheet bottomSheet = builder.create();
        bottomSheet.show();
    }

    @Override
    public void onReadSuccess(@NonNull String name, @NonNull String content) {
        mPerformNameEdit.setDefaultText(name.substring(0, name.lastIndexOf(".")));
        mPerformEdit.setDefaultText(content);
        if (content.length() > 0) {
            //??
            //            RxEventBus.getInstance().send(new RxEvent(RxEvent.TYPE_SHOW_PREVIEW, mName.getText().toString(), mContent.getText().toString()));
        }
    }

    public void noSave() {
        if (mActionSave == null)
            return;
        mActionSave.setIcon(R.drawable.ic_action_unsave);
    }

    public void saved() {
        if (mActionSave == null)
            return;
        mActionSave.setIcon(R.drawable.ic_action_save);
    }

    @Override
    public boolean hasNeedEvent(int type) {
        //??
        return type == RxEvent.TYPE_REFRESH_NOTIFY;
    }

    @Override
    public void onEventMainThread(RxEvent event) {
        if (event.isType(RxEvent.TYPE_REFRESH_NOTIFY)) {
            //markdown
            RxEventBus.getInstance().send(new RxEvent(RxEvent.TYPE_REFRESH_DATA, mName.getText().toString(),
                    mContent.getText().toString()));
        }
    }

    @Override
    public boolean onBackPressed() {
        if (mPresenter.isSave()) {
            return false;
        }
        onNoSave();
        return true;
    }

    private void onNoSave() {
        new AlertDialog.Builder(getActivity()).setMessage("????")
                .setNegativeButton("??", (dialog, which) -> {
                    getActivity().finish();

                }).setNeutralButton("?", (dialog, which) -> {
                    dialog.dismiss();

                }).setPositiveButton("?", (dialog, which) -> {
                    mPresenter.saveForExit(mName.getText().toString().trim(), mContent.getText().toString().trim(),
                            true);

                }).show();
    }

    @Override
    public void onClick(View v) {

    }
}