Fixed FAB

This commit is contained in:
Christian Basler 2017-08-10 00:40:44 +02:00
parent ec3009a257
commit e2aa0e8b1d
6 changed files with 95 additions and 106 deletions

View File

@ -75,7 +75,7 @@ dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.5.0@aar' compile 'com.journeyapps:zxing-android-embedded:3.5.0@aar'
compile 'com.google.zxing:core:3.3.0' compile 'com.google.zxing:core:3.3.0'
compile 'com.github.kobakei:MaterialFabSpeedDial:1.1.4' compile 'com.github.kobakei:MaterialFabSpeedDial:1.1.5'
compile 'com.github.amlcurran.showcaseview:library:5.4.3' compile 'com.github.amlcurran.showcaseview:library:5.4.3'
compile('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.10.4@aar') { compile('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.10.4@aar') {
transitive = true transitive = true

View File

@ -91,6 +91,7 @@ public class AddressListFragment extends AbstractItemListFragment<Void, Bitmessa
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
initFab((MainActivity) getActivity());
updateList(); updateList();
} }
@ -117,35 +118,30 @@ public class AddressListFragment extends AbstractItemListFragment<Void, Bitmessa
}.execute(); }.execute();
} }
@Override private void initFab(MainActivity activity){
public void onAttach(Context ctx) { activity.updateTitle(getString(R.string.contacts_and_subscriptions));
super.onAttach(ctx); FabSpeedDialMenu menu = new FabSpeedDialMenu(activity);
if (ctx instanceof MainActivity) { menu.add(R.string.scan_qr_code).setIcon(R.drawable.ic_action_qr_code);
MainActivity activity = (MainActivity) ctx; menu.add(R.string.create_contact).setIcon(R.drawable.ic_action_create_contact);
activity.updateTitle(getString(R.string.contacts_and_subscriptions)); FabUtils.initFab(activity, R.drawable.ic_action_add_contact, menu)
FabSpeedDialMenu menu = new FabSpeedDialMenu(ctx); .addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
menu.add(R.string.scan_qr_code).setIcon(R.drawable.ic_action_qr_code); @Override
menu.add(R.string.create_contact).setIcon(R.drawable.ic_action_create_contact); public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) {
FabUtils.initFab(activity, R.drawable.ic_action_add_contact, menu) switch (itemId) {
.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() { case 1:
@Override IntentIntegrator.forSupportFragment(AddressListFragment.this)
public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) { .setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES)
switch (itemId) { .initiateScan();
case 1: break;
IntentIntegrator.forSupportFragment(AddressListFragment.this) case 2:
.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES) Intent intent = new Intent(getActivity(), CreateAddressActivity.class);
.initiateScan(); startActivity(intent);
break; break;
case 2: default:
Intent intent = new Intent(getActivity(), CreateAddressActivity.class); break;
startActivity(intent);
break;
default:
break;
}
} }
}); }
} });
} }
@Nullable @Nullable

View File

@ -96,6 +96,7 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
MainActivity activity = (MainActivity) getActivity(); MainActivity activity = (MainActivity) getActivity();
initFab(activity);
messageRepo = Singleton.getMessageRepository(activity); messageRepo = Singleton.getMessageRepository(activity);
if (backStack.isEmpty()) { if (backStack.isEmpty()) {
@ -239,43 +240,39 @@ public class MessageListFragment extends Fragment implements ListHolder<Label> {
return rootView; return rootView;
} }
@Override private void initFab(MainActivity context){
public void onAttach(Context context) { FabSpeedDialMenu menu = new FabSpeedDialMenu(context);
if (context instanceof MainActivity) { menu.add(R.string.broadcast).setIcon(R.drawable.ic_action_broadcast);
FabSpeedDialMenu menu = new FabSpeedDialMenu(context); menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal);
menu.add(R.string.broadcast).setIcon(R.drawable.ic_action_broadcast); FabUtils.initFab(context, R.drawable.ic_action_compose_message, menu)
menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal); .addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() {
FabUtils.initFab((MainActivity) context, R.drawable.ic_action_compose_message, menu) @Override
.addOnMenuItemClickListener(new FabSpeedDial.OnMenuItemClickListener() { public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) {
@Override BitmessageAddress identity = Singleton.getIdentity(getActivity());
public void onMenuItemClick(FloatingActionButton floatingActionButton, @Nullable TextView textView, int itemId) { if (identity == null) {
BitmessageAddress identity = Singleton.getIdentity(getActivity()); Toast.makeText(getActivity(), R.string.no_identity_warning,
if (identity == null) { Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), R.string.no_identity_warning, } else {
Toast.LENGTH_LONG).show(); switch (itemId) {
} else { case 1: {
switch (itemId) { Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
case 1: { intent.putExtra(EXTRA_IDENTITY, identity);
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class); intent.putExtra(EXTRA_BROADCAST, true);
intent.putExtra(EXTRA_IDENTITY, identity); startActivity(intent);
startActivity(intent); break;
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;
} }
case 2: {
Intent intent = new Intent(getActivity(), ComposeMessageActivity.class);
intent.putExtra(EXTRA_IDENTITY, identity);
startActivity(intent);
break;
}
default:
break;
} }
} }
}); }
} });
super.onAttach(context);
} }
@Override @Override

View File

@ -1,35 +0,0 @@
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,34 @@
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
*/
object FabUtils {
@JvmStatic
fun initFab(activity: MainActivity, @DrawableRes drawableRes: Int, menu: FabSpeedDialMenu): FabSpeedDial {
val fab = activity.floatingActionButton
fab.removeAllOnMenuItemClickListeners()
fab.show()
fab.closeMenu()
val mainFab = fab.mainFab
mainFab.setImageResource(drawableRes)
fab.setMenu(menu)
fab.addOnStateChangeListener { 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

@ -60,13 +60,10 @@
<io.github.kobakei.materialfabspeeddial.FabSpeedDial <io.github.kobakei.materialfabspeeddial.FabSpeedDial
android:id="@+id/fab" android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_gravity="bottom|end" app:fab_useTouchGuard="true"
android:layout_margin="16dp" app:fab_miniFabTextBackground="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent" app:fab_miniFabTextColor="@color/colorPrimaryDarkText" />
app:layout_constraintEnd_toEndOf="parent"
app:elevation="8dp"
app:fab_useTouchGuard="true"/>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>