mirror of https://github.com/kurisufriend/Clover
parent
918d4ac326
commit
abcf5d0cf9
@ -0,0 +1,210 @@ |
||||
/* |
||||
* Clover - 4chan browser https://github.com/Floens/Clover/
|
||||
* Copyright (C) 2014 Floens |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.floens.chan.ui.cell; |
||||
|
||||
import android.annotation.TargetApi; |
||||
import android.content.Context; |
||||
import android.os.Build; |
||||
import android.support.v7.widget.CardView; |
||||
import android.text.TextUtils; |
||||
import android.util.AttributeSet; |
||||
import android.view.View; |
||||
import android.widget.ImageView; |
||||
import android.widget.TextView; |
||||
|
||||
import org.floens.chan.R; |
||||
import org.floens.chan.core.model.Post; |
||||
import org.floens.chan.core.settings.ChanSettings; |
||||
import org.floens.chan.ui.theme.Theme; |
||||
import org.floens.chan.ui.theme.ThemeHelper; |
||||
import org.floens.chan.ui.view.FastTextView; |
||||
import org.floens.chan.ui.view.FloatingMenu; |
||||
import org.floens.chan.ui.view.FloatingMenuItem; |
||||
import org.floens.chan.ui.view.ThumbnailView; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.floens.chan.utils.AndroidUtils.setRoundItemBackground; |
||||
|
||||
public class CardPostCell extends CardView implements PostCellInterface, View.OnClickListener { |
||||
private static final int COMMENT_MAX_LENGTH = 200; |
||||
|
||||
private boolean bound; |
||||
private Theme theme; |
||||
private Post post; |
||||
private PostCellInterface.PostCellCallback callback; |
||||
|
||||
private ThumbnailView thumbnailView; |
||||
private TextView title; |
||||
private FastTextView comment; |
||||
private TextView replies; |
||||
private ImageView options; |
||||
|
||||
public CardPostCell(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
public CardPostCell(Context context, AttributeSet attrs) { |
||||
super(context, attrs); |
||||
} |
||||
|
||||
public CardPostCell(Context context, AttributeSet attrs, int defStyleAttr) { |
||||
super(context, attrs, defStyleAttr); |
||||
} |
||||
|
||||
@Override |
||||
protected void onFinishInflate() { |
||||
super.onFinishInflate(); |
||||
|
||||
thumbnailView = (ThumbnailView) findViewById(R.id.thumbnail); |
||||
thumbnailView.setOnClickListener(this); |
||||
title = (TextView) findViewById(R.id.title); |
||||
comment = (FastTextView) findViewById(R.id.comment); |
||||
replies = (TextView) findViewById(R.id.replies); |
||||
options = (ImageView) findViewById(R.id.options); |
||||
setRoundItemBackground(options); |
||||
|
||||
int textSizeSp = Integer.parseInt(ChanSettings.fontSize.get()); |
||||
title.setTextSize(textSizeSp); |
||||
comment.setTextSize(textSizeSp); |
||||
replies.setTextSize(textSizeSp); |
||||
|
||||
setOnClickListener(this); |
||||
|
||||
options.setOnClickListener(new OnClickListener() { |
||||
@Override |
||||
public void onClick(View v) { |
||||
List<FloatingMenuItem> items = new ArrayList<>(); |
||||
|
||||
callback.onPopulatePostOptions(post, items); |
||||
|
||||
FloatingMenu menu = new FloatingMenu(getContext(), v, items); |
||||
menu.setCallback(new FloatingMenu.FloatingMenuCallback() { |
||||
@Override |
||||
public void onFloatingMenuItemClicked(FloatingMenu menu, FloatingMenuItem item) { |
||||
callback.onPostOptionClicked(post, item.getId()); |
||||
} |
||||
|
||||
@Override |
||||
public void onFloatingMenuDismissed(FloatingMenu menu) { |
||||
} |
||||
}); |
||||
menu.show(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
@Override |
||||
public void onClick(View v) { |
||||
if (v == thumbnailView) { |
||||
callback.onThumbnailClicked(post, thumbnailView); |
||||
} else if (v == this) { |
||||
callback.onPostClicked(post); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void onDetachedFromWindow() { |
||||
super.onDetachedFromWindow(); |
||||
|
||||
if (post != null && bound) { |
||||
unbindPost(post); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void onAttachedToWindow() { |
||||
super.onAttachedToWindow(); |
||||
|
||||
if (post != null && !bound) { |
||||
bindPost(theme, post); |
||||
} |
||||
} |
||||
|
||||
public void setPost(Theme theme, final Post post, PostCellInterface.PostCellCallback callback, boolean highlighted, int markedNo) { |
||||
if (this.post == post) { |
||||
return; |
||||
} |
||||
|
||||
if (theme == null) { |
||||
theme = ThemeHelper.theme(); |
||||
} |
||||
|
||||
if (this.post != null && bound) { |
||||
unbindPost(this.post); |
||||
this.post = null; |
||||
} |
||||
|
||||
this.theme = theme; |
||||
this.post = post; |
||||
this.callback = callback; |
||||
|
||||
bindPost(theme, post); |
||||
} |
||||
|
||||
public Post getPost() { |
||||
return post; |
||||
} |
||||
|
||||
public ThumbnailView getThumbnailView() { |
||||
return thumbnailView; |
||||
} |
||||
|
||||
@Override |
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
||||
public boolean hasOverlappingRendering() { |
||||
return false; |
||||
} |
||||
|
||||
private void bindPost(Theme theme, Post post) { |
||||
bound = true; |
||||
|
||||
if (post.hasImage) { |
||||
thumbnailView.setVisibility(View.VISIBLE); |
||||
thumbnailView.setUrl(post.thumbnailUrl, thumbnailView.getWidth(), thumbnailView.getHeight()); |
||||
} else { |
||||
thumbnailView.setVisibility(View.GONE); |
||||
thumbnailView.setUrl(null, 0, 0); |
||||
} |
||||
|
||||
if (!TextUtils.isEmpty(post.subjectSpan)) { |
||||
title.setVisibility(View.VISIBLE); |
||||
title.setText(post.subjectSpan); |
||||
} else { |
||||
title.setVisibility(View.GONE); |
||||
title.setText(null); |
||||
} |
||||
|
||||
CharSequence commentText; |
||||
if (post.comment.length() > COMMENT_MAX_LENGTH) { |
||||
commentText = post.comment.subSequence(0, COMMENT_MAX_LENGTH); |
||||
} else { |
||||
commentText = post.comment; |
||||
} |
||||
|
||||
comment.setText(commentText); |
||||
comment.setTextColor(theme.textPrimary); |
||||
|
||||
replies.setText(getResources().getString(R.string.card_stats, post.replies, post.images)); |
||||
} |
||||
|
||||
private void unbindPost(Post post) { |
||||
bound = false; |
||||
} |
||||
} |
@ -0,0 +1,56 @@ |
||||
/* |
||||
* Clover - 4chan browser https://github.com/Floens/Clover/
|
||||
* Copyright (C) 2014 Floens |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.floens.chan.ui.cell; |
||||
|
||||
import org.floens.chan.core.model.Loadable; |
||||
import org.floens.chan.core.model.Post; |
||||
import org.floens.chan.core.model.PostLinkable; |
||||
import org.floens.chan.ui.theme.Theme; |
||||
import org.floens.chan.ui.view.FloatingMenuItem; |
||||
import org.floens.chan.ui.view.ThumbnailView; |
||||
|
||||
import java.util.List; |
||||
|
||||
public interface PostCellInterface { |
||||
void setPost(Theme theme, Post post, PostCellCallback callback, boolean highlighted, int markedN); |
||||
|
||||
Post getPost(); |
||||
|
||||
ThumbnailView getThumbnailView(); |
||||
|
||||
enum PostViewMode { |
||||
LIST, |
||||
CARD |
||||
} |
||||
|
||||
interface PostCellCallback { |
||||
Loadable getLoadable(); |
||||
|
||||
void onPostClicked(Post post); |
||||
|
||||
void onThumbnailClicked(Post post, ThumbnailView thumbnail); |
||||
|
||||
void onShowPostReplies(Post post); |
||||
|
||||
void onPopulatePostOptions(Post post, List<FloatingMenuItem> menu); |
||||
|
||||
void onPostOptionClicked(Post post, Object id); |
||||
|
||||
void onPostLinkableClicked(PostLinkable linkable); |
||||
} |
||||
} |
@ -0,0 +1,106 @@ |
||||
/* |
||||
* Clover - 4chan browser https://github.com/Floens/Clover/
|
||||
* Copyright (C) 2014 Floens |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.floens.chan.ui.view; |
||||
|
||||
import android.content.Context; |
||||
import android.graphics.Canvas; |
||||
import android.graphics.Paint; |
||||
import android.text.Layout; |
||||
import android.text.StaticLayout; |
||||
import android.text.TextPaint; |
||||
import android.text.TextUtils; |
||||
import android.util.AttributeSet; |
||||
import android.view.View; |
||||
|
||||
import static org.floens.chan.utils.AndroidUtils.sp; |
||||
|
||||
public class FastTextView extends View { |
||||
private TextPaint paint; |
||||
|
||||
private CharSequence text; |
||||
|
||||
private boolean update = false; |
||||
private StaticLayout layout; |
||||
|
||||
public FastTextView(Context context) { |
||||
super(context); |
||||
init(); |
||||
} |
||||
|
||||
public FastTextView(Context context, AttributeSet attrs) { |
||||
super(context, attrs); |
||||
init(); |
||||
} |
||||
|
||||
public FastTextView(Context context, AttributeSet attrs, int defStyleAttr) { |
||||
super(context, attrs, defStyleAttr); |
||||
init(); |
||||
} |
||||
|
||||
private void init() { |
||||
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG); |
||||
} |
||||
|
||||
public void setText(CharSequence text) { |
||||
if (!TextUtils.equals(this.text, text)) { |
||||
this.text = text; |
||||
|
||||
if (text == null) { |
||||
layout = null; |
||||
} else { |
||||
update = true; |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void setTextSize(float size) { |
||||
int sizeSp = sp(size); |
||||
if (paint.getTextSize() != sizeSp) { |
||||
paint.setTextSize(sizeSp); |
||||
update = true; |
||||
} |
||||
} |
||||
|
||||
public void setTextColor(int color) { |
||||
if (paint.getColor() != color) { |
||||
paint.setColor(color); |
||||
update = true; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
||||
update = true; |
||||
} |
||||
|
||||
@Override |
||||
protected void onDraw(Canvas canvas) { |
||||
if (update) { |
||||
int width = getWidth() - getPaddingLeft() - getPaddingRight(); |
||||
layout = new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_NORMAL, 1, 0, false); |
||||
update = false; |
||||
} |
||||
|
||||
if (layout != null) { |
||||
canvas.save(); |
||||
canvas.translate(getPaddingLeft(), getPaddingTop()); |
||||
layout.draw(canvas); |
||||
canvas.restore(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,47 @@ |
||||
/* |
||||
* Clover - 4chan browser https://github.com/Floens/Clover/
|
||||
* Copyright (C) 2014 Floens |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package org.floens.chan.ui.view; |
||||
|
||||
import android.content.Context; |
||||
import android.util.AttributeSet; |
||||
|
||||
public class FixedRatioThumbnailView extends ThumbnailView { |
||||
public FixedRatioThumbnailView(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
public FixedRatioThumbnailView(Context context, AttributeSet attrs) { |
||||
super(context, attrs); |
||||
} |
||||
|
||||
public FixedRatioThumbnailView(Context context, AttributeSet attrs, int defStyleAttr) { |
||||
super(context, attrs, defStyleAttr); |
||||
} |
||||
|
||||
@Override |
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
||||
int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
||||
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY && (heightMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.AT_MOST)) { |
||||
int width = MeasureSpec.getSize(widthMeasureSpec); |
||||
|
||||
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec((int) (width / 16f * 9f), MeasureSpec.EXACTLY)); |
||||
} else { |
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,90 @@ |
||||
<?xml version="1.0" encoding="utf-8"?><!-- |
||||
Clover - 4chan browser https://github.com/Floens/Clover/ |
||||
Copyright (C) 2014 Floens |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
--> |
||||
<org.floens.chan.ui.cell.CardPostCell xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:card_view="http://schemas.android.com/apk/res-auto" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_margin="4dp" |
||||
android:padding="50dp" |
||||
card_view:cardBackgroundColor="?backcolor" |
||||
card_view:cardCornerRadius="2dp" |
||||
card_view:cardElevation="4dp"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="280dp" |
||||
android:background="@drawable/item_background" |
||||
android:orientation="vertical"> |
||||
|
||||
<FrameLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content"> |
||||
|
||||
<org.floens.chan.ui.view.FixedRatioThumbnailView |
||||
android:id="@+id/thumbnail" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" /> |
||||
|
||||
<ImageView |
||||
android:id="@+id/options" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_gravity="top|right" |
||||
android:paddingBottom="15dp" |
||||
android:paddingLeft="15dp" |
||||
android:paddingRight="5dp" |
||||
android:paddingTop="5dp" |
||||
android:src="?post_options_drawable" |
||||
tools:ignore="ContentDescription,RtlHardcoded" /> |
||||
|
||||
</FrameLayout> |
||||
|
||||
<TextView |
||||
android:id="@+id/title" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:maxLines="3" |
||||
android:paddingLeft="8dp" |
||||
android:paddingRight="8dp" |
||||
android:paddingTop="8dp" /> |
||||
|
||||
<org.floens.chan.ui.view.FastTextView |
||||
android:id="@+id/comment" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="0dp" |
||||
android:layout_weight="1" |
||||
android:paddingLeft="8dp" |
||||
android:paddingRight="8dp" |
||||
android:paddingTop="8dp" |
||||
android:textColor="?attr/text_color_primary" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/replies" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:paddingBottom="8dp" |
||||
android:paddingLeft="8dp" |
||||
android:paddingRight="8dp" |
||||
android:paddingTop="4dp" |
||||
android:singleLine="true" |
||||
android:textColor="?attr/text_color_secondary" /> |
||||
|
||||
</LinearLayout> |
||||
|
||||
</org.floens.chan.ui.cell.CardPostCell> |
@ -0,0 +1,23 @@ |
||||
<?xml version="1.0" encoding="utf-8"?><!-- |
||||
Clover - 4chan browser https://github.com/Floens/Clover/ |
||||
Copyright (C) 2014 Floens |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
--> |
||||
<resources> |
||||
<bool name="is_tablet">true</bool> |
||||
|
||||
<dimen name="grid_card_width">180dp</dimen> |
||||
<integer name="grid_card_max_spans">5</integer> |
||||
</resources> |
@ -1,4 +1,25 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<?xml version="1.0" encoding="utf-8"?><!-- |
||||
Clover - 4chan browser https://github.com/Floens/Clover/ |
||||
Copyright (C) 2014 Floens |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
--> |
||||
<resources> |
||||
<bool name="is_tablet">false</bool> |
||||
|
||||
<dimen name="toolbar_height">56dp</dimen> |
||||
|
||||
<dimen name="grid_card_width">140dp</dimen> |
||||
<integer name="grid_card_max_spans">-1</integer> |
||||
</resources> |
||||
|
Loading…
Reference in new issue