diff options
Diffstat (limited to 'app/src/main/java')
7 files changed, 218 insertions, 5 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSidebarActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSidebarActivity.java new file mode 100644 index 00000000..7470887a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSidebarActivity.java @@ -0,0 +1,185 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.text.style.SuperscriptSpan; +import android.text.util.Linkify; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.appbar.AppBarLayout; + +import org.commonmark.ext.gfm.tables.TableBlock; + +import javax.inject.Inject; + +import butterknife.BindView; +import butterknife.ButterKnife; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonConfiguration; +import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.linkify.LinkifyPlugin; +import io.noties.markwon.recycler.MarkwonAdapter; +import io.noties.markwon.recycler.table.TableEntry; +import io.noties.markwon.recycler.table.TableEntryPlugin; +import io.noties.markwon.simple.ext.SimpleExtPlugin; +import io.noties.markwon.urlprocessor.UrlProcessorRelativeToAbsolute; +import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class ViewSidebarActivity extends BaseActivity { + + public static final String EXTRA_SUBREDDIT_NAME = "ESN"; + @BindView(R.id.appbar_layout_view_sidebar_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_view_sidebar_activity) + Toolbar toolbar; + @BindView(R.id.markdown_recycler_view_view_sidebar_activity) + RecyclerView markdownRecyclerView; + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + SharedPreferences mSharedPreferences; + private SubredditViewModel mSubredditViewModel; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_view_sidebar); + + ButterKnife.bind(this); + + Resources resources = getResources(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 + && (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT + || resources.getBoolean(R.bool.isTablet)) + && mSharedPreferences.getBoolean(SharedPreferencesUtils.IMMERSIVE_INTERFACE_KEY, true)) { + Window window = getWindow(); + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + + boolean lightNavBar = false; + if ((resources.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { + lightNavBar = true; + } + boolean finalLightNavBar = lightNavBar; + + View decorView = window.getDecorView(); + if (finalLightNavBar) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() { + @Override + public void onStateChanged(AppBarLayout appBarLayout, AppBarStateChangeListener.State state) { + if (state == State.COLLAPSED) { + if (finalLightNavBar) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + } else if (state == State.EXPANDED) { + if (finalLightNavBar) { + decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); + } + } + } + }); + + int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (statusBarResourceId > 0) { + ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams(); + params.topMargin = getResources().getDimensionPixelSize(statusBarResourceId); + toolbar.setLayoutParams(params); + } + } + + String subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); + if (subredditName == null) { + Toast.makeText(this, R.string.error_getting_subreddit_name, Toast.LENGTH_SHORT).show(); + finish(); + return; + } + + toolbar.setTitle("r/" + subredditName); + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + Markwon markwon = Markwon.builder(this) + .usePlugin(new AbstractMarkwonPlugin() { + @Override + public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { + builder.linkResolver((view, link) -> { + Intent intent = new Intent(ViewSidebarActivity.this, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if (uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + ViewSidebarActivity.this.startActivity(intent); + }).urlProcessor(new UrlProcessorRelativeToAbsolute("https://www.reddit.com")); + } + }) + .usePlugin(StrikethroughPlugin.create()) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .usePlugin(SimpleExtPlugin.create(plugin -> + plugin.addExtension(1, '^', (configuration, props) -> { + return new SuperscriptSpan(); + }) + ) + ) + .usePlugin(TableEntryPlugin.create(this)) + .build(); + MarkwonAdapter markwonAdapter = MarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text) + .include(TableBlock.class, TableEntry.create(builder -> builder + .tableLayout(R.layout.adapter_table_block, R.id.table_layout) + .textLayoutIsRoot(R.layout.view_table_entry_cell))) + .build(); + + markdownRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + markdownRecyclerView.setAdapter(markwonAdapter); + + mSubredditViewModel = new ViewModelProvider(this, + new SubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, subredditName)) + .get(SubredditViewModel.class); + mSubredditViewModel.getSubredditLiveData().observe(this, subredditData -> { + if (subredditData != null) { + markwonAdapter.setMarkdown(markwon, subredditData.getSidebarDescription()); + markwonAdapter.notifyDataSetChanged(); + } + }); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + return false; + } + + @Override + public SharedPreferences getSharedPreferences() { + return mSharedPreferences; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 6dcbc863..1fcb81de 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -548,6 +548,11 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp case R.id.action_change_post_layout_view_subreddit_detail_activity: postLayoutBottomSheetFragment.show(getSupportFragmentManager(), postLayoutBottomSheetFragment.getTag()); return true; + case R.id.action_view_side_bar_view_subreddit_detail_activity: + Intent sidebarIntent = new Intent(this, ViewSidebarActivity.class); + sidebarIntent.putExtra(ViewSidebarActivity.EXTRA_SUBREDDIT_NAME, subredditName); + startActivity(sidebarIntent); + return true; } return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 92b7ae3f..df7141bb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -29,6 +29,7 @@ import ml.docilealligator.infinityforreddit.Activity.ViewImageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMessageActivity; import ml.docilealligator.infinityforreddit.Activity.ViewMultiRedditDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewPostDetailActivity; +import ml.docilealligator.infinityforreddit.Activity.ViewSidebarActivity; import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity; import ml.docilealligator.infinityforreddit.Activity.ViewUserDetailActivity; import ml.docilealligator.infinityforreddit.Fragment.CommentsListingFragment; @@ -122,4 +123,6 @@ public interface AppComponent { void inject(MultiRedditListingActivity multiRedditListingActivity); void inject(ViewMultiRedditDetailActivity viewMultiRedditDetailActivity); + + void inject(ViewSidebarActivity viewSidebarActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index 8a99fd22..78a5efa8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -24,6 +24,7 @@ class ParseSubredditData { String id = subredditDataJsonObject.getString(JSONUtils.NAME_KEY); String subredditFullName = subredditDataJsonObject.getString(JSONUtils.DISPLAY_NAME); String description = subredditDataJsonObject.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); + String sidebarDescription = subredditDataJsonObject.getString(JSONUtils.DESCRIPTION_KEY).trim(); String bannerImageUrl; if (subredditDataJsonObject.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) { @@ -50,7 +51,8 @@ class ParseSubredditData { nSubscribers = subredditDataJsonObject.getInt(JSONUtils.SUBSCRIBERS_KEY); } - return new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers); + return new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, + sidebarDescription, nSubscribers); } interface ParseSubredditDataListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index 586df44b..4287b8b7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -97,9 +97,11 @@ class ParseSubscribedThing { } else { String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME); String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); + String sidebarDescription = data.getString(JSONUtils.DESCRIPTION_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl, accountName, isFavorite)); - newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers)); + newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, + bannerImageUrl, description, sidebarDescription, nSubscribers)); } } lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index df94d020..4a55da1f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -23,7 +23,7 @@ import ml.docilealligator.infinityforreddit.User.UserDao; import ml.docilealligator.infinityforreddit.User.UserData; @Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, - SubscribedUserData.class, MultiReddit.class}, version = 4) + SubscribedUserData.class, MultiReddit.class}, version = 5) public abstract class RedditDataRoomDatabase extends RoomDatabase { private static RedditDataRoomDatabase INSTANCE; @@ -33,7 +33,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.getApplicationContext(), RedditDataRoomDatabase.class, "reddit_data") - .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4) + .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5) .build(); } } @@ -98,4 +98,12 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { "FOREIGN KEY(username) REFERENCES accounts(username) ON DELETE CASCADE)"); } }; + + private static final Migration MIGRATION_4_5 = new Migration(4, 5) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("ALTER TABLE subreddits" + + " ADD COLUMN sidebar_description TEXT"); + } + }; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java index 2bca8963..486bd0a7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDatabase/SubredditData.java @@ -19,15 +19,19 @@ public class SubredditData { private String bannerUrl; @ColumnInfo(name = "description") private String description; + @ColumnInfo(name = "sidebar_description") + private String sidebarDescription; @ColumnInfo(name = "subscribers_count") private int nSubscribers; - public SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, String description, int nSubscribers) { + public SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, + String description, String sidebarDescription, int nSubscribers) { this.id = id; this.name = name; this.iconUrl = iconUrl; this.bannerUrl = bannerUrl; this.description = description; + this.sidebarDescription = sidebarDescription; this.nSubscribers = nSubscribers; } @@ -52,6 +56,10 @@ public class SubredditData { return description; } + public String getSidebarDescription() { + return sidebarDescription; + } + public int getNSubscribers() { return nSubscribers; } |