Java tutorial
/* * 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.files; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; 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.support.v7.widget.SearchView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.ListView; import com.kccomy.orgar.R; import com.kccomy.orgar.adapter.FileListAdapter; import com.kccomy.orgar.adapter.FileSearchAdapter; import com.kccomy.orgar.adapter.RecentSearchAdapter; import com.kccomy.orgar.di.component.FileListActivityComponent; import com.kccomy.orgar.module.FileWrapper; import com.kccomy.orgar.ui.base.BaseFragment; import com.kccomy.orgar.ui.note.NoteActivity; import com.kccomy.orgar.ui.note.NoteFragment; import com.kccomy.orgar.util.NetworkUtil; import java.util.List; import javax.inject.Inject; import butterknife.BindView; import butterknife.ButterKnife; public class FileListFragment extends BaseFragment implements FileListContract.View, FileListAdapter.OnRecyclerViewItemClickListener, SwipeRefreshLayout.OnRefreshListener { @BindView(R.id.fragment_file_list_recycler) RecyclerView fileListView; @BindView(R.id.fragment_file_list_slayout) SwipeRefreshLayout swipeRefreshLayout; @BindView(R.id.fragment_file_list_rlayout_recent_search) View recentSearchView; @BindView(R.id.fragment_file_list_recent_search_lv) ListView recentListView; @BindView(R.id.fragment_file_list_search_result) View searchResultView; @BindView(R.id.fragment_file_list_search_result_recycler) RecyclerView searchResultRecyclerView; @Inject LayoutInflater layoutInflater; @Inject FileListPresenter presenter; @Inject FileListAdapter fileAdapter; @Inject FileSearchAdapter searchAdapter; @Inject RecentSearchAdapter recentAdapter; @Inject AlertDialog.Builder alertDialogBuilder; private EditText newEdt; private AlertDialog alertDialog; private FloatingActionButton fab; private SearchView searchView; @Inject public FileListFragment() { } @Override protected int getLayoutResId() { return R.layout.fragment_file_list; } @Override protected void initVariables() { getComponent(FileListActivityComponent.class).inject(this); presenter.setView(this); fab = (FloatingActionButton) getActivity().findViewById(R.id.fab); fileAdapter.setRecyclerViewItemClickListener(this); searchAdapter.setFileSearchResultClickListener(searchResultListener); } @Override protected void initViews(View view, Bundle savedInstanceState) { setHasOptionsMenu(true); /* fab? */ fab.show(); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { alertDialog.show(); } }); /* ? */ swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.colorPrimary)); swipeRefreshLayout.setOnRefreshListener(this); /* ??? */ recentSearchView.setVisibility(View.GONE); searchResultView.setVisibility(View.GONE); /* adapter */ recentListView.setAdapter(recentAdapter); fileListView.setAdapter(fileAdapter); fileListView.setLayoutManager(new GridLayoutManager(getContext(), 2)); fileListView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)); searchResultRecyclerView.setAdapter(searchAdapter); searchResultRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); /* ?Dialog */ View dialogView = layoutInflater.inflate(R.layout.diaglog_new_file, null); newEdt = ButterKnife.findById(dialogView, R.id.edt_new_file_name); alertDialog = alertDialogBuilder.setTitle(R.string.dialog_new_file_title) .setPositiveButton(R.string.dialog_action_ok, dialogListener) .setNegativeButton(R.string.dialog_action_cancel, null).setView(dialogView).create(); } @Override public void onResume() { super.onResume(); presenter.load(); presenter.loadRecentKeys(); } @Override public void onPause() { super.onPause(); } @Override public void onStop() { presenter.saveRecentKeys(recentAdapter.getData()); super.onStop(); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.toolbar_file_list, menu); super.onCreateOptionsMenu(menu, inflater); } @Override public void onPrepareOptionsMenu(Menu menu) { MenuItem search = menu.findItem(R.id.action_search_file); search.collapseActionView(); searchView = (SearchView) search.getActionView(); // Don't cover App Label searchView.setMaxWidth(600); searchView.setIconified(true); searchView.setIconifiedByDefault(true); // set input text color SearchView.SearchAutoComplete textView = (SearchView.SearchAutoComplete) searchView .findViewById(R.id.search_src_text); textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark)); searchView.setOnSearchClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showRecentSearchView(); fab.hide(); } }); searchView.setOnCloseListener(new SearchView.OnCloseListener() { @Override public boolean onClose() { showFileListView(); fab.show(); return false; } }); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { recentAdapter.addItem(query); return false; } @Override public boolean onQueryTextChange(String newText) { if (!TextUtils.isEmpty(newText)) { showSearchResultView(); presenter.processQuery(fileAdapter.getFiles(), newText); } else { showRecentSearchView(); } return false; } }); super.onPrepareOptionsMenu(menu); } @Override public void onRefresh() { showRefreshing(true); if (NetworkUtil.isConnected(getContext())) { presenter.syncDownload(); presenter.syncUpload(); } else { showRefreshing(false); } } @Override public void addItem(FileWrapper file) { fileAdapter.add(file, 0); fileListView.scrollToPosition(0); } @Override public void removeItem(FileWrapper file) { fileAdapter.remove(file); } @Override public void onItemClick(FileWrapper file) { showNoteActivity(file.getName()); } public boolean onContextItemSelected(MenuItem item) { int position; try { position = fileAdapter.getPosition(); } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage(), e); return super.onContextItemSelected(item); } FileWrapper wrapper = fileAdapter.getByPosition(position); presenter.delete(wrapper); return super.onContextItemSelected(item); } @Override public void showOrgFiles(List<FileWrapper> files) { Log.d(TAG, "showOrgFiles"); fileAdapter.replaceData(files); } private void showRecentSearchView() { recentSearchView.setVisibility(View.VISIBLE); swipeRefreshLayout.setVisibility(View.GONE); searchResultView.setVisibility(View.GONE); } private void showSearchResultView() { searchResultView.setVisibility(View.VISIBLE); swipeRefreshLayout.setVisibility(View.GONE); recentSearchView.setVisibility(View.GONE); } @Override public void replaceRecentKeys(List<String> keys) { recentAdapter.setData(keys); } private void showFileListView() { swipeRefreshLayout.setVisibility(View.VISIBLE); recentSearchView.setVisibility(View.GONE); searchResultView.setVisibility(View.GONE); } private void showNoteTreeFragment(String name) { Fragment noteTreeFragment = new NoteFragment(); Bundle bundle = new Bundle(); bundle.putSerializable("FILE_NAME", name); noteTreeFragment.setArguments(bundle); getHoldingActivity().addFragment(noteTreeFragment, true); } @Override public void showDeleteError() { showToastResId(R.string.toast_delete_file_error); } @Override public void showRefreshing(boolean refreshing) { swipeRefreshLayout.setRefreshing(refreshing); } @Override public void showRefreshError() { } @Override public void showNewNoteError() { showToastResId(R.string.toast_new_note_error); } @Override public void showNewNoteExist(String name) { showToastMessage("This file " + name + " already exists"); } @Override public void showNoteActivity(String name) { Intent intent = NoteActivity.getCallingIntent(getContext(), name); startActivity(intent); } @Override public void showSearchResult(List<FileWrapper> result) { searchAdapter.replaceData(result); } private FileSearchAdapter.OnFileSearchResultClickListener searchResultListener = new FileSearchAdapter.OnFileSearchResultClickListener() { @Override public void onItemClick(String name) { InputMethodManager imm = (InputMethodManager) getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive()) { imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } showNoteActivity(name); } }; private DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String name = newEdt.getText().toString(); Log.d(TAG, "Dialog input text: " + name); if (TextUtils.isEmpty(name)) { showToastResId(R.string.toast_new_file_empty_name); return; } presenter.newNote(name); newEdt.setText(""); } }; }