com.myrippleapps.mynotes.app.FragmentNotes.java Source code

Java tutorial

Introduction

Here is the source code for com.myrippleapps.mynotes.app.FragmentNotes.java

Source

package com.myrippleapps.mynotes.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.anjlab.android.iab.v3.BillingProcessor;
import com.anjlab.android.iab.v3.TransactionDetails;
import com.melnykov.fab.FloatingActionButton;
import com.myrippleapps.mynotes.app.adapater.AdapterNote;
import com.myrippleapps.mynotes.app.database.NoteDataSource;
import com.myrippleapps.mynotes.app.database.NoteRecord;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.listeners.ActionClickListener;
import com.nispok.snackbar.listeners.EventListener;
import android.widget.*;

public class FragmentNotes extends Fragment implements View.OnClickListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private final int START_ADD_NOTE = 5;
    //     --Commented out by Inspection (25-Dec-15 8:43 AM):
    private boolean readyToPurchase = false;
    private String IDfromArchived;
    private AdapterNote adapterNote;
    private NoteDataSource dataSource;
    private FloatingActionButton fab;
    private RecyclerView.LayoutManager layoutManager;
    //     --Commented out by Inspection (25-Dec-15 8:43 AM):
    private String isPurchased = "isPurchased";
    private LinearLayout no_note_layout;
    private BillingProcessor bp;
    FrameLayout view;

    public static FragmentNotes newInstance(String param1) {
        FragmentNotes fragmentNotes = new FragmentNotes();
        Bundle arg = new Bundle();
        arg.putString(ARG_PARAM1, "");
        arg.putString(ARG_PARAM2, "");
        fragmentNotes.setArguments(arg);
        return fragmentNotes;
    }

    private void isNoteEmpty() {
        if (adapterNote.getItemCount() == 0) {
            //Default Value = 0
            no_note_layout.setVisibility(view.VISIBLE);
            return;
        }
        //Default Value = 8
        no_note_layout.setVisibility(view.GONE);
    }

    // --Commented out by Inspection START (25-Dec-15 8:43 AM):
    public void isChangingTheme() {

    }
    // --Commented out by Inspection STOP (25-Dec-15 8:43 AM)

    // --Commented out by Inspection START (25-Dec-15 8:43 AM):
    public void fName() {

    }
    // --Commented out by Inspection STOP (25-Dec-15 8:43 AM)

    @Override
    public void onActivityResult(int requestCode, int resultCode, final Intent data) {
        if (requestCode == 1) {
            if (resultCode == -1) {
                final int position = Integer.parseInt(data.getStringExtra("position"));
                adapterNote.remove(position);
                isNoteEmpty();
                SnackbarManager.show(Snackbar.with(getActivity().getApplicationContext())
                        .text(getResources().getString(R.string.note_archived))
                        .actionColor(getResources().getColor(R.color.primary_color))
                        .duration(Snackbar.SnackbarDuration.LENGTH_SHORT)
                        .actionLabel(getResources().getString(R.string.undo).toUpperCase())
                        .actionListener(new ActionClickListener() {

                            @Override
                            public void onActionClicked(Snackbar snackbar) {
                                NoteRecord noteRecord = (NoteRecord) data.getSerializableExtra("records");
                                IDfromArchived = data.getStringExtra("ID");
                                dataSource.open();
                                dataSource.unarchivedNote(Integer.parseInt(IDfromArchived));
                                adapterNote.add(position, noteRecord);
                                layoutManager.scrollToPosition(position);
                                isNoteEmpty();
                            }
                        }).eventListener(new EventListener() {

                            @Override
                            public void onDismiss(Snackbar snackbar) {
                                fab.animate().translationYBy(snackbar.getHeight()).setDuration(470);
                            }

                            @Override
                            public void onDismissByReplace(Snackbar snackbar) {
                            }

                            @Override
                            public void onDismissed(Snackbar snackbar) {
                                fab.show();
                                fab.bringToFront();
                            }

                            @Override
                            public void onShow(Snackbar snackbar) {
                                fab.animate().translationYBy(-snackbar.getHeight());
                            }

                            @Override
                            public void onShowByReplace(Snackbar snackbar) {
                            }

                            @Override
                            public void onShown(Snackbar snackbar) {
                            }
                        }), getActivity());
            } else {
                int RESULT_DELETED = 99;
                if (resultCode == RESULT_DELETED) {
                    int position = Integer.parseInt(data.getStringExtra("position"));
                    adapterNote.remove(position);
                    isNoteEmpty();
                    SnackbarManager.show(Snackbar.with(getActivity().getApplicationContext())
                            .text(getResources().getString(2131558501))
                            .duration(Snackbar.SnackbarDuration.LENGTH_SHORT).eventListener(new EventListener() {

                                @Override
                                public void onDismiss(Snackbar snackbar) {
                                    fab.animate().translationYBy(snackbar.getHeight()).setDuration(470);
                                }

                                @Override
                                public void onDismissByReplace(Snackbar snackbar) {
                                }

                                @Override
                                public void onDismissed(Snackbar snackbar) {
                                }

                                @Override
                                public void onShow(Snackbar snackbar) {
                                    fab.animate().translationYBy(-snackbar.getHeight());
                                }

                                @Override
                                public void onShowByReplace(Snackbar snackbar) {
                                }

                                @Override
                                public void onShown(Snackbar snackbar) {
                                }
                            }), getActivity());
                    return;
                }
                int RESULT_ADDED = 100;
                if (resultCode == RESULT_ADDED) {
                    int position = Integer.parseInt(data.getStringExtra("position"));
                    NoteRecord noteRecord = (NoteRecord) data.getSerializableExtra("records");
                    adapterNote.notify(position, noteRecord);
                    isNoteEmpty();
                    return;
                }
                int RESULT_LOCK = 101;
                if (resultCode != RESULT_LOCK)
                    return;
                {
                    int position = Integer.parseInt(data.getStringExtra("position"));
                    NoteRecord noteRecord = (NoteRecord) data.getSerializableExtra("records");
                    adapterNote.notify(position, noteRecord);
                }
            }
        } else {
            if (requestCode != START_ADD_NOTE || resultCode != -1)
                return;
            {
                NoteRecord noteRecord = (NoteRecord) data.getSerializableExtra("noteRecord");
                noteRecord.setNote_id(dataSource.getLatestID());
                adapterNote.add(0, noteRecord);
                layoutManager.scrollToPosition(0);
                isNoteEmpty();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.fab: {
            Intent intent = new Intent(getActivity(), AddNote.class);
            getActivity().startActivityForResult(intent, START_ADD_NOTE);
            return;
        }
        default:
        }
    }

    //    @Override
    //    public void onCreate(Bundle savedInstanceState) {
    //        super.onCreate(savedInstanceState);
    //        if (getArguments() != null) {
    //            String mParam1 = getArguments().getString(ARG_PARAM1);
    //            String mParam2 = getArguments().getString(ARG_PARAM2);
    //        }

    //    }

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        view = (FrameLayout) layoutInflater.inflate(R.layout.fragment_notes, viewGroup, false);
        if (getArguments() != null) {
            String mParam1 = getArguments().getString(ARG_PARAM1);
            String mParam2 = getArguments().getString(ARG_PARAM2);
        }
        dataSource = new NoteDataSource(getActivity());
        dataSource.open();
        String sharedPreferencesNotes = "mPreferences";
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(sharedPreferencesNotes, 0);
        String notesLayoutKey = "notesLayoutKey";
        String notes_layout_to_bind = sharedPreferences.getString(notesLayoutKey, "");
        String SINGLE_COLUMN_VIEW = "single";
        String MULTI_COLUMN_VIEW = "mutli";
        if (notes_layout_to_bind.equals("") || notes_layout_to_bind.equals(SINGLE_COLUMN_VIEW)) {
            layoutManager = new LinearLayoutManager(getActivity());
        } else if (notes_layout_to_bind.equals(MULTI_COLUMN_VIEW)) {
            int SPAN_COUNT = 2;
            int ORIENTATION = 1;
            layoutManager = new StaggeredGridLayoutManager(SPAN_COUNT, ORIENTATION);
        }

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.noteList);
        no_note_layout = (LinearLayout) view.findViewById(R.id.no_note_layout);
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        adapterNote = new AdapterNote(getActivity(), dataSource.getAllNotes(), recyclerView, fragmentManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapterNote);
        fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.attachToRecyclerView(recyclerView);
        Animation animationGrow = AnimationUtils.loadAnimation(getActivity(), R.anim.simple_grow);
        fab.setOnClickListener(this);
        isNoteEmpty();
        fab.startAnimation(animationGrow);
        fab.attachToRecyclerView(recyclerView);
        sharedPreferences = getActivity().getSharedPreferences(sharedPreferencesNotes, 0);
        if (!sharedPreferences.getBoolean("isPurchased", false)) {
            FragmentAdsListener.track(view.findViewById(R.id.adView), recyclerView, savedInstanceState);

        }
        return view;
    }

    private void iabInit() {
        if (!BillingProcessor.isIabServiceAvailable(getActivity())) {
            Constant.showMaterialDialog(getActivity(), "Android Play Service Error",
                    "In-app billing service is unavailable, please upgrade Android Market/Play to version >= 3.9.16",
                    getResources().getString(R.string.ok), new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                        }
                    }, null, null, null, null);
            //showToast("In-app billing service is unavailable, please upgrade Android Market/Play to version >= 3.9.16");
        }
        bp = new BillingProcessor(getActivity(), Constant.LICENSE_KEY, Constant.MERCHANT_ID,
                new BillingProcessor.IBillingHandler() {
                    @Override
                    public void onProductPurchased(String productId, TransactionDetails details) {
                        Log.i(Constant.LOG_IAB, "onProductPurchased: " + productId);
                        //updateAppTitle();
                        startActivity(new Intent(getActivity().getApplicationContext(), MyActivity.class));
                    }

                    @Override
                    public void onBillingError(int errorCode, Throwable error) {
                        Log.i(Constant.LOG_IAB, "onBillingError load: Error= " + Integer.toString(errorCode));
                        Constant.showMaterialDialog(getActivity().getApplicationContext(), "Purchased failed",
                                "Error while purchasing. Please check your network connection and try again. Error Code= "
                                        + Integer.toString(errorCode),
                                "Try Again", new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog,
                                            @NonNull DialogAction which) {
                                        bp.purchase(getActivity(), Constant.PRODUCT_ID);
                                    }
                                }, getString(R.string.cancel), new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog,
                                            @NonNull DialogAction which) {
                                        dialog.dismiss();
                                    }
                                }, null, null);

                        //showToast("Purchase failed!");
                    }

                    @Override
                    public void onBillingInitialized() {
                        Log.i(Constant.LOG_IAB, "InAppBilling Initialized!");
                        readyToPurchase = true;
                        //updateAppTitle();
                    }

                    @Override
                    public void onPurchaseHistoryRestored() {
                        Constant.showToast(getActivity().getApplicationContext());
                        for (String sku : bp.listOwnedProducts())
                            Log.d(Constant.LOG_IAB, "Owned Managed Product: " + sku);
                        for (String sku : bp.listOwnedSubscriptions())
                            Log.d(Constant.LOG_IAB, "Owned Subscription: " + sku);
                        //updateAppTitle();
                    }
                });
    }

    // --Commented out by Inspection START (25-Dec-15 8:43 AM):
    public String updateAppTitle() {
        try {
            return String.format("%s %s", getResources().getString(R.string.app_name),
                    bp.isPurchased(Constant.PRODUCT_ID) ? "Pro" : "");
        } catch (NullPointerException e) {
            iabInit();
            return String.format("%s %s", getResources().getString(R.string.app_name),
                    bp.isPurchased(Constant.PRODUCT_ID) ? "Pro" : "");
        }
    }
    // --Commented out by Inspection STOP (25-Dec-15 8:43 AM)

    // --Commented out by Inspection START (25-Dec-15 8:43 AM):
    public void showMaterialDialog2(String title, String content, String positive, String negative,
            MaterialDialog.ButtonCallback buttonCallback) {
        //String p = getResources().getString(R.string.cancel);
        new MaterialDialog.Builder(getActivity()).title(title).content(content).positiveText(positive)
                .negativeText(negative).callback(buttonCallback).show();
    }
    // --Commented out by Inspection STOP (25-Dec-15 8:43 AM)

}