-
Notifications
You must be signed in to change notification settings - Fork 120
/
CommentViewModel.java
75 lines (61 loc) · 2.53 KB
/
CommentViewModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.hitherejoe.mvvm_hackernews.viewModel;
import android.content.Context;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.hitherejoe.mvvm_hackernews.R;
import com.hitherejoe.mvvm_hackernews.model.Comment;
import com.hitherejoe.mvvm_hackernews.util.ViewUtils;
import org.ocpsoft.prettytime.PrettyTime;
import java.util.Date;
public class CommentViewModel extends BaseObservable {
private Context context;
private Comment comment;
public CommentViewModel(Context context, Comment comment) {
this.context = context;
this.comment = comment;
}
@BindingAdapter("containerMargin")
public static void setContainerMargin(View view, boolean isTopLevelComment) {
if (view.getTag() == null) {
view.setTag(true);
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)
view.getLayoutParams();
float horizontalMargin = view.getContext().getResources().getDimension(R.dimen.activity_horizontal_margin);
float topMargin = isTopLevelComment
? view.getContext().getResources().getDimension(R.dimen.activity_vertical_margin) : 0;
layoutParams.setMargins((int) horizontalMargin, (int) topMargin, (int) horizontalMargin, 0);
view.setLayoutParams(layoutParams);
}
}
@BindingAdapter("commentDepth")
public static void setCommentIndent(View view, int depth) {
if (view.getTag() == null) {
view.setTag(true);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
float margin = ViewUtils.convertPixelsToDp(depth * 20, view.getContext());
layoutParams.setMargins((int) margin, 0, 0, 0);
view.setLayoutParams(layoutParams);
}
}
public String getCommentText() {
return Html.fromHtml(comment.text.trim()).toString();
}
public String getCommentAuthor() {
return context.getResources().getString(R.string.text_comment_author, comment.by);
}
public String getCommentDate() {
return new PrettyTime().format(new Date(comment.time * 1000));
}
public int getCommentDepth() {
return comment.depth;
}
public boolean getCommentIsTopLevel() {
return comment.isTopLevelComment;
}
}