diff options
author | stefan-niedermann <info@niedermann.it> | 2019-10-19 18:06:55 +0300 |
---|---|---|
committer | stefan-niedermann <info@niedermann.it> | 2019-10-19 18:06:55 +0300 |
commit | 028ed85cc5fc9c9c92e1182f811001b2ce07c968 (patch) | |
tree | ee9459503897fe67d8bb5fab1689d2403e357928 | |
parent | 06edaa157a441056aa5acaf736bae2beeadb2eb2 (diff) |
Use interaces for Activity / Fragment communication
3 files changed, 64 insertions, 26 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java index bd90ba508..7a934400e 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java @@ -58,7 +58,7 @@ import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.BUNDLE_KEY_STACK_ import static it.niedermann.nextcloud.deck.ui.card.CardAdapter.NO_LOCAL_ID; import static it.niedermann.nextcloud.deck.ui.stack.EditStackDialogFragment.NO_STACK_ID; -public class MainActivity extends DrawerActivity { +public class MainActivity extends DrawerActivity implements EditStackDialogFragment.EditStackListener, EditBoardDialogFragment.EditBoardListener { @BindView(R.id.swipe_refresh_layout) @@ -186,6 +186,7 @@ public class MainActivity extends DrawerActivity { })); } + @Override public void onCreateStack(String stackName) { observeOnce(syncManager.getStacksForBoard(account.getId(), currentBoardId), MainActivity.this, fullStacks -> { // FIXME fullStacks.size() is always 0 Stack s = new Stack(); @@ -208,6 +209,7 @@ public class MainActivity extends DrawerActivity { }); } + @Override public void onUpdateStack(long localStackId, String stackName) { observeOnce(syncManager.getStack(account.getId(), localStackId), MainActivity.this, fullStack -> { fullStack.getStack().setTitle(stackName); @@ -215,6 +217,7 @@ public class MainActivity extends DrawerActivity { }); } + @Override public void onCreateBoard(String title, String color) { Board b = new Board(); b.setTitle(title); @@ -239,6 +242,7 @@ public class MainActivity extends DrawerActivity { }); } + @Override public void onUpdateBoard(FullBoard fullBoard) { syncManager.updateBoard(fullBoard); } @@ -372,9 +376,7 @@ public class MainActivity extends DrawerActivity { * @param board Board */ private void displayStacksForBoard(Board board, Account account) { - if (toolbar != null) { - toolbar.setTitle(board.getTitle()); - } + toolbar.setTitle(board.getTitle()); currentBoardHasEditPermission = board.isPermissionEdit(); if (currentBoardHasEditPermission) { @@ -455,6 +457,7 @@ public class MainActivity extends DrawerActivity { .show(getSupportFragmentManager(), addColumn); break; case R.id.action_card_list_rename_column: + // TODO call newInstance with old stack name EditStackDialogFragment.newInstance(stackAdapter.getItem(viewPager.getCurrentItem()).getStackId()) .show(getSupportFragmentManager(), actionCardListRenameColumn); break; diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/EditBoardDialogFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/EditBoardDialogFragment.java index 4a83a547a..655d85342 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/EditBoardDialogFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/board/EditBoardDialogFragment.java @@ -2,6 +2,7 @@ package it.niedermann.nextcloud.deck.ui.board; import android.app.Activity; import android.app.Dialog; +import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -23,7 +24,6 @@ import it.niedermann.nextcloud.deck.Application; import it.niedermann.nextcloud.deck.R; import it.niedermann.nextcloud.deck.model.full.FullBoard; import it.niedermann.nextcloud.deck.persistence.sync.SyncManager; -import it.niedermann.nextcloud.deck.ui.MainActivity; import it.niedermann.nextcloud.deck.ui.helper.colorchooser.ColorChooser; public class EditBoardDialogFragment extends DialogFragment { @@ -32,6 +32,8 @@ public class EditBoardDialogFragment extends DialogFragment { private static final String KEY_BOARD_ID = "board_id"; private static final Long NO_BOARD_ID = -1L; + private EditBoardListener editBoardListener; + private FullBoard fullBoard = null; @BindView(R.id.input) @@ -45,7 +47,18 @@ public class EditBoardDialogFragment extends DialogFragment { /** * Use newInstance()-Method */ - private EditBoardDialogFragment() {} + private EditBoardDialogFragment() { + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + if (context instanceof EditBoardListener) { + this.editBoardListener = (EditBoardListener) context; + } else { + throw new ClassCastException("Caller must implement " + EditBoardListener.class.getCanonicalName()); + } + } @NonNull @Override @@ -60,14 +73,14 @@ public class EditBoardDialogFragment extends DialogFragment { if (NO_BOARD_ID.equals(boardId)) { dialogBuilder.setTitle(R.string.add_board); - dialogBuilder.setPositiveButton(R.string.simple_add, (dialog, which) -> ((MainActivity) getActivity()).onCreateBoard(boardTitle.getText().toString(), colorChooser.getSelectedColor())); + dialogBuilder.setPositiveButton(R.string.simple_add, (dialog, which) -> editBoardListener.onCreateBoard(boardTitle.getText().toString(), colorChooser.getSelectedColor())); this.colorChooser.selectColor(String.format("#%06X", 0xFFFFFF & boardDefaultColor)); } else { dialogBuilder.setTitle(R.string.edit_board); dialogBuilder.setPositiveButton(R.string.simple_save, (dialog, which) -> { this.fullBoard.board.setColor(colorChooser.getSelectedColor().substring(1)); this.fullBoard.board.setTitle(this.boardTitle.getText().toString()); - ((MainActivity) getActivity()).onUpdateBoard(fullBoard); + editBoardListener.onUpdateBoard(fullBoard); }); new SyncManager(activity).getFullBoardById(Objects.requireNonNull(getArguments()).getLong(KEY_ACCOUNT_ID), boardId).observe(EditBoardDialogFragment.this, (FullBoard fb) -> { if (fb.board != null) { @@ -116,4 +129,10 @@ public class EditBoardDialogFragment extends DialogFragment { return dialog; } + public interface EditBoardListener { + void onUpdateBoard(FullBoard fullBoard); + + void onCreateBoard(String title, String color); + } + }
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/EditStackDialogFragment.java b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/EditStackDialogFragment.java index b850f1fbc..c07ace799 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/EditStackDialogFragment.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/ui/stack/EditStackDialogFragment.java @@ -1,6 +1,7 @@ package it.niedermann.nextcloud.deck.ui.stack; import android.app.Dialog; +import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -19,12 +20,13 @@ import butterknife.BindView; import butterknife.ButterKnife; import it.niedermann.nextcloud.deck.Application; import it.niedermann.nextcloud.deck.R; -import it.niedermann.nextcloud.deck.ui.MainActivity; public class EditStackDialogFragment extends DialogFragment { public static final Long NO_STACK_ID = -1L; private static final String KEY_STACK_ID = "board_id"; + private static final String KEY_OLD_TITLE = "old_title"; private long stackId = NO_STACK_ID; + private EditStackListener editStackListener; @BindView(R.id.input) EditText input; @@ -35,6 +37,16 @@ public class EditStackDialogFragment extends DialogFragment { private EditStackDialogFragment() { } + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + if (context instanceof EditStackListener) { + this.editStackListener = (EditStackListener) context; + } else { + throw new ClassCastException("Caller must implement " + EditStackListener.class.getCanonicalName()); + } + } + @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { @@ -46,24 +58,17 @@ public class EditStackDialogFragment extends DialogFragment { .setNegativeButton(R.string.simple_cancel, (dialog, which) -> { // Do something else }); - if (getArguments() != null) { - stackId = getArguments().getLong(KEY_STACK_ID); - if (stackId == NO_STACK_ID) { - builder.setTitle(R.string.add_column) - .setPositiveButton(R.string.simple_add, (dialog, which) -> { - ((MainActivity) getActivity()).onCreateStack(input.getText().toString()); - }); - } else { - builder.setTitle(R.string.rename_column) - .setPositiveButton(R.string.simple_rename, (dialog, which) -> { - ((MainActivity) getActivity()).onUpdateStack(stackId, input.getText().toString()); - }); - } - } else { + if (getArguments() == null) { + throw new IllegalArgumentException("Please add at least stack id to the arguments"); + } + stackId = getArguments().getLong(KEY_STACK_ID); + if (stackId == NO_STACK_ID) { builder.setTitle(R.string.add_column) - .setPositiveButton(R.string.simple_add, (dialog, which) -> { - ((MainActivity) getActivity()).onCreateStack(input.getText().toString()); - }); + .setPositiveButton(R.string.simple_add, (dialog, which) -> editStackListener.onCreateStack(input.getText().toString())); + } else { + input.setText(getArguments().getString(KEY_OLD_TITLE)); + builder.setTitle(R.string.rename_column) + .setPositiveButton(R.string.simple_rename, (dialog, which) -> editStackListener.onUpdateStack(stackId, input.getText().toString())); } return builder.create(); } @@ -77,12 +82,23 @@ public class EditStackDialogFragment extends DialogFragment { } public static EditStackDialogFragment newInstance(long stackId) { + return newInstance(stackId, null); + } + + public static EditStackDialogFragment newInstance(long stackId, String oldTitle) { EditStackDialogFragment dialog = new EditStackDialogFragment(); Bundle args = new Bundle(); args.putLong(KEY_STACK_ID, stackId); + args.putString(KEY_OLD_TITLE, oldTitle); dialog.setArguments(args); return dialog; } + + public interface EditStackListener { + void onCreateStack(String title); + + void onUpdateStack(long stackId, String title); + } } |