Browse Source

create inbox recyclerview and adapter

pms
Thodoris1999 6 years ago
parent
commit
193a79c78a
  1. 201
      app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java
  2. 11
      app/src/main/java/gr/thmmy/mthmmy/model/Inbox.java
  3. 25
      app/src/main/java/gr/thmmy/mthmmy/model/PM.java
  4. 115
      app/src/main/res/layout/activity_inbox.xml
  5. 31
      app/src/main/res/layout/activity_inbox_pm_row.xml

201
app/src/main/java/gr/thmmy/mthmmy/activities/inbox/InboxAdapter.java

@ -0,0 +1,201 @@
package gr.thmmy.mthmmy.activities.inbox;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.Objects;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.board.BoardActivity;
import gr.thmmy.mthmmy.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.model.PM;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.CircleTransform;
import gr.thmmy.mthmmy.viewmodel.InboxViewModel;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_TITLE;
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_THUMBNAIL_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_URL;
import static gr.thmmy.mthmmy.activities.profile.ProfileActivity.BUNDLE_PROFILE_USERNAME;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL;
public class InboxAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private InboxViewModel inboxViewModel;
public InboxAdapter(InboxActivity context) {
inboxViewModel = ViewModelProviders.of(context).get(InboxViewModel.class);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_inbox_pm_row, parent, false);
return new InboxAdapter.PMViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
InboxAdapter.PMViewHolder holder = (PMViewHolder) viewHolder;
PM currentPM = pms().get(position);
//Post's WebView parameters
holder.pm.setClickable(true);
holder.pm.setWebViewClient(new LinkLauncher());
Picasso.with(context)
.load(currentPM.getThumbnailUrl())
.fit()
.centerCrop()
.error(Objects.requireNonNull(ResourcesCompat.getDrawable(context.getResources()
, R.drawable.ic_default_user_avatar_darker, null)))
.placeholder(Objects.requireNonNull(ResourcesCompat.getDrawable(context.getResources()
, R.drawable.ic_default_user_avatar_darker, null)))
.transform(new CircleTransform())
.into(holder.thumbnail);
//Sets username,submit date, index number, subject, post's and attached files texts
holder.username.setText(currentPM.getAuthor());
holder.pmDate.setText(currentPM.getPostDate());
holder.subject.setText(currentPM.getSubject());
holder.pm.loadDataWithBaseURL("file:///android_asset/", currentPM.getContent(),
"text/html", "UTF-8", null);
}
@Override
public int getItemCount() {
return pms().size();
}
private ArrayList<PM> pms() {
return inboxViewModel.getInbox().getPms();
}
static class PMViewHolder extends RecyclerView.ViewHolder {
final LinearLayout cardChildLinear;
final TextView pmDate, username, subject;
final ImageView thumbnail;
final public WebView pm;
final ImageButton overflowButton;
final RelativeLayout header;
final LinearLayout userExtraInfo;
final TextView specialRank, rank, gender, numberOfPosts, personalText, stars;
PMViewHolder(@NonNull View view) {
super(view);
cardChildLinear = view.findViewById(R.id.card_child_linear);
pmDate = view.findViewById(R.id.pm_date);
thumbnail = view.findViewById(R.id.thumbnail);
username = view.findViewById(R.id.username);
subject = view.findViewById(R.id.subject);
pm = view.findViewById(R.id.pm);
pm.setBackgroundColor(Color.argb(1, 255, 255, 255));
overflowButton = view.findViewById(R.id.pm_overflow_menu);
//User's extra info
header = view.findViewById(R.id.header);
userExtraInfo = view.findViewById(R.id.user_extra_info);
specialRank = view.findViewById(R.id.special_rank);
rank = view.findViewById(R.id.rank);
gender = view.findViewById(R.id.gender);
numberOfPosts = view.findViewById(R.id.number_of_posts);
personalText = view.findViewById(R.id.personal_text);
stars = view.findViewById(R.id.stars);
}
}
/**
* This class is used to handle link clicks in WebViews. When link url is one that the app can
* handle internally, it does. Otherwise user is prompt to open the link in a browser.
*/
@SuppressWarnings("unchecked")
private class LinkLauncher extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(uri);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(uri);
}
@SuppressWarnings("SameReturnValue")
private boolean handleUri(final Uri uri) {
final String uriString = uri.toString();
ThmmyPage.PageCategory target = ThmmyPage.resolvePageCategory(uri);
if (target.is(ThmmyPage.PageCategory.TOPIC)) {
//This url points to a topic
Intent intent = new Intent(context, TopicActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_TOPIC_URL, uriString);
intent.putExtras(extras);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
} else if (target.is(ThmmyPage.PageCategory.BOARD)) {
Intent intent = new Intent(context, BoardActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_BOARD_URL, uriString);
extras.putString(BUNDLE_BOARD_TITLE, "");
intent.putExtras(extras);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
} else if (target.is(ThmmyPage.PageCategory.PROFILE)) {
Intent intent = new Intent(context, ProfileActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_PROFILE_URL, uriString);
extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, "");
extras.putString(BUNDLE_PROFILE_USERNAME, "");
intent.putExtras(extras);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
}
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
//Method always returns true as no url should be loaded in the WebViews
return true;
}
}
}

11
app/src/main/java/gr/thmmy/mthmmy/model/Inbox.java

@ -1,4 +1,15 @@
package gr.thmmy.mthmmy.model;
import java.util.ArrayList;
public class Inbox {
private ArrayList<PM> pms;
public ArrayList<PM> getPms() {
return pms;
}
public void setPms(ArrayList<PM> pms) {
this.pms = pms;
}
}

25
app/src/main/java/gr/thmmy/mthmmy/model/PM.java

@ -1,4 +1,29 @@
package gr.thmmy.mthmmy.model;
public class PM {
private String thumbnailUrl;
private String author;
private String subject;
private String content;
private String postDate;
public String getAuthor() {
return author;
}
public String getContent() {
return content;
}
public String getSubject() {
return subject;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public String getPostDate() {
return postDate;
}
}

115
app/src/main/res/layout/activity_inbox.xml

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
@ -7,19 +7,112 @@
android:orientation="vertical"
tools:context=".activities.inbox.InboxActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ToolbarTheme">
android:layout_height="@dimen/progress_bar_height"
android:indeterminate="true"
android:visibility="invisible"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|center"
app:mpb_indeterminateTint="@color/accent"
app:mpb_progressStyle="horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ToolbarTheme">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:gravity="center"
app:popupTheme="@style/ToolbarTheme" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/inbox_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:layout_below="@id/appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="gr.thmmy.mthmmy.activities.topic.TopicActivity"
tools:listitem="@layout/activity_inbox_pm_row" />
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom|end"
android:background="@color/primary"
app:elevation="8dp"
app:layout_behavior="gr.thmmy.mthmmy.utils.ScrollAwareLinearBehavior">
<ImageButton
android:id="@+id/page_first_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_first"
app:srcCompat="@drawable/page_first" />
<ImageButton
android:id="@+id/page_previous_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_previous"
app:srcCompat="@drawable/page_previous" />
<TextView
android:id="@+id/page_indicator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
app:popupTheme="@style/ToolbarTheme" />
</com.google.android.material.appbar.AppBarLayout>
android:hint="@string/button_page"
android:maxLines="1"
android:textColor="@color/white"
android:textSize="22sp" />
</LinearLayout>
<ImageButton
android:id="@+id/page_next_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_next"
app:srcCompat="@drawable/page_next" />
<ImageButton
android:id="@+id/page_last_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/button_last"
app:srcCompat="@drawable/page_last" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/send_pm_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="50dp"
android:layout_marginEnd="@dimen/fab_margins"
app:layout_behavior="gr.thmmy.mthmmy.utils.ScrollAwareFABBehavior"
app:srcCompat="@drawable/ic_message_white_24dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

31
app/src/main/res/layout/activity_inbox_pm_row.xml

@ -162,7 +162,7 @@
</LinearLayout>
<FrameLayout
android:id="@+id/pm_date_and_number_exp"
android:id="@+id/pm_date_exp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
@ -177,15 +177,6 @@
android:text=""
android:textColor="@color/accent"
android:textSize="11sp" />
<TextView
android:id="@+id/pm_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text=""
android:textColor="@color/accent"
android:textSize="11sp" />
</FrameLayout>
@ -216,26 +207,6 @@
android:focusable="true"
tools:text="@string/pm_content" />
</FrameLayout>
<View
android:id="@+id/body_footer_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="9dp"
android:background="@color/divider"
android:visibility="gone" />
<LinearLayout
android:id="@+id/pm_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="9dp"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Loading…
Cancel
Save