Skip to content

Commit

Permalink
Move ColorChooser to own ViewComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-niedermann committed May 9, 2019
1 parent 1195cd6 commit 7df89b4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 45 deletions.
Expand Up @@ -12,8 +12,6 @@
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.google.android.flexbox.FlexboxLayout;

import java.util.Objects;

import butterknife.BindView;
Expand All @@ -24,15 +22,16 @@
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.util.ViewUtil;
import it.niedermann.nextcloud.deck.ui.helper.colorchooser.ColorChooser;

public class EditBoardDialogFragment extends DialogFragment {

private static final String KEY_ACCOUNT_ID = "account_id";
private static final String KEY_BOARD_ID = "board_id";
private static final Long NO_BOARD_ID = -1L;

@NonNull private Activity activity;
@NonNull
private Activity activity;
private Context context;
private SyncManager syncManager;

Expand All @@ -44,8 +43,9 @@ public class EditBoardDialogFragment extends DialogFragment {

@BindView(R.id.input)
EditText boardTitle;
@BindView(R.id.colorPicker)
FlexboxLayout colorPicker;

@BindView(R.id.colorChooser)
ColorChooser colorChooser;

@NonNull
@Override
Expand All @@ -59,9 +59,9 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
accountId = Objects.requireNonNull(getArguments()).getLong(KEY_ACCOUNT_ID);
syncManager = new SyncManager(this.context, this.activity);

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.activity);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.activity);

if(NO_BOARD_ID.equals(boardId)) {
if (NO_BOARD_ID.equals(boardId)) {
initColorChooser();
dialogBuilder.setTitle(R.string.create_board);
dialogBuilder.setPositiveButton(R.string.simple_create, (dialog, which) -> ((MainActivity) getActivity()).onCreateBoard(boardTitle.getText().toString(), selectedColor));
Expand All @@ -75,7 +75,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
DeckLog.log("--- request with | " + boardId);
syncManager.getFullBoardById(accountId, boardId).observe(EditBoardDialogFragment.this, (FullBoard fb) -> {
DeckLog.log("--- localid | id | " + fb.getLocalId() + " | " + fb.getId());
if(fb.board != null) {
if (fb.board != null) {
this.fullBoard = fb;
this.boardTitle.setText(this.fullBoard.board.getTitle());
initColorChooser();
Expand All @@ -85,7 +85,8 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {

return dialogBuilder
.setView(view)
.setNegativeButton(R.string.simple_cancel, (dialog, which) -> {})
.setNegativeButton(R.string.simple_cancel, (dialog, which) -> {
})
.create();
}

Expand All @@ -112,34 +113,9 @@ public static EditBoardDialogFragment newInstance() {
}

private void initColorChooser() {
String[] colors = getResources().getStringArray(R.array.board_default_colors);

// TODO refactor color chooser as own View Component
for (final String color : colors) {
ImageView image = new ImageView(getContext());
image.setOnClickListener((imageView) -> {
if (previouslySelectedImageView != null) { // null when first selection
previouslySelectedImageView.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_grey600_36dp, previouslySelectedColor));
}
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_alpha_check_36dp, color));
selectedColor = color;
this.previouslySelectedColor = color;
this.previouslySelectedImageView = image;
});
if(this.fullBoard != null && color.toLowerCase().equals("#" + this.fullBoard.board.getColor().toLowerCase())) {
this.selectedColor = color;
this.previouslySelectedColor = color;
this.previouslySelectedImageView = image;
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_alpha_check_36dp, color));
} else {
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_grey600_36dp, color));
}
colorPicker.addView(image);
}

// Preselect when create a new board
if(fullBoard == null && colorPicker.getChildCount() > 0) {
colorPicker.getChildAt(0).performClick();
if (fullBoard == null && colorChooser.getChildCount() > 0) {
colorChooser.selectColor(0);
}
}
}
@@ -0,0 +1,84 @@
package it.niedermann.nextcloud.deck.ui.helper.colorchooser;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.google.android.flexbox.FlexboxLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.util.ViewUtil;

public class ColorChooser extends LinearLayout {

private Context context;

@BindView(R.id.colorPicker)
FlexboxLayout colorPicker;

private String selectedColor;
private String previouslySelectedColor;
private ImageView previouslySelectedImageView;


public ColorChooser(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;

TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ColorChooser, 0, 0);
String[] colors = getResources().getStringArray(a.getResourceId(R.styleable.ColorChooser_colors, 0));
String defaultColor = a.getString(R.styleable.ColorChooser_defaultColor);
a.recycle();

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.color_chooser, this, true);
ButterKnife.bind(this);
initDefaultColors(colors, defaultColor);
}

private void initDefaultColors(String[] colors, String defaultColor) {
for (final String color : colors) {
ImageView image = new ImageView(getContext());
image.setOnClickListener((imageView) -> {
if (previouslySelectedImageView != null) { // null when first selection
previouslySelectedImageView.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_grey600_36dp, previouslySelectedColor));
}
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_alpha_check_36dp, color));
selectedColor = color;
this.previouslySelectedColor = color;
this.previouslySelectedImageView = image;
});
if(color.toLowerCase().equals("#" + defaultColor.toLowerCase())) {
this.selectedColor = color;
this.previouslySelectedColor = color;
this.previouslySelectedImageView = image;
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_alpha_check_36dp, color));
} else {
image.setImageDrawable(ViewUtil.getTintedImageView(this.context, R.drawable.circle_grey600_36dp, color));
}
colorPicker.addView(image);
}

// Preselect when create a new board
// if(colorPicker.getChildCount() > 0) {
// colorPicker.getChildAt(0).performClick();
// }
}

public void selectColor(int index) {
if(colorPicker.getChildCount() > 0) {
colorPicker.getChildAt(0);
} else {
throw new IndexOutOfBoundsException("index not available");
}
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/layout/color_chooser.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.flexbox.FlexboxLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/colorPicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin"
app:flexWrap="wrap"
app:alignItems="stretch"
app:justifyContent="space_between" />
</LinearLayout>
13 changes: 5 additions & 8 deletions app/src/main/res/layout/dialog_board_create.xml
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="@dimen/standard_margin"
android:orientation="vertical">

Expand All @@ -16,13 +17,9 @@
<requestFocus />
</EditText>

<com.google.android.flexbox.FlexboxLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/colorPicker"
<it.niedermann.nextcloud.deck.ui.helper.colorchooser.ColorChooser
android:id="@+id/colorChooser"
app:colors="@array/board_default_colors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/standard_margin"
app:flexWrap="wrap"
app:alignItems="stretch"
app:justifyContent="space_between" />
android:layout_height="wrap_content" />
</LinearLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/values/attrs.xml
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ColorChooser">
<attr name="defaultColor" format="string" />
<attr name="colors" format="reference" />
</declare-styleable>
</resources>

0 comments on commit 7df89b4

Please sign in to comment.