Java tutorial
/* * Copyright (C) 2013 Jozsef Szilvasi <jozsef.szilvasi@gmail.com> * * This file is part of NaturShutd. * * NaturShutd is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * NaturShutd is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NaturShutd. If not, see <http://www.gnu.org/licenses/>. */ package hu.naturlecso.naturshutd; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import butterknife.ButterKnife; import butterknife.InjectView; import butterknife.OnClick; import hu.naturlecso.naturshutd.adapter.CommandsRecyclerAdapter; import hu.naturlecso.naturshutd.model.Command; import hu.naturlecso.naturshutd.model.Host; import hu.naturlecso.naturshutd.network.SshService; import ollie.query.Select; import rx.Observable; import rx.android.app.AppObservable; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class CommandsFragment extends Fragment implements CommandsRecyclerAdapter.ButtonListener { public static final String TAG = CommandsFragment.class.getSimpleName(); public static final String HOST_ID = "host_id"; private static final long INVALID_ID = -1L; private Callbacks callbacks; private CommandsRecyclerAdapter adapter; public interface Callbacks { void onEditHost(Long id); void onEditCommand(long hostId, Long commandId); } @InjectView(R.id.command_list) View listView; @InjectView(R.id.command_empty) TextView emptyView; @InjectView(R.id.command_recyclerview) RecyclerView recyclerView; private long hostId; public CommandsFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); hostId = sp.getLong(HOST_ID, INVALID_ID); adapter = new CommandsRecyclerAdapter(this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_commands, container, false); ButterKnife.inject(this, rootView); return rootView; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); setHasOptionsMenu(true); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setItemAnimator(new DefaultItemAnimator()); Select.from(Command.class).observable().flatMap(Observable::from).map(command -> command.id) .subscribe(commandId -> adapter.add(commandId, adapter.getItemCount())); } @Override public void onDestroyView() { super.onDestroyView(); ButterKnife.reset(this); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { callbacks = (Callbacks) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement " + TAG + ".Callbacks!"); } } @Override public void onResume() { super.onResume(); toggleEmptyViewVisibility(); ((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(Host.find(Host.class, hostId).name); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.fragment_action, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_action_manage: callbacks.onEditHost(hostId); return true; default: break; } return super.onOptionsItemSelected(item); } @Override public void onCommandItemEditClicked(int position) { callbacks.onEditCommand(hostId, adapter.getCommandId(position)); } @Override public void onCommandItemExecuteClicked(int position) { // TODO execute command AppObservable.bindFragment(this, new SshService().executeCommand(hostId, adapter.getCommandId(position))) .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show(), throwable -> Toast .makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_SHORT).show()); } @OnClick(R.id.command_empty) public void onClickEmptyView() { callbacks.onEditHost(null); } @OnClick(R.id.command_fab) public void onClickAddCommand() { callbacks.onEditCommand(hostId, null); } /** * Update host object in fragment * @param id host id */ protected void updateHost(long id) { hostId = id; } protected void addCommand(long id) { adapter.add(id, 0); } protected void deleteCommand(long id) { adapter.remove(id); } protected void updateCommand(long id) { adapter.update(id); } private void toggleEmptyViewVisibility() { Integer count = Select.columns("COUNT(*)").from(Host.class).fetchValue(Integer.class); if (count < 1) { listView.setVisibility(View.INVISIBLE); emptyView.setVisibility(View.VISIBLE); } else { listView.setVisibility(View.VISIBLE); emptyView.setVisibility(View.INVISIBLE); } } }