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

Commit

Permalink
feat(notification-inbox): add sample code to display notifications wi…
Browse files Browse the repository at this point in the history
…th images in customData
  • Loading branch information
jpollak committed Jun 28, 2017
1 parent 2e77408 commit 2f9de83
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ dependencies {

compile 'com.android.support:design:25.3.1'

compile 'com.squareup.picasso:picasso:2.5.2'

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,66 @@
package com.emarsys.mobileengage.sample;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.emarsys.mobileengage.inbox.model.Notification;
import com.squareup.picasso.Picasso;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

public class NotificationListAdapter extends RecyclerView.Adapter<NotificationListAdapter.ViewHolder> {

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public Context context;
public TextView title;
public TextView receivedAt;
public ImageView imageView;

public ViewHolder(LinearLayout root) {
public ViewHolder(ConstraintLayout root) {
super(root);
mTextView = (TextView) root.findViewById(R.id.adapterNotificationTitle);
context = root.getContext();
title = (TextView) root.findViewById(R.id.adapterNotificationTitle);
receivedAt = (TextView) root.findViewById(R.id.receivedAt);
imageView = (ImageView) root.findViewById(R.id.imageview);
}
}

List<Notification> notifications;
DateFormat dateFormat;

public NotificationListAdapter(List<Notification> notifications) {
this.notifications = notifications;
this.dateFormat = DateFormat.getDateTimeInstance();
}

@Override
public NotificationListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LinearLayout root = (LinearLayout) LayoutInflater.from(parent.getContext())
ConstraintLayout root = (ConstraintLayout) LayoutInflater.from(parent.getContext())
.inflate(R.layout.adapter_notification_list_item, parent, false);
return new ViewHolder(root);
}

@Override
public void onBindViewHolder(NotificationListAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(notifications.get(position).getTitle());
Notification notification = notifications.get(position);

holder.title.setText(notification.getTitle());
String dateString = dateFormat.format(new Date(notification.getReceivedAt()));
holder.receivedAt.setText("Received at " + dateString);

String imageUrl = notification.getCustomData().get("image");
if (imageUrl != null) {
Picasso.with(holder.context).load(imageUrl).into(holder.imageView);
} else {
Picasso.with(holder.context).load(R.drawable.ic_image_black).into(holder.imageView);
}
}

@Override
Expand Down
Binary file added app/src/main/res/drawable/ic_image_black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 37 additions & 5 deletions app/src/main/res/layout/adapter_notification_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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:orientation="vertical"
android:padding="@dimen/gap_small">

<TextView
android:id="@+id/adapterNotificationTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@+id/imageview"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Notification title" />

<TextView
android:id="@+id/receivedAt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/imageview"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/adapterNotificationTitle"
tools:text="Received at: 2014. 03. 23" />

<ImageView
android:id="@+id/imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>

0 comments on commit 2f9de83

Please sign in to comment.