-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandedPostActivity.java
More file actions
137 lines (113 loc) · 4.67 KB
/
ExpandedPostActivity.java
File metadata and controls
137 lines (113 loc) · 4.67 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.example.ikoala.ui.social;
import androidx.appcompat.app.ActionBar;
import androidx.core.text.HtmlCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.ikoala.R;
import com.example.ikoala.adapters.ForumCommentAdapter;
import com.example.ikoala.database.BaseActivity;
import com.example.ikoala.database.DatabaseManager;
import com.example.ikoala.database.ForumItem;
import com.example.ikoala.ui.ParentActivity;
import com.example.ikoala.utils.ColorUtils;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class ExpandedPostActivity extends ParentActivity {
private BaseActivity mActivity;
private ForumItem postContent;
private String forumName;
private String postId;
private TextView title;
private TextView description;
private TextView datePosted;
private RecyclerView comments;
private EditText writeComment;
private RecyclerView.Adapter adapter;
private List<Map<String, Object>> commentsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expanded_post);
mActivity = getIntent().getParcelableExtra("activity");
postContent = getIntent().getParcelableExtra("post");
forumName = getIntent().getStringExtra("forumPage");
commentsList = postContent.getComments();
postId = getIntent().getStringExtra("postId");
ActionBar bar = getSupportActionBar();
if (bar != null)
{
bar.show();
bar.setDisplayHomeAsUpEnabled(true);
bar.setDisplayShowHomeEnabled(true);
int color = ColorUtils.getColor(this, R.attr.themeColorHighlight);
String htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & color));
bar.setTitle(HtmlCompat.fromHtml("<font color=\"" + htmlColor + "\">" + mActivity.getName() + " Forum</font>", HtmlCompat.FROM_HTML_MODE_LEGACY));
bar.setBackgroundDrawable(new ColorDrawable(ColorUtils.getColor(this, R.attr.themeColorSecondary)));
}
title = findViewById(R.id.expanded_post_title);
description = findViewById(R.id.expanded_post_des);
comments = findViewById(R.id.expanded_post_recyclerpost);
comments.setNestedScrollingEnabled(false);
comments.setHasFixedSize(true);
title.setText(postContent.getTitle());
description.setText(postContent.getDescription());
writeComment = findViewById(R.id.add_post_comment);
Button addComment = findViewById(R.id.expanded_post_addComment);
addComment.setOnClickListener(v -> {
createAComment();
});
showPostComments();
}
private void showPostComments() {
RecyclerView recyclerView = findViewById(R.id.expanded_post_recyclerpost);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ForumCommentAdapter(commentsList, this);
comments.setAdapter(adapter);
}
private void createAComment() {
String comment = writeComment.getText().toString();
Date date = new Date(System.currentTimeMillis());
String userName = getFirebaseAuth().getUid();
if(comment.trim().isEmpty()){
Toast.makeText(this, "Cannot submit blank comment", Toast.LENGTH_SHORT).show();
return;
}
//creating a empty structure to forfil comments section of a new post
List<Map<String, Object>> comments = new ArrayList<Map<String, Object>>();
comments = commentsList;
Map<String, Object> structure = new HashMap<>();
structure.put("comment", comment);
structure.put("datePosted", date);
structure.put("name", userName);
comments.add(structure);
DatabaseManager.setForumPostComments(mActivity.getName().toLowerCase(), forumName, postId, comments);
Toast.makeText(this, "Comment added", Toast.LENGTH_SHORT).show();
finish();
}
@Override
public void onBackPressed()
{
super.onDefaultBackPressed();
}
@Override
protected void setupViewUI() {
}
@Override
public boolean onSupportNavigateUp()
{
onBackPressed();
return true;
}
}