com.kccomy.orgar.ui.note.NotePresenter.java Source code

Java tutorial

Introduction

Here is the source code for com.kccomy.orgar.ui.note.NotePresenter.java

Source

/*
 * Copyright (C) 2016. kccomy, Orgar
 *
 * 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.kccomy.orgar.ui.note;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.kccomy.orgar.data.OrgRepository;
import com.kccomy.orgar.module.SerializedOrgFile;
import com.kccomy.orgar.module.TreeNode;
import com.kccomy.orgar.ui.editor.NodeEditorFragment;

import org.cowboyprogrammer.org.OrgFile;
import org.cowboyprogrammer.org.OrgNode;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NotePresenter implements NoteContract.Presenter {

    private static final String TAG = "NotePresenter";

    @Inject
    Context context;

    @Inject
    OrgRepository repository;

    private OrgFile orgFile;

    private SerializedOrgFile serializedOrgFile;

    private NoteContract.View view;

    @Inject
    public NotePresenter() {

    }

    public void setView(NoteContract.View view) {
        this.view = view;
    }

    @Override
    public void load(String fileName) {
        Log.d(TAG, "load(" + fileName + ")");
        repository.read(fileName).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<OrgFile>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "read orgfile error!");
                    }

                    @Override
                    public void onNext(OrgFile file) {
                        processLoad(file);
                    }
                });
    }

    private void processLoad(OrgFile file) {
        orgFile = file;
        Log.d(TAG, orgFile.treeToString());
        serializedOrgFile = new SerializedOrgFile(orgFile);

        if (file.getSubNodes().size() == 0) {
            Log.d(TAG, "number of subnodes of orgfile " + file.getFilename() + " = " + file.getSubNodes().size());
            view.showNoNodeView();
            view.hideTree();
        } else {
            view.showTree(orgFile);
            view.hideNoNodeView();
        }

    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {

    }

    @Override
    public void shareToOther(@NonNull String packageName, @NonNull String name) {
        Intent it = new Intent(Intent.ACTION_SEND);
        it.setComponent(new ComponentName(packageName, name));
        it.putExtra(Intent.EXTRA_TEXT, "TBD");
        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        it.setType("text/plain");
        context.startActivity(it);
    }

    @Override
    public void foldOrUnfold() {

    }

    @Override
    public void newNode() {
        Fragment editFragment = new NodeEditorFragment();
        Bundle bundle = new Bundle();
        bundle.putString("action", NodeEditorFragment.EditAction.NEW.toString());
        bundle.putSerializable("serialized_org_file", serializedOrgFile);
        editFragment.setArguments(bundle);
        view.showNodeEditorFragment(editFragment);
    }

    @Override
    public void edit(TreeNode n) {
        Fragment editFragment = new NodeEditorFragment();
        Bundle bundle = new Bundle();
        bundle.putString("action", NodeEditorFragment.EditAction.EDIT.toString());
        bundle.putSerializable("node", n);
        bundle.putSerializable("serialized_org_file", serializedOrgFile);
        editFragment.setArguments(bundle);
        view.showNodeEditorFragment(editFragment);
    }

    @Override
    public void addNode(TreeNode n) {
        Fragment editFragment = new NodeEditorFragment();
        Bundle bundle = new Bundle();
        bundle.putString("action", NodeEditorFragment.EditAction.ADD.toString());
        bundle.putSerializable("node", n);
        bundle.putSerializable("serialized_org_file", serializedOrgFile);
        editFragment.setArguments(bundle);

        view.showNodeEditorFragment(editFragment);
    }

    @Override
    public void addSubNode(TreeNode n) {
        Fragment editFragment = new NodeEditorFragment();
        Bundle bundle = new Bundle();
        bundle.putString("action", NodeEditorFragment.EditAction.ADD_SUB.toString());
        bundle.putSerializable("node", n);
        bundle.putSerializable("serialized_org_file", serializedOrgFile);
        editFragment.setArguments(bundle);

        view.showNodeEditorFragment(editFragment);
    }

    @Override
    public void delete(final TreeNode n) {
        final OrgNode curNode = n.getOrgNode();
        //Traverse org tree and delete that node
        if (curNode.getSubNodes().size() > 0) {
            // Has sub node, pop up an alert dialog
            DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    OrgNode parent = curNode.getParent();
                    parent.getSubNodes().remove(curNode);
                    view.removeItem(n);
                }
            };

            view.showDeleteDialog(listener);

        } else {
            OrgNode parent = curNode.getParent();
            parent.getSubNodes().remove(curNode);
            view.removeItem(n);
        }

        repository.upload(orgFile).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }

}