Browse Source

fill in the adapter class

pull/61/merge
Thodoris1999 6 years ago
parent
commit
db9c800743
  1. 107
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java
  2. 3
      app/src/main/res/layout/activity_topic_post_row.xml
  3. 52
      app/src/main/res/layout/shout.xml

107
app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java

@ -1,30 +1,64 @@
package gr.thmmy.mthmmy.activities.main.shoutbox; package gr.thmmy.mthmmy.activities.main.shoutbox;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import java.util.ArrayList; import java.util.ArrayList;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
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.Shout; import gr.thmmy.mthmmy.model.Shout;
import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
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 ShoutAdapter extends CustomRecyclerView.Adapter<ShoutAdapter.ShoutViewHolder> { public class ShoutAdapter extends CustomRecyclerView.Adapter<ShoutAdapter.ShoutViewHolder> {
private Context context;
private ArrayList<Shout> shouts; private ArrayList<Shout> shouts;
public ShoutAdapter(ArrayList<Shout> shouts) { public ShoutAdapter(Context context, ArrayList<Shout> shouts) {
this.context = context;
this.shouts = shouts; this.shouts = shouts;
} }
@NonNull @NonNull
@Override @Override
public ShoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public ShoutViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null; View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.shout, parent, false);
return new ShoutViewHolder(view);
} }
@Override @Override
public void onBindViewHolder(@NonNull ShoutViewHolder holder, int position) { public void onBindViewHolder(@NonNull ShoutViewHolder holder, int position) {
Shout currentShout = shouts.get(position);
holder.author.setText(currentShout.getShouter());
holder.dateTime.setText(currentShout.getDate());
holder.shoutContent.setClickable(true);
holder.shoutContent.setWebViewClient(new LinkLauncher());
holder.shoutContent.loadDataWithBaseURL("file:///android_asset/", currentShout.getShout(),
"text/html", "UTF-8", null);
} }
@Override @Override
@ -34,8 +68,73 @@ public class ShoutAdapter extends CustomRecyclerView.Adapter<ShoutAdapter.ShoutV
static class ShoutViewHolder extends CustomRecyclerView.ViewHolder { static class ShoutViewHolder extends CustomRecyclerView.ViewHolder {
public ShoutViewHolder(@NonNull View itemView) { TextView author, dateTime;
WebView shoutContent;
ShoutViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
author = itemView.findViewById(R.id.author_textview);
dateTime = itemView.findViewById(R.id.date_time_textview);
shoutContent = itemView.findViewById(R.id.shout_content);
}
}
class LinkLauncher extends WebViewClient {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return handleUri(uri);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return handleUri(uri);
}
private boolean handleUri(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;
} }
} }
} }

3
app/src/main/res/layout/activity_topic_post_row.xml

@ -198,7 +198,8 @@
android:gravity="start" android:gravity="start"
android:text="" android:text=""
android:textColor="@color/accent" android:textColor="@color/accent"
android:textSize="11sp" /> android:textSize="11sp"
tools:text="date"/>
<TextView <TextView
android:id="@+id/post_number" android:id="@+id/post_number"

52
app/src/main/res/layout/shout.xml

@ -1,6 +1,52 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.cardview.widget.CardView
android:orientation="vertical" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"> xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@color/card_background"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/author_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/accent"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1"
tools:text="author"/>
<TextView
android:id="@+id/date_time_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11sp"
android:textColor="@color/primary_text"
tools:text="date &amp; time"/>
<WebView
android:id="@+id/shout_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@color/card_background"
android:clickable="true"
android:focusable="true"
android:text="@string/post"
tools:ignore="WebViewLayout"/>
</LinearLayout> </LinearLayout>
</androidx.cardview.widget.CardView>
Loading…
Cancel
Save