Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Implement View Counter For Articles #31

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AHSMobile_Android/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 73 additions & 35 deletions AHSMobile_Android/app/src/main/java/com/hsappdev/ahs/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,73 @@

import androidx.annotation.NonNull;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Article implements Parcelable {

private final String ID;
private final long time_updated;
private final long timestamp;
private final String title;
private final String author;
private final String story;
private final String [] imagePaths;
private final String body;
private final String [] imageURLs;
private final String [] videoIDS;

private int views;
private final Type type;


public Article(
@NonNull
String ID,
long time_updated,
@NonNull
String title,
@NonNull
String author,
@NonNull
String story,
String[] imagePaths,
String[] videoIDS,
Type type
@NonNull
String ID,
long timestamp,
@NonNull
String title,
@NonNull
String author,
@NonNull
String body,
String[] imageURLs,
String[] videoIDS,
Type type,
int views
)
{
this.ID = ID;
this.timestamp = timestamp;
this.title = title;
this.author = author;
this.body = body;
this.imageURLs = imageURLs;
this.videoIDS = videoIDS;
this.type = type;
this.views = views;
}


public Article(
@NonNull
String ID,
long timestamp,
@NonNull
String title,
@NonNull
String author,
@NonNull
String body,
String[] imageURLs,
String[] videoIDS,
Type type
)
{
this.ID = ID;
this.time_updated = time_updated;
this.timestamp = timestamp;
this.title = title;
this.author = author;
this.story = story;
this.imagePaths = imagePaths;
this.body = body;
this.imageURLs = imageURLs;
this.videoIDS = videoIDS;
this.type = type;
this.views = 0;
}

public static final Creator<Article> CREATOR = new Creator<Article>() {
Expand All @@ -55,22 +86,26 @@ public Article[] newArray(int size) {
};

public String getID() {return ID;}
public long getTimeUpdated()
public long getTimestamp()
{
return timestamp;
}
public int getViews()
{
return time_updated;
return views;
}
public String getTitle()
{
return title;
}
public String getAuthor() {return author;}
public String getStory()
public String getBody()
{
return story;
return body;
}
public String[] getImagePaths()
public String[] getImageURLs()
{
return imagePaths;
return imageURLs;
}
public String[] getVideoIDS() {return videoIDS;}

Expand All @@ -81,24 +116,26 @@ public String[] getImagePaths()
public String toString()
{
return "ID::\t" + ID + "\n" +
"time::\t" + time_updated + "\n" +
"time::\t" + timestamp + "\n" +
"title::\t" + title + "\n" +
"author::\t" + author + "\n" +
"story::\t" + ((story.length() > 40) ? story.substring(0,40) : story) + "\n" + // so output might not be overly long
"type::\t" + type.toString();
"body::\t" + ((body.length() > 40) ? body.substring(0,40) : body) + "\n" + // so output might not be overly long
"type::\t" + type.toString() +
"views::\t" + views;
}

// The following methods are for the purpose of extending Parcelable
// make sure to update methods should a new field be added
protected Article(Parcel in) {
ID = in.readString();
time_updated = in.readLong();
timestamp = in.readLong();
title = in.readString();
author = in.readString();
story = in.readString();
imagePaths = in.createStringArray();
body = in.readString();
imageURLs = in.createStringArray();
videoIDS = in.createStringArray();
type = (Type) in.readSerializable();
views = in.readInt();
}

@Override
Expand All @@ -109,13 +146,14 @@ public int describeContents() {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(ID);
dest.writeLong(time_updated);
dest.writeLong(timestamp);
dest.writeString(title);
dest.writeString(author);
dest.writeString(story);
dest.writeStringArray(imagePaths);
dest.writeString(body);
dest.writeStringArray(imageURLs);
dest.writeStringArray(videoIDS);
dest.writeSerializable(type);
dest.writeInt(views);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
Expand All @@ -17,6 +18,9 @@

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ServerValue;
import com.hsappdev.ahs.misc.FullScreenActivity;
import com.hsappdev.ahs.misc.Helper;
import com.hsappdev.ahs.misc.MediaYoutubeFragment;
Expand All @@ -32,13 +36,38 @@ public class ArticleActivity extends FullScreenActivity implements ArticleImageF
private ValContainer<Boolean> saved_copy;
private Article article;


public void incrementViews() {
Resources r = getResources();
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(r.getString(R.string.fb_news_key));
Log.d("NewViews", article.getType().getName()+" "+article.getID());
Log.d("NewViews", ref.child(convertTypeNumCode(article.getType().getNumCode())).child(article.getID()).child(r.getString(R.string.fb_art_views)).toString());
ref.child(convertTypeNumCode(article.getType().getNumCode())).child(article.getID()).child(r.getString(R.string.fb_art_views)).setValue(ServerValue.increment(1));
}

public String convertTypeNumCode(int i){
switch (i){
case 1:
return "ASB";
case 2:
return "District";

case 3:
return "General_Info";

}
return "";

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.article_layout);

article = getIntent().getParcelableExtra(data_key);
incrementViews();
final String[] imagePaths= article.getImagePaths();

final String[] videoIDs = article.getVideoIDS();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Article_Slim implements Parcelable, Comparable<Article_Slim> {
private final String title;
private final String story;
private final String imagePath;
private int views;

private final Article.Type type;

Expand All @@ -28,6 +29,7 @@ public Article_Slim(Article article) {
else
this.imagePath = "";
this.type = article.getType();
this.views = article.getViews();
}

public static ArrayList<Article_Slim> toArticle_Slim(ArrayList<Article> articles) {
Expand Down Expand Up @@ -90,7 +92,10 @@ public String getImagePath()
{
return imagePath;
}

public int getViews()
{
return views;
}
public Article.Type getType() { return type;}

@NonNull
Expand All @@ -113,6 +118,7 @@ protected Article_Slim(Parcel in) {
story = in.readString();
imagePath = in.readString();
type = (Article.Type) in.readSerializable();
views = in.readInt();
}

@Override
Expand All @@ -128,6 +134,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(story);
dest.writeString(imagePath);
dest.writeSerializable(type);
dest.writeSerializable(views);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public void getNewsArticles(final NewsArticleCallback callback) {
articleImages = r.getString(R.string.fb_art_images),
articleVideos = r.getString(R.string.fb_art_videos),
articleTime = r.getString(R.string.fb_art_time),
articleFeatured = r.getString(R.string.fb_art_featured);
articleFeatured = r.getString(R.string.fb_art_featured),
articleViews = r.getString(R.string.fb_art_views);

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(r.getString(R.string.fb_news_key));
ref.addValueEventListener(new ValueEventListener() {
Expand All @@ -74,6 +75,11 @@ public void onDataChange(@NonNull final DataSnapshot snapshot) {
String body = child_sn.child(articleBody).getValue(String.class);
if(body == null)
body = "";
int num_views = 0;
Integer num_views_temp = child_sn.child(articleViews).getValue(Integer.class);
if(num_views_temp != null){
num_views = num_views_temp.intValue();
}
// so html parse works correctly with new line characters
body = body.replace("\n","<br/>");

Expand All @@ -100,7 +106,7 @@ public void onDataChange(@NonNull final DataSnapshot snapshot) {

long article_time = (long) child_sn.child(articleTime).getValue();

Article article = new Article(ID,article_time,title,author,body,imagePaths,videoIDs, Article.Type.values()[i]);
Article article = new Article(ID,article_time,title,author,body,imagePaths,videoIDs, Article.Type.values()[i], num_views);
boolean is_featured = (boolean) child_sn.child(articleFeatured).getValue();

if(is_featured)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public void init(final Article_Slim article) {
Helper.setTimeText_toView(timeText,
Helper.TimeFromNow(article.getTimeUpdated())
);
// TODO: REMOVE Testing views functionality only
timeText.setText(timeText.getText()+" | "+Integer.toString(article.getViews()) + " views");
String imagePaths = article.getImagePath();
if(imagePaths != null && imagePaths.length() > 0)
Helper.setImageFromUrl_CenterCrop_FullSize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static void setImageFromUrl_CenterCrop(final ImageView view, String url)

}


public static void setImageFromUrl_CenterCrop_FullSize(final ImageView view, String url) {
Glide
.with(view.getContext())
Expand Down
Loading