Browse Source

finish shoutbox

pull/61/merge
Thodoris1999 6 years ago
parent
commit
c2303f660d
  1. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  2. 10
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java
  3. 44
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java
  4. 10
      app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java
  5. 1
      app/src/main/res/layout/fragment_shoutbox.xml
  6. 14
      app/src/main/res/layout/shout.xml

4
app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java

@ -23,6 +23,7 @@ import gr.thmmy.mthmmy.activities.board.BoardActivity;
import gr.thmmy.mthmmy.activities.downloads.DownloadsActivity;
import gr.thmmy.mthmmy.activities.main.forum.ForumFragment;
import gr.thmmy.mthmmy.activities.main.recent.RecentFragment;
import gr.thmmy.mthmmy.activities.main.shoutbox.ShoutboxFragment;
import gr.thmmy.mthmmy.activities.main.unread.UnreadFragment;
import gr.thmmy.mthmmy.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.activities.topic.TopicActivity;
@ -105,6 +106,9 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF
fragmentTransaction.commit();
return true;
case R.id.action_shoutbox:
ShoutboxFragment shoutboxFragment = ShoutboxFragment.newInstance(4);
fragmentTransaction.replace(R.id.fragment_container, shoutboxFragment);
fragmentTransaction.commit();
return true;
default:
return false;

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

@ -54,6 +54,16 @@ public class ShoutAdapter extends CustomRecyclerView.Adapter<ShoutAdapter.ShoutV
public void onBindViewHolder(@NonNull ShoutViewHolder holder, int position) {
Shout currentShout = shouts.get(position);
holder.author.setText(currentShout.getShouter());
holder.author.setOnClickListener(view -> {
Intent intent = new Intent(context, ProfileActivity.class);
Bundle extras = new Bundle();
extras.putString(BUNDLE_PROFILE_URL, shouts.get(holder.getAdapterPosition()).getShouterProfileURL());
extras.putString(BUNDLE_PROFILE_THUMBNAIL_URL, "");
extras.putString(BUNDLE_PROFILE_USERNAME, "");
intent.putExtras(extras);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
});
holder.dateTime.setText(currentShout.getDate());
holder.shoutContent.setClickable(true);
holder.shoutContent.setWebViewClient(new LinkLauncher());

44
app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java

@ -5,12 +5,17 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.activities.main.forum.ForumFragment;
import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.Shout;
import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.NetworkResultCodes;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
public class ShoutboxFragment extends BaseFragment {
@ -18,9 +23,13 @@ public class ShoutboxFragment extends BaseFragment {
private static final String TAG = "ShoutboxFragment";
private MaterialProgressBar progressBar;
private ShoutboxTask shoutboxTask;
private ShoutAdapter shoutAdapter;
private SwipeRefreshLayout swipeRefreshLayout;
private ArrayList<Shout> shouts;
public static ForumFragment newInstance(int sectionNumber) {
ForumFragment fragment = new ForumFragment();
public static ShoutboxFragment newInstance(int sectionNumber) {
ShoutboxFragment fragment = new ShoutboxFragment();
Bundle args = new Bundle();
args.putString(ARG_TAG, TAG);
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
@ -28,6 +37,20 @@ public class ShoutboxFragment extends BaseFragment {
return fragment;
}
private void onShoutboxTaskSarted() {
progressBar.setVisibility(View.VISIBLE);
}
private void onShoutboxTaskFinished(int resultCode, ArrayList<Shout> shouts) {
progressBar.setVisibility(View.INVISIBLE);
swipeRefreshLayout.setRefreshing(false);
if (resultCode == NetworkResultCodes.SUCCESSFUL) {
this.shouts.clear();
this.shouts.addAll(shouts);
shoutAdapter.notifyDataSetChanged();
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -35,7 +58,20 @@ public class ShoutboxFragment extends BaseFragment {
progressBar = rootView.findViewById(R.id.progressBar);
CustomRecyclerView recyclerView = rootView.findViewById(R.id.shoutbox_recyclerview);
shouts = new ArrayList<>();
shoutAdapter = new ShoutAdapter(getContext(), shouts);
recyclerView.setAdapter(shoutAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
shoutboxTask = new ShoutboxTask(this::onShoutboxTaskSarted, this::onShoutboxTaskFinished);
shoutboxTask.execute("https://www.thmmy.gr/smf/index.php?");
swipeRefreshLayout = rootView.findViewById(R.id.swiperefresh);
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.primary);
swipeRefreshLayout.setOnRefreshListener(() -> {
shoutboxTask = new ShoutboxTask(ShoutboxFragment.this::onShoutboxTaskSarted, ShoutboxFragment.this::onShoutboxTaskFinished);
shoutboxTask.execute("https://www.thmmy.gr/smf/index.php?");
});
return super.onCreateView(inflater, container, savedInstanceState);
return rootView;
}
}

10
app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java

@ -11,14 +11,20 @@ import gr.thmmy.mthmmy.utils.parsing.NewParseTask;
import gr.thmmy.mthmmy.utils.parsing.ParseException;
import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import okhttp3.Response;
import timber.log.Timber;
public class ShoutboxTask extends NewParseTask<ArrayList<Shout>> {
public ShoutboxTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener<ArrayList<Shout>> onParseTaskFinishedListener) {
super(onTaskStartedListener, onParseTaskFinishedListener);
}
@Override
protected ArrayList<Shout> parse(Document document, Response response) throws ParseException {
// shout container: document.select("div[class=smalltext]" && div.text().contains("Τελευταίες 75 φωνές:") η στα αγγλικα
Element shoutboxContainer = document.select("div[style=width: 99%; height: 600px; overflow: auto;]").first();
ArrayList<Shout> shouts = new ArrayList<>();
for (Element shout : shoutboxContainer.children()) {
for (Element shout : shoutboxContainer.select("div[style=margin: 4px;]")) {
Element user = shout.child(0);
Element link = user.select("a").first();
String profileUrl = link.attr("href");
@ -37,6 +43,6 @@ public class ShoutboxTask extends NewParseTask<ArrayList<Shout>> {
@Override
protected int getResultCode(Response response, ArrayList<Shout> data) {
return data.size() > 0 ? NetworkResultCodes.SUCCESSFUL : NetworkResultCodes.PERFORM_TASK_ERROR;
return NetworkResultCodes.SUCCESSFUL;
}
}

1
app/src/main/res/layout/fragment_shoutbox.xml

@ -3,7 +3,6 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

@ -6,6 +6,8 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@color/card_background"
card_view:cardCornerRadius="5dp"
@ -21,6 +23,7 @@
android:id="@+id/author_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:textColor="@color/accent"
android:textStyle="bold"
android:ellipsize="end"
@ -31,10 +34,21 @@
android:id="@+id/date_time_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:textSize="11sp"
android:textColor="@color/primary_text"
tools:text="date &amp; time"/>
<View
android:id="@+id/header_body_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="9dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="5dp"
android:background="@color/divider" />
<WebView
android:id="@+id/shout_content"
android:layout_width="match_parent"

Loading…
Cancel
Save