Java tutorial
/* * 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 ren.qinc.markdowneditors.view; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TextInputLayout; import android.support.v4.view.MenuItemCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.view.ActionMode; import android.support.v7.widget.AppCompatAutoCompleteTextView; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SearchView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import java.io.File; import java.util.ArrayList; import java.util.List; import butterknife.Bind; import butterknife.OnClick; import ren.qinc.markdowneditors.R; import ren.qinc.markdowneditors.adapter.FileListAdapter; import ren.qinc.markdowneditors.adapter.OnItemClickLitener; import ren.qinc.markdowneditors.base.BaseApplication; import ren.qinc.markdowneditors.base.BaseRefreshFragment; import ren.qinc.markdowneditors.engine.ActionModeCallback; import ren.qinc.markdowneditors.entity.FileBean; import ren.qinc.markdowneditors.event.RxEvent; import ren.qinc.markdowneditors.presenter.FolderManagerPresenter; import ren.qinc.markdowneditors.presenter.IFolderManagerView; import ren.qinc.markdowneditors.utils.Check; import ren.qinc.markdowneditors.utils.ViewUtils; import ren.qinc.markdowneditors.widget.TabView; /** * ?? * Created by ? on 16/1/27. */ public class FolderManagerFragment extends BaseRefreshFragment implements IFolderManagerView, View.OnClickListener, OnItemClickLitener { @Bind(R.id.content_view) protected RecyclerView mfileList; @Bind(R.id.tab_view) protected TabView mTabView; @Bind(R.id.noContent) protected View noContent; @Bind(R.id.fab) protected FloatingActionButton mActionButton; private FolderManagerPresenter mPresenter; private List<FileBean> files = new ArrayList<>(); private FileListAdapter mAdapter; private ActionMode mActionMode; //?ActionMode private ActionMode.Callback pasteModeCallback; //?ActionMode private ActionMode.Callback editModeCallback; @Override public void onDestroyView() { super.onDestroyView(); mPresenter.detachView();//VP mPresenter = null; } @Override protected void onRefresh(SwipeRefreshLayout swipeRefreshLayout) { mPresenter.refreshCurrentPath(); mPresenter.closeEditMode(); } @Override public int getLayoutId() { return R.layout.fragment_folder_manager; } @Override public void onCreateAfter(Bundle savedInstanceState) { super.onCreateAfter(savedInstanceState); initActionMode(); //?Presenter mPresenter = new FolderManagerPresenter(files); mPresenter.attachView(this); //?recycleView LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); mfileList.setLayoutManager(linearLayoutManager); mfileList.setAdapter(mAdapter = new FileListAdapter(mContext, files)); // mfileList.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST)); mfileList.setItemAnimator(new DefaultItemAnimator()); mfileList.setLongClickable(true); mAdapter.setOnItemClickLitener(this); //ListView ?MultiChoiceModeListener } @Override public void initData() { mPresenter.initRoot(mContext); } /** * * On failure. * * @param errorCode the error code * @param message the message * @param flag the flag */ @Override public void onFailure(int errorCode, String message, int flag) { switch (flag) { case CALL_GET_FILES://??? finishRefresh(); default: BaseApplication.showSnackbar(getSwipeRefreshLayout(), message); break; } } @Override public void showWait(String message, boolean canBack, int flag) { switch (flag) { case CALL_GET_FILES://? getSwipeRefreshLayout().setRefreshing(true); break; } } @Override public void hideWait(int flag) { switch (flag) { case CALL_GET_FILES://? finishRefresh(); break; } } @Override public void getFileListSuccess(List<FileBean> files) { mAdapter.notifyDataSetChanged(); if (files.isEmpty()) { noContent.setVisibility(View.VISIBLE); } else { noContent.setVisibility(View.GONE); } finishRefresh(); } @Override public void otherSuccess(int flag) { switch (flag) { case CALL_COPY_PASTE://? case CALL_CUT_PASTE:// // getSwipeRefreshLayout().postDelayed(()->refresh(),50); break; case CALL_PASTE_MODE://? pasteMode(); break; case CALL_CLOSE_PASTE_MODE://?? closeActionMode(); break; case CALL_EDIT_MODE://? openEditMode(); break; case CALL_CLOSE_EDIT_MODE://? closeEditMode(); break; case CALL_REMOVE_TAB:// removeTab(); break; } } private void closeEditMode() { mAdapter.setEditMode(false); mAdapter.notifyDataSetChanged(); closeActionMode(); } private void openEditMode() { mAdapter.setEditMode(true); //?ActionMode mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(editModeCallback); mActionMode.setTitle(String.valueOf(mPresenter.getSelectCount())); } private void closeActionMode() { // mAdapter.notifyDataSetChanged(); if (mActionMode != null) mActionMode.finish(); mActionMode = null; } private void pasteMode() { //?ActionMode mActionMode = ((AppCompatActivity) getActivity()).startSupportActionMode(pasteModeCallback); mActionMode.setTitle("?"); } @Override public void addTab(String title) { mTabView.addTab(title, this); } private boolean removeTab() { return mTabView.removeTab(); } @Override public void updatePosition(int position, FileBean bean) { mAdapter.notifyItemChanged(position); } @Override public void addFilePosition(int position, FileBean bean) { mAdapter.addData(position, bean); } @Override public void onItemClick(View view, int position) { FileBean fileBean = files.get(position); //? if (mPresenter.isEditMode() && mActionMode != null) { fileBean.isSelect = !fileBean.isSelect; mAdapter.notifyItemChanged(position); //?? int selectCount = mPresenter.getSelectCount(); //?1???????? //?0? if (selectCount == 0) { mActionMode.setTitle(""); mPresenter.closeEditMode(); } else if (selectCount == 1) { mActionMode.setTitle(String.valueOf(selectCount)); mActionMode.getMenu().findItem(R.id.action_edit).setVisible(true); } else { mActionMode.setTitle(String.valueOf(selectCount)); mActionMode.getMenu().findItem(R.id.action_edit).setVisible(false); } return; } //?? if (fileBean.isDirectory) {// mPresenter.enterFolder(fileBean.absPath); } else {// Intent intent = new Intent(mContext, EditorActivity.class); intent.setAction(Intent.ACTION_VIEW); //?URI?? intent.setDataAndType(Uri.fromFile(new File(fileBean.absPath)), "file"); ViewUtils.startActivity(intent, getActivity(), view, EditorActivity.SHARED_ELEMENT_NAME); } } @Override public void onItemLongClick(View view, int position) { if (mPresenter.isEditMode()) return; FileBean fileBean = files.get(position); fileBean.isSelect = !fileBean.isSelect; mAdapter.notifyItemChanged(position); view.postDelayed(() -> mPresenter.openEditMode(), 5); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.file_name: Object tag = v.getTag(R.id.tag); if (tag != null && tag instanceof Integer) {// int index = ((Integer) tag).intValue(); mPresenter.backFolder(index); } break; } } @Override public boolean hasMenu() { return true; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_folder_manager, menu); initSearchView(menu); super.onCreateOptionsMenu(menu, inflater); } /** * ?SearchView * Init search view. * * @param menu the menu */ private SearchView searchView; boolean searchViewIsShow; private void initSearchView(Menu menu) { //SearchView final MenuItem searchItem = menu.findItem(R.id.action_search); searchView = (SearchView) MenuItemCompat.getActionView(searchItem); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { BaseApplication.showSnackbar(getSwipeRefreshLayout(), "" + s); if (!Check.isEmpty(s)) { mPresenter.searchCurrentPath(s); } searchView.setIconified(false); return true; } public boolean onQueryTextChange(String s) { if (s.length() == 0) mPresenter.refreshCurrentPath(); return false; } }); searchView.setOnQueryTextFocusChangeListener((view, queryTextFocused) -> { if (queryTextFocused) searchViewIsShow = true; }); AppCompatAutoCompleteTextView editText = (AppCompatAutoCompleteTextView) searchView .findViewById(android.support.v7.appcompat.R.id.search_src_text); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_create_folder: createFolder(); break; case R.id.action_create_file: createNote(); break; } return super.onOptionsItemSelected(item); } /** * * Create file. */ private void createNote() { Intent intent = new Intent(mContext, EditorActivity.class); intent.setAction(Intent.ACTION_VIEW); //?URI?? String path = new File(mPresenter.currentPath()).getPath(); intent.setDataAndType(Uri.fromFile(new File(path)), "file"); mContext.startActivity(intent); } @OnClick(R.id.fab) public void newNote(View v) { Intent intent = new Intent(mContext, EditorActivity.class); intent.setAction(Intent.ACTION_VIEW); //?URI?? String path = new File(mPresenter.currentPath()).getPath(); intent.setDataAndType(Uri.fromFile(new File(path)), "file"); // ViewUtils.startActivity(intent, getActivity(), v, EditorActivity.SHARED_ELEMENT_COLOR_NAME); startActivity(intent); } /** * * Create folder. */ public void createFolder() { //???? View rootView = LayoutInflater.from(mContext).inflate(R.layout.view_common_input_view, null); AlertDialog mInputDialog = new AlertDialog.Builder(mContext).setTitle("").setView(rootView) .show(); TextInputLayout textInputLayout = (TextInputLayout) rootView.findViewById(R.id.inputHint); EditText text = (EditText) rootView.findViewById(R.id.text); textInputLayout.setHint("??"); rootView.findViewById(R.id.sure).setOnClickListener(v -> { String result = text.getText().toString().trim(); if (Check.isEmpty(result)) { textInputLayout.setError("?"); return; } if (mPresenter.createFoloderIsExists(result)) { textInputLayout.setError("?"); return; } mPresenter.createFolder(result); mInputDialog.dismiss(); }); rootView.findViewById(R.id.cancel).setOnClickListener(v -> { mInputDialog.dismiss(); }); mInputDialog.show(); } @Override public boolean onBackPressed() {// if (searchView != null && searchView.isShown() && searchViewIsShow) {//??? searchView.onActionViewCollapsed(); //ActionView(SearchView) searchView.setQuery("", false); // getActivity().supportInvalidateOptionsMenu();//?? searchViewIsShow = false; return true; } else { return mPresenter.backFolder(); } } /** * ?ActionModeCallBack * Init action mode. */ private void initActionMode() { pasteModeCallback = new ActionModeCallback(getActivity(), R.color.colorPrimary) { @Override public void onDestroyActionModeCustom(ActionMode mode) { mActionMode = null; } @Override public boolean onCreateActionModeCustom(ActionMode mode, Menu menu) { MenuInflater inflater = mode.getMenuInflater(); mode.setTitle("1"); inflater.inflate(R.menu.menu_action_paste, menu); return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { boolean flag = false; switch (item.getItemId()) { case R.id.action_paste: mPresenter.paste(); flag = true; break; case R.id.action_create_folder: createFolder(); flag = true; break; } return flag; } }; editModeCallback = new ActionModeCallback(getActivity(), R.color.colorPrimary) { @Override public void onDestroyActionModeCustom(ActionMode mode) { mPresenter.closeEditMode(); mActionMode = null; } @Override public boolean onCreateActionModeCustom(ActionMode mode, Menu menu) { MenuInflater inflater = mode.getMenuInflater(); // mode.setTitle("1"); inflater.inflate(R.menu.menu_action_folder, menu); menu.findItem(R.id.action_edit).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.findItem(R.id.action_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.findItem(R.id.action_copy).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.findItem(R.id.action_cut).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return true; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { boolean ret = false; switch (item.getItemId()) { case R.id.action_edit: rename(); break; case R.id.action_delete: deleteFiles(); break; case R.id.action_copy: coptFiles(); break; case R.id.action_cut: cutFiles(); break; } return ret; } }; } /** * Re name. * ???? */ private void rename() { FileBean selectBean = mPresenter.getSelectBean(); if (selectBean == null) { return; } //???? View rootView = LayoutInflater.from(mContext).inflate(R.layout.view_common_input_view, null); AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("???").setView(rootView).show(); TextInputLayout textInputLayout = (TextInputLayout) rootView.findViewById(R.id.inputHint); EditText text = (EditText) rootView.findViewById(R.id.text); text.setText(selectBean.name); text.setSelection(0, selectBean.isDirectory ? selectBean.name.length() : selectBean.name.lastIndexOf(".")); textInputLayout.setHint("" + (selectBean.isDirectory ? "??" : "??")); rootView.findViewById(R.id.sure).setOnClickListener(v -> { String result = text.getText().toString().trim(); if (!selectBean.isDirectory && !result.endsWith(".md") && !result.endsWith(".markdown") && !result.endsWith(".markd")) { textInputLayout.setError("???md|markdown|markd"); return; } if (Check.isEmpty(result)) { textInputLayout.setError("?"); return; } if (!selectBean.isDirectory && mPresenter.fileIsExists(result)) { textInputLayout.setError("?"); return; } if (selectBean.isDirectory && mPresenter.createFoloderIsExists(result)) { textInputLayout.setError("?"); return; } if (!mPresenter.rename(selectBean, result)) { textInputLayout.setError("???"); return; } textInputLayout.setErrorEnabled(false); if (mActionMode != null) { mActionMode.finish(); } dialog.dismiss(); }); rootView.findViewById(R.id.cancel).setOnClickListener(v -> { dialog.dismiss(); }); dialog.show(); } /** * * Delete file. */ private void deleteFiles() { int selectCount = mPresenter.getSelectCount(); if (selectCount <= 0) { BaseApplication.showSnackbar(getSwipeRefreshLayout(), ""); return; } AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogTheme); builder.setMessage(String.format("%d", selectCount)) .setNegativeButton("?", (dialog, which) -> { dialog.dismiss(); }).setPositiveButton("", (dialog1, which) -> { if (mPresenter.delete()) { BaseApplication.showSnackbar(getSwipeRefreshLayout(), "?"); // mAdapter.removeData(); refresh(); } if (mActionMode != null) { mActionMode.finish(); } dialog1.dismiss(); }).show(); } /** * ? * Copt files. */ private void coptFiles() { int selectCount = mPresenter.getSelectCount(); if (selectCount <= 0) { BaseApplication.showSnackbar(getSwipeRefreshLayout(), ""); return; } mPresenter.copy(); } /** * * Cut files. */ private void cutFiles() { int selectCount = mPresenter.getSelectCount(); if (selectCount <= 0) { BaseApplication.showSnackbar(getSwipeRefreshLayout(), ""); return; } mPresenter.cut(); } @Override public boolean hasNeedEvent(int type) { //?? return type == RxEvent.TYPE_REFRESH_FOLDER; } @Override public void onEventMainThread(RxEvent event) { if (event.isType(RxEvent.TYPE_REFRESH_FOLDER)) { mPresenter.refreshCurrentPath(); mPresenter.closeEditMode(); } } }