From 4b6bb27d0001f99e181dd4d64b85c11204cd466d Mon Sep 17 00:00:00 2001 From: Martijn Brekhof Date: Tue, 25 Jul 2017 20:34:53 +0200 Subject: [PATCH] Widgets use ButterKnife now and implement all View constructors. (#424) The new widgets now implement all View constructors to make the code more robust to any future changes in how the widgets are used. --- .../xbmc/kore/ui/widgets/HighlightButton.java | 26 +++++++--- .../ui/widgets/MediaProgressIndicator.java | 29 +++++------ .../xbmc/kore/ui/widgets/NowPlayingPanel.java | 49 +++++++------------ .../kore/ui/widgets/RepeatModeButton.java | 24 +++++++-- .../kore/ui/widgets/VolumeLevelIndicator.java | 24 ++++----- 5 files changed, 83 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/org/xbmc/kore/ui/widgets/HighlightButton.java b/app/src/main/java/org/xbmc/kore/ui/widgets/HighlightButton.java index f550e3d..f253daf 100644 --- a/app/src/main/java/org/xbmc/kore/ui/widgets/HighlightButton.java +++ b/app/src/main/java/org/xbmc/kore/ui/widgets/HighlightButton.java @@ -16,7 +16,6 @@ package org.xbmc.kore.ui.widgets; import android.content.Context; -import android.content.res.Resources; import android.content.res.TypedArray; import android.support.v7.widget.AppCompatImageButton; import android.util.AttributeSet; @@ -28,14 +27,19 @@ public class HighlightButton extends AppCompatImageButton { private boolean highlight; + public HighlightButton(Context context) { + super(context); + setStyle(context); + } + public HighlightButton(Context context, AttributeSet attrs) { super(context, attrs); - Resources.Theme theme = context.getTheme(); - TypedArray styledAttributes = theme.obtainStyledAttributes(new int[]{ - R.attr.colorAccent}); - colorFilter = styledAttributes.getColor(styledAttributes.getIndex(0), - context.getResources().getColor(R.color.accent_default)); - styledAttributes.recycle(); + setStyle(context); + } + + public HighlightButton(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + setStyle(context); } public void setHighlight(boolean highlight) { @@ -50,4 +54,12 @@ public class HighlightButton extends AppCompatImageButton { public boolean isHighlighted() { return highlight; } + + private void setStyle(Context context) { + TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(new int[]{ + R.attr.colorAccent}); + colorFilter = styledAttributes.getColor(styledAttributes.getIndex(0), + context.getResources().getColor(R.color.accent_default)); + styledAttributes.recycle(); + } } diff --git a/app/src/main/java/org/xbmc/kore/ui/widgets/MediaProgressIndicator.java b/app/src/main/java/org/xbmc/kore/ui/widgets/MediaProgressIndicator.java index df66a41..040f38f 100644 --- a/app/src/main/java/org/xbmc/kore/ui/widgets/MediaProgressIndicator.java +++ b/app/src/main/java/org/xbmc/kore/ui/widgets/MediaProgressIndicator.java @@ -22,19 +22,23 @@ import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; import android.view.LayoutInflater; +import android.view.View; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import org.xbmc.kore.R; -import org.xbmc.kore.utils.LogUtils; import org.xbmc.kore.utils.UIUtils; +import butterknife.ButterKnife; +import butterknife.InjectView; + public class MediaProgressIndicator extends LinearLayout { - private SeekBar seekBar; - private TextView durationTextView; - private TextView progressTextView; + @InjectView(R.id.mpi_seek_bar) SeekBar seekBar; + @InjectView(R.id.mpi_duration) TextView durationTextView; + @InjectView(R.id.mpi_progress) TextView progressTextView; + private int speed = 0; private int maxProgress; private int progress; @@ -57,18 +61,15 @@ public class MediaProgressIndicator extends LinearLayout { initializeView(context); } - private void initializeView(Context context) { - LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - inflater.inflate(R.layout.media_progress_indicator, this); + public MediaProgressIndicator(Context context, AttributeSet attributeSet, int defStyle) { + super(context, attributeSet, defStyle); + initializeView(context); } - @Override - protected void onFinishInflate() { - super.onFinishInflate(); - - seekBar = (SeekBar) findViewById(R.id.mpi_seek_bar); - progressTextView = (TextView) findViewById(R.id.mpi_progress); - durationTextView = (TextView) findViewById(R.id.mpi_duration); + private void initializeView(Context context) { + LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.media_progress_indicator, this); + ButterKnife.inject(view); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override diff --git a/app/src/main/java/org/xbmc/kore/ui/widgets/NowPlayingPanel.java b/app/src/main/java/org/xbmc/kore/ui/widgets/NowPlayingPanel.java index cb11f9e..187b87b 100644 --- a/app/src/main/java/org/xbmc/kore/ui/widgets/NowPlayingPanel.java +++ b/app/src/main/java/org/xbmc/kore/ui/widgets/NowPlayingPanel.java @@ -32,6 +32,9 @@ import org.xbmc.kore.R; import org.xbmc.kore.jsonrpc.type.GlobalType; import org.xbmc.kore.utils.UIUtils; +import butterknife.ButterKnife; +import butterknife.InjectView; + public class NowPlayingPanel extends SlidingUpPanelLayout { public interface OnPanelButtonsClickListener { @@ -46,18 +49,18 @@ public class NowPlayingPanel extends SlidingUpPanelLayout { private OnPanelButtonsClickListener onPanelButtonsClickListener; - private TextView title; - private TextView details; - private ImageView poster; - private ImageButton previousButton; - private ImageButton nextButton; - private ImageButton playButton; - private MediaProgressIndicator mediaProgressIndicator; - private VolumeLevelIndicator volumeLevelIndicator; - private HighlightButton volumeMuteButton; - private HighlightButton volumeMutedIndicatorButton; - private RepeatModeButton repeatModeButton; - private HighlightButton shuffleButton; + @InjectView(R.id.npp_title) TextView title; + @InjectView(R.id.npp_details) TextView details; + @InjectView(R.id.npp_poster) ImageView poster; + @InjectView(R.id.npp_previous) ImageButton previousButton; + @InjectView(R.id.npp_next) ImageButton nextButton; + @InjectView(R.id.npp_play) ImageButton playButton; + @InjectView(R.id.npp_progress_indicator) MediaProgressIndicator mediaProgressIndicator; + @InjectView(R.id.npp_volume_level_indicator) VolumeLevelIndicator volumeLevelIndicator; + @InjectView(R.id.npp_volume_mute) HighlightButton volumeMuteButton; + @InjectView(R.id.npp_volume_muted_indicator) HighlightButton volumeMutedIndicatorButton; + @InjectView(R.id.npp_repeat) RepeatModeButton repeatModeButton; + @InjectView(R.id.npp_shuffle) HighlightButton shuffleButton; public NowPlayingPanel(Context context) { super(context); @@ -75,26 +78,8 @@ public class NowPlayingPanel extends SlidingUpPanelLayout { private void initializeView(Context context) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - inflater.inflate(R.layout.now_playing_panel, this); - } - - @Override - protected void onFinishInflate() { - super.onFinishInflate(); - - title = (TextView) findViewById(R.id.npp_title); - details = (TextView) findViewById(R.id.npp_details); - poster = (ImageView) findViewById(R.id.npp_poster); - previousButton = (ImageButton) findViewById(R.id.npp_previous); - nextButton = (ImageButton) findViewById(R.id.npp_next); - playButton = (ImageButton) findViewById(R.id.npp_play); - mediaProgressIndicator = (MediaProgressIndicator) findViewById(R.id.npp_progress_indicator); - volumeLevelIndicator = (VolumeLevelIndicator) findViewById(R.id.npp_volume_level_indicator); - volumeMuteButton = (HighlightButton) findViewById(R.id.npp_volume_mute); - repeatModeButton = (RepeatModeButton) findViewById(R.id.npp_repeat); - shuffleButton = (HighlightButton) findViewById(R.id.npp_shuffle); - volumeMutedIndicatorButton = (HighlightButton) findViewById(R.id.npp_volume_muted_indicator); - + View view = inflater.inflate(R.layout.now_playing_panel, this); + ButterKnife.inject(view); setupButtonClickListeners(); } diff --git a/app/src/main/java/org/xbmc/kore/ui/widgets/RepeatModeButton.java b/app/src/main/java/org/xbmc/kore/ui/widgets/RepeatModeButton.java index cf4428b..c7ded7c 100644 --- a/app/src/main/java/org/xbmc/kore/ui/widgets/RepeatModeButton.java +++ b/app/src/main/java/org/xbmc/kore/ui/widgets/RepeatModeButton.java @@ -34,13 +34,19 @@ public class RepeatModeButton extends AppCompatImageButton { private static TypedArray styledAttributes; private static int accentDefaultColor; + public RepeatModeButton(Context context) { + super(context); + setStyle(context); + } + public RepeatModeButton(Context context, AttributeSet attrs) { super(context, attrs); - styledAttributes = context.getTheme().obtainStyledAttributes(new int[]{ - R.attr.colorAccent, - R.attr.iconRepeat, - R.attr.iconRepeatOne}); - accentDefaultColor = getContext().getResources().getColor(R.color.accent_default); + setStyle(context); + } + + public RepeatModeButton(Context context, AttributeSet attributeSet, int defStyle) { + super(context, attributeSet, defStyle); + setStyle(context); } public void setMode(MODE mode) { @@ -65,4 +71,12 @@ public class RepeatModeButton extends AppCompatImageButton { public MODE getMode() { return mode; } + + private void setStyle(Context context) { + styledAttributes = context.getTheme().obtainStyledAttributes(new int[]{ + R.attr.colorAccent, + R.attr.iconRepeat, + R.attr.iconRepeatOne}); + accentDefaultColor = getContext().getResources().getColor(R.color.accent_default); + } } diff --git a/app/src/main/java/org/xbmc/kore/ui/widgets/VolumeLevelIndicator.java b/app/src/main/java/org/xbmc/kore/ui/widgets/VolumeLevelIndicator.java index 0a12553..61b2630 100644 --- a/app/src/main/java/org/xbmc/kore/ui/widgets/VolumeLevelIndicator.java +++ b/app/src/main/java/org/xbmc/kore/ui/widgets/VolumeLevelIndicator.java @@ -18,15 +18,19 @@ package org.xbmc.kore.ui.widgets; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; +import android.view.View; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import org.xbmc.kore.R; +import butterknife.ButterKnife; +import butterknife.InjectView; + public class VolumeLevelIndicator extends LinearLayout { - private SeekBar volumeSeekBar; - private TextView volumeTextView; + @InjectView(R.id.vli_seek_bar) SeekBar volumeSeekBar; + @InjectView(R.id.vli_volume_text) TextView volumeTextView; private OnVolumeChangeListener onVolumeChangeListener; @@ -44,17 +48,15 @@ public class VolumeLevelIndicator extends LinearLayout { initializeView(context); } - private void initializeView(Context context) { - LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - inflater.inflate(R.layout.volume_level_indicator, this); + public VolumeLevelIndicator(Context context, AttributeSet attributeSet, int defStyle) { + super(context, attributeSet, defStyle); + initializeView(context); } - @Override - protected void onFinishInflate() { - super.onFinishInflate(); - - volumeSeekBar = (SeekBar) findViewById(R.id.vli_seek_bar); - volumeTextView = (TextView) findViewById(R.id.vli_volume_text); + private void initializeView(Context context) { + LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View view = inflater.inflate(R.layout.volume_level_indicator, this); + ButterKnife.inject(view); volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override