1
0
mirror of https://github.com/moparisthebest/Yaaic synced 2024-11-25 02:12:16 -05:00

Used shared interface for fragments accessing main activity. Set title of activity from fragments.

This commit is contained in:
Sebastian Kaspari 2015-04-04 14:01:57 +02:00
parent fc6516e1d3
commit 6ee5ed0eff
5 changed files with 92 additions and 21 deletions

View File

@ -17,7 +17,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Yaaic. If not, see <http://www.gnu.org/licenses/>. along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.yaaic.activity; package org.yaaic.activity;
import android.app.Fragment; import android.app.Fragment;
@ -31,6 +31,7 @@ import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -50,7 +51,7 @@ import org.yaaic.model.Status;
/** /**
* The main activity of Yaaic. We'll add, remove and replace fragments here. * The main activity of Yaaic. We'll add, remove and replace fragments here.
*/ */
public class MainActivity extends ActionBarActivity implements OverviewFragment.Callback, ServiceConnection { public class MainActivity extends ActionBarActivity implements YaaicActivity, ServiceConnection {
private ActionBarDrawerToggle toggle; private ActionBarDrawerToggle toggle;
private Toolbar toolbar; private Toolbar toolbar;
private DrawerLayout drawer; private DrawerLayout drawer;
@ -184,6 +185,11 @@ public class MainActivity extends ActionBarActivity implements OverviewFragment.
return binder; return binder;
} }
@Override
public void setToolbarTitle(String title) {
toolbar.setTitle(title);
}
@Override @Override
public void onServiceConnected(ComponentName name, IBinder service) { public void onServiceConnected(ComponentName name, IBinder service) {
binder = (IRCBinder) service; binder = (IRCBinder) service;

View File

@ -0,0 +1,35 @@
/*
Yaaic - Yet Another Android IRC Client
Copyright 2009-2015 Sebastian Kaspari
This file is part of Yaaic.
Yaaic 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.
Yaaic 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 Yaaic. If not, see <http://www.gnu.org/licenses/>.
*/
package org.yaaic.activity;
import org.yaaic.irc.IRCBinder;
import org.yaaic.model.Server;
/**
* Interface for fragments accessing functionality of the main activity.
*/
public interface YaaicActivity {
IRCBinder getBinder();
void setToolbarTitle(String title);
void onServerSelected(Server server);
}

View File

@ -56,6 +56,7 @@ import org.yaaic.Yaaic;
import org.yaaic.activity.JoinActivity; import org.yaaic.activity.JoinActivity;
import org.yaaic.activity.UserActivity; import org.yaaic.activity.UserActivity;
import org.yaaic.activity.UsersActivity; import org.yaaic.activity.UsersActivity;
import org.yaaic.activity.YaaicActivity;
import org.yaaic.adapter.ConversationPagerAdapter; import org.yaaic.adapter.ConversationPagerAdapter;
import org.yaaic.adapter.MessageListAdapter; import org.yaaic.adapter.MessageListAdapter;
import org.yaaic.command.CommandParser; import org.yaaic.command.CommandParser;
@ -101,6 +102,8 @@ public class ConversationFragment extends Fragment implements ServerListener, Co
private ConversationReceiver channelReceiver; private ConversationReceiver channelReceiver;
private ServerReceiver serverReceiver; private ServerReceiver serverReceiver;
private YaaicActivity activity;
private EditText input; private EditText input;
private ViewPager pager; private ViewPager pager;
private ConversationPagerAdapter pagerAdapter; private ConversationPagerAdapter pagerAdapter;
@ -161,6 +164,17 @@ public class ConversationFragment extends Fragment implements ServerListener, Co
} }
}; };
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof YaaicActivity)) {
throw new IllegalArgumentException("Activity has to implement YaaicActivity interface");
}
this.activity = (YaaicActivity) activity;
}
/** /**
* On create * On create
*/ */
@ -171,7 +185,8 @@ public class ConversationFragment extends Fragment implements ServerListener, Co
serverId = getArguments().getInt("serverId"); serverId = getArguments().getInt("serverId");
server = Yaaic.getInstance().getServerById(serverId); server = Yaaic.getInstance().getServerById(serverId);
// Create a new scrollback history activity.setToolbarTitle(server.getTitle());
scrollback = new Scrollback(); scrollback = new Scrollback();
} }

View File

@ -33,17 +33,16 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.Toast; import android.widget.Toast;
import org.yaaic.R; import org.yaaic.R;
import org.yaaic.Yaaic; import org.yaaic.Yaaic;
import org.yaaic.activity.AddServerActivity; import org.yaaic.activity.AddServerActivity;
import org.yaaic.activity.YaaicActivity;
import org.yaaic.adapter.ServersAdapter; import org.yaaic.adapter.ServersAdapter;
import org.yaaic.db.Database; import org.yaaic.db.Database;
import org.yaaic.irc.IRCBinder; import org.yaaic.irc.IRCBinder;
import org.yaaic.irc.IRCService;
import org.yaaic.listener.ServerListener; import org.yaaic.listener.ServerListener;
import org.yaaic.model.Broadcast; import org.yaaic.model.Broadcast;
import org.yaaic.model.Extra; import org.yaaic.model.Extra;
@ -57,27 +56,26 @@ import org.yaaic.receiver.ServerReceiver;
public class OverviewFragment extends Fragment implements ServerListener, ServersAdapter.ClickListener, View.OnClickListener { public class OverviewFragment extends Fragment implements ServerListener, ServersAdapter.ClickListener, View.OnClickListener {
public static final String TRANSACTION_TAG = "fragment_overview"; public static final String TRANSACTION_TAG = "fragment_overview";
/**
* Callback interface to be implemented by Activities using this fragment.
*/
public interface Callback {
void onServerSelected(Server server);
IRCBinder getBinder();
}
private ServersAdapter adapter; private ServersAdapter adapter;
private Callback callback; private YaaicActivity activity;
private BroadcastReceiver receiver; private BroadcastReceiver receiver;
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
if (!(activity instanceof Callback)) { if (!(activity instanceof YaaicActivity)) {
throw new IllegalArgumentException("Activity has to implement Callback interface"); throw new IllegalArgumentException("Activity has to implement YaaicActivity interface");
} }
this.callback = (Callback) activity; this.activity = (YaaicActivity) activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity.setToolbarTitle(getString(R.string.app_name));
} }
@Nullable @Nullable
@ -124,12 +122,12 @@ public class OverviewFragment extends Fragment implements ServerListener, Server
@Override @Override
public void onServerSelected(Server server) { public void onServerSelected(Server server) {
callback.onServerSelected(server); activity.onServerSelected(server);
} }
@Override @Override
public void onConnectToServer(Server server) { public void onConnectToServer(Server server) {
IRCBinder binder = callback.getBinder(); IRCBinder binder = activity.getBinder();
if (binder != null && server.getStatus() == Status.DISCONNECTED) { if (binder != null && server.getStatus() == Status.DISCONNECTED) {
binder.connect(server); binder.connect(server);
@ -140,7 +138,7 @@ public class OverviewFragment extends Fragment implements ServerListener, Server
@Override @Override
public void onDisconnectFromServer(Server server) { public void onDisconnectFromServer(Server server) {
IRCBinder binder = callback.getBinder(); IRCBinder binder = activity.getBinder();
if (binder != null) { if (binder != null) {
server.clearConversations(); server.clearConversations();
@ -164,7 +162,7 @@ public class OverviewFragment extends Fragment implements ServerListener, Server
@Override @Override
public void onDeleteServer(Server server) { public void onDeleteServer(Server server) {
IRCBinder binder = callback.getBinder(); IRCBinder binder = activity.getBinder();
if (binder != null) { if (binder != null) {
binder.getService().getConnection(server.getId()).quitServer(); binder.getService().getConnection(server.getId()).quitServer();

View File

@ -1,9 +1,11 @@
package org.yaaic.fragment; package org.yaaic.fragment;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import org.yaaic.R; import org.yaaic.R;
import org.yaaic.activity.YaaicActivity;
/** /**
* Fragment displaying all settings. * Fragment displaying all settings.
@ -11,10 +13,25 @@ import org.yaaic.R;
public class SettingsFragment extends PreferenceFragment { public class SettingsFragment extends PreferenceFragment {
public static final String TRANSACTION_TAG = "fragment_settings"; public static final String TRANSACTION_TAG = "fragment_settings";
private YaaicActivity activity;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof YaaicActivity)) {
throw new IllegalArgumentException("Activity has to implement YaaicActivity interface");
}
this.activity = (YaaicActivity) activity;
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
activity.setToolbarTitle(getString(R.string.navigation_settings));
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
} }
} }