Skip to content

Commit

Permalink
Merge pull request #135 from Popalay/popalay/notification-list
Browse files Browse the repository at this point in the history
Implement Notification list
  • Loading branch information
grishka authored Feb 23, 2021
2 parents 08e0531 + 4759b45 commit 524b6a9
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.grishka.houseclub.api.methods;

import java.util.HashMap;
import java.util.List;

import me.grishka.houseclub.api.ClubhouseAPIRequest;
import me.grishka.houseclub.api.model.Notification;

public class GetNotifications extends ClubhouseAPIRequest<GetNotifications.Response>{
public GetNotifications(int userID, int pageSize, int page){
super("GET", "get_notifications", Response.class);
queryParams=new HashMap<>();
queryParams.put("user_id", userID+"");
queryParams.put("page_size", pageSize+"");
queryParams.put("page", page+"");
}

public static class Response{
public List<Notification> notifications;
public int count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.grishka.houseclub.api.model;

import java.util.Date;

public class Notification {
public int notificationId;
public boolean inUnread;
public User userProfile;
public int eventId;
public int type;
public Date timeCreated;
public String message;

public static final int NOTIFICATION_TYPE_USER=1;
public static final int NOTIFICATION_TYPE_EVENT=16;
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,19 @@ public boolean wantsLightStatusBar(){

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
menu.add("").setIcon(R.drawable.ic_baseline_person_24).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,0,0,"").setIcon(R.drawable.ic_notifications).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,1,0,"").setIcon(R.drawable.ic_baseline_person_24).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
Bundle args=new Bundle();
args.putInt("id", Integer.parseInt(ClubhouseSession.userID));
Nav.go(getActivity(), ProfileFragment.class, args);
if(item.getItemId()==0) {
Nav.go(getActivity(), NotificationListFragment.class, args);
} else if(item.getItemId()==1){
Nav.go(getActivity(), ProfileFragment.class, args);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package me.grishka.houseclub.fragments;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import me.grishka.appkit.Nav;
import me.grishka.appkit.fragments.BaseRecyclerFragment;
import me.grishka.appkit.imageloader.ImageLoaderRecyclerAdapter;
import me.grishka.appkit.imageloader.ImageLoaderViewHolder;
import me.grishka.appkit.utils.BindableViewHolder;
import me.grishka.appkit.views.UsableRecyclerView;
import me.grishka.houseclub.R;
import me.grishka.appkit.api.SimpleCallback;
import me.grishka.houseclub.api.ClubhouseSession;
import me.grishka.houseclub.api.methods.GetNotifications;
import me.grishka.houseclub.api.model.FullUser;
import me.grishka.houseclub.api.model.Notification;

public class NotificationListFragment extends BaseRecyclerFragment<Notification>{
private NotificationListAdapter adapter;

public NotificationListFragment(){
super(50);
}

@Override
public void onAttach(Activity activity){
super.onAttach(activity);
loadData();
setTitle(R.string.notifications_title);
}

@Override
protected RecyclerView.Adapter getAdapter(){
if(adapter==null){
adapter=new NotificationListAdapter();
}
return adapter;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
getToolbar().setElevation(0);
}

@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
getToolbar().setElevation(0);
}

@Override
protected void doLoadData(int offset, int count){
currentRequest=new GetNotifications(getArguments().getInt("id"), 50, offset/50+1)
.setCallback(new SimpleCallback<GetNotifications.Response>(this){
@Override
public void onSuccess(GetNotifications.Response result){
currentRequest=null;
onDataLoaded(result.notifications, data.size()+preloadedData.size()+result.notifications.size()<result.count);
}
})
.exec();
}

private class NotificationListAdapter extends RecyclerView.Adapter<NotificationViewHolder> implements ImageLoaderRecyclerAdapter{

@NonNull
@Override
public NotificationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
return new NotificationViewHolder();
}

@Override
public void onBindViewHolder(@NonNull NotificationViewHolder holder, int position){
holder.bind(data.get(position));
}

@Override
public int getItemCount(){
return data.size();
}

@Override
public int getImageCountForItem(int position){
return data.get(position).userProfile.photoUrl!=null ? 1 : 0;
}

@Override
public String getImageURL(int position, int image){
return data.get(position).userProfile.photoUrl;
}
}

private class NotificationViewHolder extends BindableViewHolder<Notification> implements ImageLoaderViewHolder, UsableRecyclerView.Clickable{

public TextView name, message, time;
public Button followBtn;
public ImageView photo;
private Drawable placeholder=new ColorDrawable(0xFF808080);

public NotificationViewHolder(){
super(getActivity(), R.layout.notification_list_row);

name=findViewById(R.id.name);
message=findViewById(R.id.message);
time=findViewById(R.id.time);
photo=findViewById(R.id.photo);
}

@Override
public void onBind(Notification item){
itemView.setAlpha(item.inUnread?1F:0.5F);
name.setText(item.userProfile.name);
message.setText(item.message);
time.setText(DateUtils.getRelativeTimeSpanString(item.timeCreated.getTime()));

if(item.userProfile.photoUrl!=null)
imgLoader.bindViewHolder(adapter, this, getAdapterPosition());
else
photo.setImageDrawable(placeholder);
}

@Override
public void setImage(int index, Bitmap bitmap){
photo.setImageBitmap(bitmap);
}

@Override
public void clearImage(int index){
photo.setImageDrawable(placeholder);
}

@Override
public void onClick(){
Bundle args=new Bundle();
args.putInt("id", item.userProfile.userId);
Nav.go(getActivity(), ProfileFragment.class, args);
}
}
}
10 changes: 10 additions & 0 deletions Houseclub/src/main/res/drawable/ic_notifications.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#000"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z" />
</vector>
53 changes: 53 additions & 0 deletions Houseclub/src/main/res/layout/notification_list_row.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingRight="16dp"
android:paddingBottom="8dp">

<me.grishka.houseclub.views.SquircleImageView
android:id="@+id/photo"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginRight="12dp"
tools:src="#0f0" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_marginRight="4dp"
android:orientation="vertical">

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="sans-serif-medium"
android:singleLine="true"
android:textSize="14dp"
tools:text="User Name" />

<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="13dp"
tools:text="bio" />

</LinearLayout>

<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
tools:text="18h ago" />

</LinearLayout>
1 change: 1 addition & 0 deletions Houseclub/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<string name="following">Following</string>
<string name="followers_title">Followers</string>
<string name="following_title">Following</string>
<string name="notifications_title">Notifications</string>
<string name="confirm_unfollow">Unfollow <b>%s</b>?</string>
<string name="yes">Yes</string>
<string name="no">No</string>
Expand Down

0 comments on commit 524b6a9

Please sign in to comment.