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

Java tutorial

Introduction

Here is the source code for com.kccomy.orgar.ui.note.NoteFragment.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.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.kccomy.orgar.R;
import com.kccomy.orgar.adapter.ShareAdapter;
import com.kccomy.orgar.adapter.SimpleTreeAdapter;
import com.kccomy.orgar.adapter.TreeListViewAdapter;
import com.kccomy.orgar.di.component.NoteActivityComponent;
import com.kccomy.orgar.module.TreeNode;
import com.kccomy.orgar.ui.base.BaseFragment;

import org.cowboyprogrammer.org.OrgNode;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class NoteFragment extends BaseFragment implements NoteContract.View,
        TreeListViewAdapter.OnTreeNodeClickListener, MenuItem.OnMenuItemClickListener {

    @Inject
    NotePresenter presenter;

    @BindView(R.id.fragment_note_tree_list)
    ListView listView;

    @BindView(R.id.fragment_note_tree_rlayout_no_node)
    View noNodeView;

    @BindView(R.id.fragment_note_tree_btn_add_node)
    Button addBtn;

    @BindView(R.id.fragment_note_tree_llayout)
    LinearLayout linearLayout;

    private SimpleTreeAdapter adapter;

    private String fileName;

    @Inject
    public NoteFragment() {
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_note;
    }

    //    @Override
    //    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    //        int cx = (linearLayout.getLeft() + linearLayout.getRight()) / 2;
    //        int cy = (linearLayout.getTop() + linearLayout.getBottom()) / 2;
    //
    //        float finalRadius = (float) Math.hypot(linearLayout.getWidth(), linearLayout.getHeight());
    //
    //        Animator anim = ViewAnimationUtils.createCircularReveal(linearLayout, cx, cy, 0,
    //                finalRadius);
    //        anim.setDuration(300);
    //        anim.addListener(new AnimatorListenerAdapter() {
    //            @Override public void onAnimationEnd(Animator animation) {
    //                super.onAnimationEnd(animation);
    //                //view.setVisibility(View.VISIBLE);
    //                //listener.onRevealShow();
    //            }
    //
    //            @Override public void onAnimationStart(Animator animation) {
    //                super.onAnimationStart(animation);
    //                //view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color
    //                        //.colorPrimary));
    //            }
    //        });
    //        return null;
    //    }

    @Override
    protected void initVariables() {
        getComponent(NoteActivityComponent.class).inject(this);

        //        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_item);
        //        LayoutAnimationController controller = new LayoutAnimationController(animation);
        //        controller.setDelay(0.5f);
        //        listView.setLayoutAnimation(controller);

        fileName = getArguments().getString("FILE_NAME");
        presenter.setView(this);

        adapter = new SimpleTreeAdapter(listView, getContext());
        adapter.setOnTreeNodeClickListener(this);
        listView.setAdapter(adapter);
    }

    @Override
    protected void initViews(final View view, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.load(fileName);
    }

    @Override
    public void removeItem(TreeNode node) {
        adapter.removeItem(node);
    }

    @OnClick(R.id.fragment_note_tree_btn_add_node)
    public void newNode() {
        presenter.newNode();
    }

    @Override
    public void showTree(OrgNode root) {
        listView.setVisibility(View.VISIBLE);
        adapter.replaceData(root);
    }

    @Override
    public void hideTree() {
        listView.setVisibility(View.GONE);
    }

    @Override
    public void showNoNodeView() {
        noNodeView.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideNoNodeView() {
        noNodeView.setVisibility(View.GONE);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.toolbar_note_tree, menu);

        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setOnMenuItemClickListener(this);
        }
    }

    @Override
    public void showDeleteDialog(DialogInterface.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.dialog_delete_node_alert);
        builder.setNegativeButton(R.string.dialog_action_cancel, null);
        builder.setPositiveButton(R.string.dialog_action_ok, listener);
        builder.show();
    }

    @Override
    public void showNodeEditorFragment(Fragment fragment) {
        getHoldingActivity().addFragment(fragment, true);
    }

    @Override
    public void onNodeClick(TreeNode treeNode, int position) {

    }

    @Override
    public void onNodeLongClick(View view, final TreeNode treeNode, final int position) {
        LinearLayout menuLayout = (LinearLayout) view.findViewById(R.id.item_org_tree_llayout_menu);
        menuLayout.setVisibility(View.VISIBLE);

        ImageButton imgBtnEdit = (ImageButton) menuLayout.findViewById(R.id.item_org_tree_menu_ibtn_edit);
        imgBtnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.edit(treeNode);
            }
        });

        ImageButton imageBtnAddNode = (ImageButton) menuLayout.findViewById(R.id.item_org_tree_menu_ibtn_add_node);
        imageBtnAddNode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.addNode(treeNode);
            }
        });

        ImageButton imageButtonAddSub = (ImageButton) menuLayout
                .findViewById(R.id.item_org_tree_menu_ibtn_add_subnode);
        imageButtonAddSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.addSubNode(treeNode);
            }
        });

        ImageButton imageBtnDelete = (ImageButton) menuLayout.findViewById(R.id.item_org_tree_menu_ibtn_delete);
        imageBtnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.delete(treeNode);
            }
        });

    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {

        int id = menuItem.getItemId();

        Log.d(TAG, "MenuItem " + id + " clicked");

        switch (id) {
        case R.id.action_share:
            initPopupWindow();
            break;

        case R.id.fold_unfold_all:
            presenter.foldOrUnfold();
            break;
        }

        return true;
    }

    private void initPopupWindow() {
        View popupWindowView = getActivity().getLayoutInflater().inflate(R.layout.popup_window_share, null);

        //??
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.addCategory(Intent.CATEGORY_DEFAULT);
        shareIntent.setType("text/plain");

        PackageManager packageManager = getContext().getPackageManager();
        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(shareIntent,
                PackageManager.GET_RESOLVED_FILTER);

        // RecyclerAdapter
        RecyclerView recyclerView = (RecyclerView) popupWindowView.findViewById(R.id.popup_widow_share_recycler);
        ShareAdapter shareAdapter = new ShareAdapter(packageManager);
        shareAdapter.setData(resolveInfos);
        shareAdapter.setAppIconClickListener(shareListener);

        recyclerView.setAdapter(shareAdapter);
        recyclerView
                .setLayoutManager(new GridLayoutManager(getContext(), 2, LinearLayoutManager.HORIZONTAL, false));

        // ?PopupWindow
        popupWindowView.setFocusable(true);
        popupWindowView.setFocusableInTouchMode(true);

        PopupWindow popupWindow = new PopupWindow(popupWindowView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));

        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);

        setWindowAlpha(0.5f);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setWindowAlpha(1f);
            }
        });

        popupWindow.showAtLocation(getActivity().getLayoutInflater().inflate(R.layout.fragment_note, null),
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    private void setWindowAlpha(float f) {
        Window window = getActivity().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = f;
        window.setAttributes(lp);
    }

    private ShareAdapter.OnAppIconClickListener shareListener = new ShareAdapter.OnAppIconClickListener() {
        @Override
        public void onClick(String packageName, String name) {
            Log.d(TAG, "app icon clicked, package name = " + packageName + ", class = " + name);
            presenter.shareToOther(packageName, name);
        }
    };
}