Improved fab speed dial

This commit is contained in:
Christian Basler 2017-07-26 21:01:07 +02:00
parent faa6752b10
commit e064012551
15 changed files with 187 additions and 212 deletions

View File

@ -36,11 +36,12 @@ import com.google.zxing.integration.android.IntentIntegrator;
import java.util.LinkedList;
import java.util.List;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.repository.AndroidAddressRepository;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.FabUtils;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
import io.github.kobakei.materialfabspeeddial.FabSpeedDialMenu;
/**
* Fragment that shows a list of all contacts, the ones we subscribed to first.
@ -119,8 +120,31 @@ public class AddressListFragment extends AbstractItemListFragment<Void, Bitmessa
@Override
public void onAttach(Context ctx) {
super.onAttach(ctx);
if (ctx instanceof ActionBarListener) {
((ActionBarListener) ctx).updateTitle(getString(R.string.contacts_and_subscriptions));
if (ctx instanceof MainActivity) {
MainActivity activity = (MainActivity) ctx;
activity.updateTitle(getString(R.string.contacts_and_subscriptions));
FabSpeedDialMenu menu = new FabSpeedDialMenu(ctx);
menu.add(R.string.scan_qr_code).setIcon(R.drawable.ic_action_qr_code);
menu.add(R.string.create_contact).setIcon(R.drawable.ic_action_create_contact);
FabUtils.initFab(activity, R.drawable.ic_action_add_contact, menu)
.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
@Override
public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) {
switch (itemId) {
case 1:
IntentIntegrator.forSupportFragment(AddressListFragment.this)
.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
.initiateScan();
break;
case 2:
Intent intent = new Intent(getActivity(), CreateAddressActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
}
@ -128,29 +152,7 @@ public class AddressListFragment extends AbstractItemListFragment<Void, Bitmessa
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_address_list, container, false);
FabSpeedDial fabSpeedDial = (FabSpeedDial) view.findViewById(R.id.fab_add_contact);
fabSpeedDial.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
@Override
public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) {
switch (itemId) {
case R.id.action_read_qr_code:
IntentIntegrator.forSupportFragment(AddressListFragment.this)
.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
.initiateScan();
break;
case R.id.action_create_contact:
Intent intent = new Intent(getActivity(), CreateAddressActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
return view;
return inflater.inflate(R.layout.fragment_address_list, container, false);
}
@Override

View File

@ -56,7 +56,6 @@ import java.util.List;
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity;
import ch.dissem.apps.abit.drawer.ProfileImageListener;
import ch.dissem.apps.abit.drawer.ProfileSelectionListener;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.listener.ListSelectionListener;
import ch.dissem.apps.abit.service.BitmessageService;
import ch.dissem.apps.abit.service.Singleton;
@ -67,6 +66,7 @@ import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.valueobject.Label;
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
import static ch.dissem.apps.abit.ComposeMessageActivity.launchReplyTo;
import static ch.dissem.apps.abit.repository.AndroidMessageRepository.LABEL_ARCHIVE;
@ -91,7 +91,7 @@ import static ch.dissem.apps.abit.service.BitmessageService.isRunning;
* </p>
*/
public class MainActivity extends AppCompatActivity
implements ListSelectionListener<Serializable>, ActionBarListener {
implements ListSelectionListener<Serializable> {
public static final String EXTRA_SHOW_MESSAGE = "ch.dissem.abit.ShowMessage";
public static final String EXTRA_SHOW_LABEL = "ch.dissem.abit.ShowLabel";
public static final String EXTRA_REPLY_TO_MESSAGE = "ch.dissem.abit.ReplyToMessage";
@ -120,13 +120,17 @@ public class MainActivity extends AppCompatActivity
private Drawer drawer;
private SwitchDrawerItem nodeSwitch;
private FabSpeedDial fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = new WeakReference<>(this);
bmc = Singleton.getBitmessageContext(this);
setContentView(R.layout.activity_message_list);
setContentView(R.layout.activity_main);
fab = (FabSpeedDial) findViewById(R.id.fab);
fab.hide();
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@ -487,7 +491,6 @@ public class MainActivity extends AppCompatActivity
}
}
@Override
public void updateUnread() {
for (IDrawerItem item : drawer.getDrawerItems()) {
if (item.getTag() instanceof Label) {
@ -573,7 +576,6 @@ public class MainActivity extends AppCompatActivity
}
}
@Override
public void updateTitle(CharSequence title) {
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
@ -584,6 +586,10 @@ public class MainActivity extends AppCompatActivity
return selectedLabel;
}
public FabSpeedDial getFloatingActionButton() {
return fab;
}
public static MainActivity getInstance() {
if (instance == null) return null;
return instance.get();

View File

@ -43,7 +43,6 @@ import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.Assets;
import ch.dissem.apps.abit.util.Drawables;
@ -142,8 +141,8 @@ public class MessageDetailFragment extends Fragment {
}
MessageRepository messageRepo = Singleton.getMessageRepository(inflater.getContext());
if (removed) {
if (getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateUnread();
if (getActivity() instanceof MainActivity) {
((MainActivity) getActivity()).updateUnread();
}
messageRepo.save(item);
}
@ -200,13 +199,13 @@ public class MessageDetailFragment extends Fragment {
case R.id.mark_unread:
item.addLabels(messageRepo.getLabels(Label.Type.UNREAD));
messageRepo.save(item);
if (getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateUnread();
if (getActivity() instanceof MainActivity) {
((MainActivity) getActivity()).updateUnread();
}
return true;
case R.id.archive:
if (item.isUnread() && getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateUnread();
if (item.isUnread() && getActivity() instanceof MainActivity) {
((MainActivity) getActivity()).updateUnread();
}
item.getLabels().clear();
messageRepo.save(item);

View File

@ -16,6 +16,7 @@
package ch.dissem.apps.abit;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
@ -45,14 +46,15 @@ import java.util.List;
import java.util.Stack;
import ch.dissem.apps.abit.adapter.SwipeableMessageAdapter;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.listener.ListSelectionListener;
import ch.dissem.apps.abit.repository.AndroidMessageRepository;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.FabUtils;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.entity.valueobject.Label;
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
import io.github.kobakei.materialfabspeeddial.FabSpeedDialMenu;
import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_BROADCAST;
import static ch.dissem.apps.abit.ComposeMessageActivity.EXTRA_IDENTITY;
@ -119,8 +121,8 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
private void doUpdateList(final Label label) {
adapter.clear(label);
if (label == null) {
if (getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateTitle(getString(R.string.app_name));
if (getActivity() instanceof MainActivity) {
((MainActivity) getActivity()).updateTitle(getString(R.string.app_name));
}
adapter.notifyDataSetChanged();
return;
@ -129,8 +131,8 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
if (emptyTrashMenuItem != null) {
emptyTrashMenuItem.setVisible(label.getType() == Label.Type.TRASH);
}
if (getActivity() instanceof ActionBarListener) {
ActionBarListener actionBarListener = (ActionBarListener) getActivity();
if (getActivity() instanceof MainActivity) {
MainActivity actionBarListener = (MainActivity) getActivity();
if ("archive".equals(label.toString())) {
actionBarListener.updateTitle(getString(R.string.archive));
} else {
@ -167,38 +169,6 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
// Show the dummy content as text in a TextView.
FabSpeedDial fab = (FabSpeedDial) rootView.findViewById(R.id
.fab_compose_message);
fab.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
@Override
public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) {
BitmessageAddress identity = Singleton.getIdentity(getActivity());
if (identity == null) {
Toast.makeText(getActivity(), R.string.no_identity_warning,
Toast.LENGTH_LONG).show();
} else {
switch (itemId) {
case R.id.action_compose_message: {
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
intent.putExtra(EXTRA_IDENTITY, identity);
startActivity(intent);
break;
}
case R.id.action_compose_broadcast: {
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
intent.putExtra(EXTRA_IDENTITY, identity);
intent.putExtra(EXTRA_BROADCAST, true);
startActivity(intent);
break;
}
default:
break;
}
}
}
});
// touch guard manager (this class is required to suppress scrolling while swipe-dismiss
// animation is running)
recyclerViewTouchActionGuardManager = new RecyclerViewTouchActionGuardManager();
@ -269,6 +239,45 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
return rootView;
}
@Override
public void onAttach(Context context) {
if (context instanceof MainActivity) {
FabSpeedDialMenu menu = new FabSpeedDialMenu(context);
menu.add(R.string.broadcast).setIcon(R.drawable.ic_action_broadcast);
menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal);
FabUtils.initFab((MainActivity) context, R.drawable.ic_action_compose_message, menu)
.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
@Override
public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) {
BitmessageAddress identity = Singleton.getIdentity(getActivity());
if (identity == null) {
Toast.makeText(getActivity(), R.string.no_identity_warning,
Toast.LENGTH_LONG).show();
} else {
switch (itemId) {
case 1: {
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
intent.putExtra(EXTRA_IDENTITY, identity);
startActivity(intent);
break;
}
case 2: {
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
intent.putExtra(EXTRA_IDENTITY, identity);
intent.putExtra(EXTRA_BROADCAST, true);
startActivity(intent);
break;
}
default:
break;
}
}
}
});
}
super.onAttach(context);
}
@Override
public void onDestroyView() {
if (recyclerViewSwipeManager != null) {

View File

@ -24,12 +24,12 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.View;
import android.widget.Toast;
import com.mikepenz.aboutlibraries.Libs;
import com.mikepenz.aboutlibraries.LibsBuilder;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.synchronization.SyncAdapter;
import ch.dissem.bitmessage.BitmessageContext;
@ -121,11 +121,14 @@ public class SettingsFragment
@Override
public void onAttach(Context ctx) {
super.onAttach(ctx);
if (ctx instanceof MainActivity){
((MainActivity) ctx).getFloatingActionButton().hide();
}
PreferenceManager.getDefaultSharedPreferences(ctx)
.registerOnSharedPreferenceChangeListener(this);
if (ctx instanceof ActionBarListener) {
((ActionBarListener) ctx).updateTitle(getString(R.string.settings));
if (ctx instanceof MainActivity) {
((MainActivity) ctx).updateTitle(getString(R.string.settings));
}
}

View File

@ -1,26 +0,0 @@
/*
* Copyright 2015 Christian Basler
*
* 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 ch.dissem.apps.abit.listener;
/**
* @author Christian Basler
*/
public interface ActionBarListener {
void updateTitle(CharSequence title);
void updateUnread();
}

View File

@ -0,0 +1,35 @@
package ch.dissem.apps.abit.util;
import android.support.annotation.DrawableRes;
import android.support.design.widget.FloatingActionButton;
import ch.dissem.apps.abit.MainActivity;
import ch.dissem.apps.abit.R;
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
import io.github.kobakei.materialfabspeeddial.FabSpeedDialMenu;
/**
* Utilities to work with the common floating action button in the main activity
*/
public class FabUtils {
public static FabSpeedDial initFab(MainActivity activity, final @DrawableRes int drawableRes, FabSpeedDialMenu menu) {
FabSpeedDial fab = activity.getFloatingActionButton();
fab.show();
fab.closeMenu();
final FloatingActionButton mainFab = fab.getMainFab();
mainFab.setImageResource(drawableRes);
fab.setMenu(menu);
fab.addOnStateChangeListener(new FabSpeedDial.OnStateChangeListener() {
@Override
public void onStateChange(boolean isOpened) {
if (isOpened) {
// It will be turned 45 degrees, which makes an x out of the +
mainFab.setImageResource(R.drawable.ic_action_add);
} else {
mainFab.setImageResource(drawableRes);
}
}
});
return fab;
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#DEFFFFFF"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>

View File

@ -58,4 +58,15 @@
tools:ignore="InconsistentLayout"
tools:layout="@layout/fragment_message_detail" />
<io.github.kobakei.materialfabspeeddial.FabSpeedDial
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:elevation="8dp"
app:fab_useTouchGuard="true"/>
</android.support.constraint.ConstraintLayout>

View File

@ -0,0 +1,35 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
tools:ignore="UnusedAttribute" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
tools:context=".MessageListActivity"
tools:layout="@android:layout/list_content" />
<io.github.kobakei.materialfabspeeddial.FabSpeedDial
android:id="@+id/fab"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:fab_useTouchGuard="true"
app:fab_miniFabTextBackground="@color/colorAccent"
app:fab_miniFabTextColor="@color/colorPrimaryDarkText" />
</RelativeLayout>

View File

@ -1,26 +0,0 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"
tools:ignore="UnusedAttribute"/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_below="@id/toolbar"
android:id="@+id/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MessageListActivity"
tools:layout="@android:layout/list_content"/>
</RelativeLayout>

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -17,17 +16,4 @@
android:paddingBottom="88dp"
android:scrollbarStyle="outsideOverlay" />
<io.github.kobakei.materialfabspeeddial.FabSpeedDial
android:id="@+id/fab_add_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:elevation="8dp"
app:fab_useTouchGuard="true"
app:fab_miniFabTextBackground="@color/colorPrimaryLight"
app:fab_fabDrawable="@drawable/ic_action_add_contact"
app:fab_menu="@menu/fab_address" />
</RelativeLayout>

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -19,17 +18,4 @@
android:scrollbars="vertical"
tools:listitem="@layout/message_row"/>
<io.github.kobakei.materialfabspeeddial.FabSpeedDial
android:id="@+id/fab_compose_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:elevation="8dp"
app:fab_useTouchGuard="true"
app:fab_miniFabTextBackground="@color/colorPrimaryLight"
app:fab_fabDrawable="@drawable/ic_action_compose_message"
app:fab_menu="@menu/fab_message"/>
</RelativeLayout>

View File

@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Christian Basler
~
~ 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.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_read_qr_code"
android:icon="@drawable/ic_action_qr_code"
android:title="@string/scan_qr_code"/>
<item
android:id="@+id/action_create_contact"
android:icon="@drawable/ic_action_create_contact"
android:title="@string/create_contact"/>
</menu>

View File

@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2016 Christian Basler
~
~ 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.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_compose_broadcast"
android:icon="@drawable/ic_action_broadcast"
android:title="@string/broadcast"/>
<item
android:id="@+id/action_compose_message"
android:icon="@drawable/ic_action_personal"
android:title="@string/personal_message"/>
</menu>