diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 00358438..a927d54d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/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; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java index 8f1230ea..dffa7716 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutAdapter.java @@ -54,6 +54,16 @@ public class ShoutAdapter extends CustomRecyclerView.Adapter { + 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()); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java index 4c0d08d9..7692342c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxFragment.java +++ b/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 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 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; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java index 74359195..c88b2960 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/shoutbox/ShoutboxTask.java +++ b/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> { + + public ShoutboxTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener> onParseTaskFinishedListener) { + super(onTaskStartedListener, onParseTaskFinishedListener); + } + @Override protected ArrayList 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 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> { @Override protected int getResultCode(Response response, ArrayList data) { - return data.size() > 0 ? NetworkResultCodes.SUCCESSFUL : NetworkResultCodes.PERFORM_TASK_ERROR; + return NetworkResultCodes.SUCCESSFUL; } } diff --git a/app/src/main/res/layout/fragment_shoutbox.xml b/app/src/main/res/layout/fragment_shoutbox.xml index 107db8b8..24e5058f 100644 --- a/app/src/main/res/layout/fragment_shoutbox.xml +++ b/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"> diff --git a/app/src/main/res/layout/shout.xml b/app/src/main/res/layout/shout.xml index c1c24563..c9f17aa7 100644 --- a/app/src/main/res/layout/shout.xml +++ b/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 & time"/> + +