diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/AppComponent.java b/app/src/main/java/eu/toldi/infinityforlemmy/AppComponent.java index e459968f..087cbe95 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/AppComponent.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/AppComponent.java @@ -82,7 +82,6 @@ import eu.toldi.infinityforlemmy.fragments.ViewImgurVideoFragment; import eu.toldi.infinityforlemmy.fragments.ViewPostDetailFragment; import eu.toldi.infinityforlemmy.fragments.ViewRedditGalleryImageOrGifFragment; import eu.toldi.infinityforlemmy.fragments.ViewRedditGalleryVideoFragment; -import eu.toldi.infinityforlemmy.post.ObjectResolver; import eu.toldi.infinityforlemmy.services.DownloadMediaService; import eu.toldi.infinityforlemmy.services.DownloadRedditVideoService; import eu.toldi.infinityforlemmy.services.EditProfileService; @@ -308,8 +307,6 @@ public interface AppComponent { void inject(MorePostsInfoFragment morePostsInfoFragment); - void inject(ObjectResolver mObjectResolver); - @Component.Factory interface Factory { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/AppModule.java b/app/src/main/java/eu/toldi/infinityforlemmy/AppModule.java index 22924ba8..2b898dcc 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/AppModule.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/AppModule.java @@ -22,6 +22,7 @@ import dagger.Module; import dagger.Provides; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; import eu.toldi.infinityforlemmy.customviews.LoopAvailableExoCreator; +import eu.toldi.infinityforlemmy.post.MarkPostAsRead; import eu.toldi.infinityforlemmy.post.ObjectResolver; import eu.toldi.infinityforlemmy.utils.CustomThemeSharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; @@ -206,7 +207,13 @@ abstract class AppModule { @Provides @Singleton - static ObjectResolver provideObjectResolver() { - return new ObjectResolver(); + static ObjectResolver provideObjectResolver(@Named("no_oauth") RetrofitHolder retrofitHolder) { + return new ObjectResolver(retrofitHolder); + } + + @Provides + @Singleton + static MarkPostAsRead provideMarkPostAsRead(@Named("no_oauth") RetrofitHolder retrofitHolder) { + return new MarkPostAsRead(retrofitHolder); } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/AccountSavedThingActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/AccountSavedThingActivity.java index d980ea71..77b99970 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/AccountSavedThingActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/AccountSavedThingActivity.java @@ -8,6 +8,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.Window; import android.view.WindowManager; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -40,6 +41,7 @@ import eu.toldi.infinityforlemmy.events.ChangeNSFWEvent; import eu.toldi.infinityforlemmy.events.SwitchAccountEvent; import eu.toldi.infinityforlemmy.fragments.CommentsListingFragment; import eu.toldi.infinityforlemmy.fragments.PostFragment; +import eu.toldi.infinityforlemmy.post.MarkPostAsRead; import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.post.PostPagingSource; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; @@ -64,10 +66,15 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + + @Inject + MarkPostAsRead markPostAsRead; private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; private String mAccessToken; private String mAccountName; + + private String mAccountQualifiedName; private PostLayoutBottomSheetFragment postLayoutBottomSheetFragment; private ActivityAccountSavedThingBinding binding; @@ -116,6 +123,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); initializeViewPager(); } @@ -249,7 +257,17 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT @Override public void markPostAsRead(Post post) { - InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId()); + markPostAsRead.markPostAsRead(post.getId(), mAccessToken, new MarkPostAsRead.MarkPostAsReadListener() { + @Override + public void onMarkPostAsReadSuccess() { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountQualifiedName, post.getId()); + } + + @Override + public void onMarkPostAsReadFailed() { + Toast.makeText(AccountSavedThingActivity.this, R.string.mark_post_as_read_failed, Toast.LENGTH_SHORT).show(); + } + }); } private class SectionsPagerAdapter extends FragmentStateAdapter { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/FilteredPostsActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/FilteredPostsActivity.java index 0dda8736..3a105fed 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/FilteredPostsActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/FilteredPostsActivity.java @@ -103,6 +103,8 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec public SubredditViewModel mSubredditViewModel; private String mAccessToken; private String mAccountName; + + private String mAccountQualifiedName; private String name; private String userWhere; private int postType; @@ -158,6 +160,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); name = getIntent().getStringExtra(EXTRA_NAME); postType = getIntent().getIntExtra(EXTRA_POST_TYPE, PostPagingSource.TYPE_FRONT_PAGE); @@ -470,7 +473,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec @Override public void markPostAsRead(Post post) { - InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId()); + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountQualifiedName, post.getId()); } @Override diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java index 56c88f0c..4a8cbbc0 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/LinkResolverActivity.java @@ -197,7 +197,6 @@ public class LinkResolverActivity extends AppCompatActivity { intent.putExtra(ViewPostDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); startActivity(intent); } else { - ((Infinity) getApplication()).getAppComponent().inject(mObjectResolver); mObjectResolver.resolvePost(uri.toString(), mAccessToken, new ObjectResolver.ObjectResolverListener() { @Override public void onResolveObjectSuccess(Object p) { @@ -238,7 +237,6 @@ public class LinkResolverActivity extends AppCompatActivity { } }); } else { - ((Infinity) getApplication()).getAppComponent().inject(mObjectResolver); mObjectResolver.resolveComment(uri.toString(), mAccessToken, new ObjectResolver.ObjectResolverListener() { @Override public void onResolveObjectSuccess(Object c) { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java index c6d21faf..d35a0a25 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/MainActivity.java @@ -113,6 +113,7 @@ import eu.toldi.infinityforlemmy.fragments.PostFragment; import eu.toldi.infinityforlemmy.message.ReadMessage; import eu.toldi.infinityforlemmy.multireddit.MultiReddit; import eu.toldi.infinityforlemmy.multireddit.MultiRedditViewModel; +import eu.toldi.infinityforlemmy.post.MarkPostAsRead; import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.post.PostPagingSource; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; @@ -213,6 +214,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + + @Inject + MarkPostAsRead markPostAsRead; + private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; private NavigationDrawerRecyclerViewMergedAdapter adapter; @@ -1608,7 +1613,17 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public void markPostAsRead(Post post) { - InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId()); + markPostAsRead.markPostAsRead(post.getId(), mAccessToken, new MarkPostAsRead.MarkPostAsReadListener() { + @Override + public void onMarkPostAsReadSuccess() { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountQualifiedName, post.getId()); + } + + @Override + public void onMarkPostAsReadFailed() { + Toast.makeText(MainActivity.this, R.string.mark_post_as_read_failed, Toast.LENGTH_SHORT).show(); + } + }); } public void doNotShowRedditAPIInfoAgain() { diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewMultiRedditDetailActivity.java index f5f4e103..3eed25e6 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewMultiRedditDetailActivity.java @@ -67,6 +67,7 @@ import eu.toldi.infinityforlemmy.events.SwitchAccountEvent; import eu.toldi.infinityforlemmy.fragments.PostFragment; import eu.toldi.infinityforlemmy.multireddit.DeleteMultiReddit; import eu.toldi.infinityforlemmy.multireddit.MultiReddit; +import eu.toldi.infinityforlemmy.post.MarkPostAsRead; import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.post.PostPagingSource; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; @@ -131,8 +132,11 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + @Inject + MarkPostAsRead markPostAsRead; private String mAccessToken; private String mAccountName; + private String mAccountQualifiedName; private String multiPath; private Fragment mFragment; private int fabOption; @@ -221,6 +225,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); lockBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR, false); if (savedInstanceState != null) { @@ -868,7 +873,17 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT @Override public void markPostAsRead(Post post) { - InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId()); + markPostAsRead.markPostAsRead(post.getId(), mAccessToken, new MarkPostAsRead.MarkPostAsReadListener() { + @Override + public void onMarkPostAsReadSuccess() { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountQualifiedName, post.getId()); + } + + @Override + public void onMarkPostAsReadFailed() { + Toast.makeText(ViewMultiRedditDetailActivity.this, R.string.mark_post_as_read_failed, Toast.LENGTH_SHORT).show(); + } + }); } @Override diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java index bc2fdfd2..7bc52e7b 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewSubredditDetailActivity.java @@ -93,6 +93,7 @@ import eu.toldi.infinityforlemmy.fragments.SidebarFragment; import eu.toldi.infinityforlemmy.markdown.MarkdownUtils; import eu.toldi.infinityforlemmy.message.ReadMessage; import eu.toldi.infinityforlemmy.multireddit.MultiReddit; +import eu.toldi.infinityforlemmy.post.MarkPostAsRead; import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.post.PostPagingSource; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; @@ -196,6 +197,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + @Inject + MarkPostAsRead markPostAsRead; + private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; private NavigationWrapper navigationWrapper; @@ -1562,7 +1566,18 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp @Override public void markPostAsRead(Post post) { - InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId()); + markPostAsRead.markPostAsRead(post.getId(), mAccessToken, new MarkPostAsRead.MarkPostAsReadListener() { + @Override + public void onMarkPostAsReadSuccess() { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountQualifiedName, post.getId()); + } + + @Override + public void onMarkPostAsReadFailed() { + Toast.makeText(ViewSubredditDetailActivity.this, R.string.mark_post_as_read_failed, Toast.LENGTH_SHORT).show(); + } + }); + } @Override diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java index aa410161..f48e3cb0 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewUserDetailActivity.java @@ -95,6 +95,7 @@ import eu.toldi.infinityforlemmy.fragments.PostFragment; import eu.toldi.infinityforlemmy.markdown.MarkdownUtils; import eu.toldi.infinityforlemmy.message.ReadMessage; import eu.toldi.infinityforlemmy.multireddit.MultiReddit; +import eu.toldi.infinityforlemmy.post.MarkPostAsRead; import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.post.PostPagingSource; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; @@ -202,6 +203,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; + + @Inject + MarkPostAsRead markPostAsRead; public UserViewModel userViewModel; private FragmentManager fragmentManager; private SectionsPagerAdapter sectionsPagerAdapter; @@ -210,6 +214,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele private Call subredditAutocompleteCall; private String mAccessToken; private String mAccountName; + private String mAccountQualifiedName; private String username; private String qualifiedName; private String description; @@ -272,6 +277,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); lockBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR, false); @@ -1550,7 +1556,17 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele @Override public void markPostAsRead(Post post) { - InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId()); + markPostAsRead.markPostAsRead(post.getId(), mAccessToken, new MarkPostAsRead.MarkPostAsReadListener() { + @Override + public void onMarkPostAsReadSuccess() { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountQualifiedName, post.getId()); + } + + @Override + public void onMarkPostAsReadFailed() { + Toast.makeText(ViewUserDetailActivity.this, R.string.mark_post_as_read_failed, Toast.LENGTH_SHORT).show(); + } + }); } @Override diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java index cc84cc9b..4d763b4f 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java @@ -8,6 +8,7 @@ import eu.toldi.infinityforlemmy.dto.CommentVoteDTO; import eu.toldi.infinityforlemmy.dto.EditCommentDTO; import eu.toldi.infinityforlemmy.dto.FollowCommunityDTO; import eu.toldi.infinityforlemmy.dto.PostVoteDTO; +import eu.toldi.infinityforlemmy.dto.ReadPostDTO; import eu.toldi.infinityforlemmy.dto.SaveCommentDTO; import eu.toldi.infinityforlemmy.dto.SavePostDTO; import eu.toldi.infinityforlemmy.dto.SubmitPostDTO; @@ -98,6 +99,10 @@ public interface LemmyAPI { @PUT("api/v3/post/save") Call postSave(@Body SavePostDTO params); + @Headers("Content-Type: application/json") + @POST("api/v3/post/mark_as_read") + Call postRead(@Body ReadPostDTO params); + @Headers("Content-Type: application/json") @PUT("api/v3/comment/save") Call commentSave(@Body SaveCommentDTO params); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/dto/ReadPostDTO.java b/app/src/main/java/eu/toldi/infinityforlemmy/dto/ReadPostDTO.java new file mode 100644 index 00000000..b0612f8f --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/dto/ReadPostDTO.java @@ -0,0 +1,26 @@ +package eu.toldi.infinityforlemmy.dto; + +public class ReadPostDTO { + + private int post_id; + private boolean read; + private String auth; + + public ReadPostDTO(int post_id, boolean read, String auth) { + this.post_id = post_id; + this.read = read; + this.auth = auth; + } + + public int getPost_id() { + return post_id; + } + + public boolean isRead() { + return read; + } + + public String getAuth() { + return auth; + } +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java index 1ffcb479..50cb79d2 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java @@ -107,6 +107,7 @@ import eu.toldi.infinityforlemmy.message.ReadMessage; import eu.toldi.infinityforlemmy.post.FetchPost; import eu.toldi.infinityforlemmy.post.FetchRemovedPost; import eu.toldi.infinityforlemmy.post.HidePost; +import eu.toldi.infinityforlemmy.post.MarkPostAsRead; import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; import eu.toldi.infinityforlemmy.subreddit.FetchSubredditData; @@ -190,6 +191,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic ExoCreator mExoCreator; @Inject Executor mExecutor; + @Inject + MarkPostAsRead markPostAsRead; @State Post mPost; @State @@ -225,6 +228,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic private Menu mMenu; private String mAccessToken; private String mAccountName; + private String mAccountQualifiedName; private int postListPosition = -1; private Integer mSingleCommentId; private String mContextNumber; @@ -277,6 +281,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); + mAccountQualifiedName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, null); mSavedIcon = getMenuItemIcon(R.drawable.ic_bookmark_toolbar_24dp); mUnsavedIcon = getMenuItemIcon(R.drawable.ic_bookmark_border_toolbar_24dp); @@ -307,7 +312,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mLockFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false); mSwipeUpToHideFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false); mExpandChildren = !mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_TOP_LEVEL_COMMENTS_FIRST, false); - mMarkPostsAsRead = mPostHistorySharedPreferences.getBoolean(mAccountName + SharedPreferencesUtils.MARK_POSTS_AS_READ_BASE, false); + mMarkPostsAsRead = mPostHistorySharedPreferences.getBoolean(mAccountQualifiedName + SharedPreferencesUtils.MARK_POSTS_AS_READ_BASE, false); if (savedInstanceState == null) { mRespectSubredditRecommendedSortType = mSharedPreferences.getBoolean(SharedPreferencesUtils.RESPECT_SUBREDDIT_RECOMMENDED_COMMENT_SORT_TYPE, false); viewPostDetailFragmentId = System.currentTimeMillis(); @@ -1166,8 +1171,18 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic private void tryMarkingPostAsRead() { if (mMarkPostsAsRead && mPost != null && !mPost.isRead()) { mPost.markAsRead(); - InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, mPost.getId()); - EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + markPostAsRead.markPostAsRead(mPost.getId(), mAccessToken, new MarkPostAsRead.MarkPostAsReadListener() { + @Override + public void onMarkPostAsReadSuccess() { + InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountQualifiedName, mPost.getId()); + EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + } + + @Override + public void onMarkPostAsReadFailed() { + Toast.makeText(activity, R.string.mark_post_as_read_failed, Toast.LENGTH_SHORT).show(); + } + }); } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/MarkPostAsRead.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/MarkPostAsRead.java new file mode 100644 index 00000000..b7da035a --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/MarkPostAsRead.java @@ -0,0 +1,51 @@ +package eu.toldi.infinityforlemmy.post; + +import eu.toldi.infinityforlemmy.RetrofitHolder; +import eu.toldi.infinityforlemmy.apis.LemmyAPI; +import eu.toldi.infinityforlemmy.dto.ReadPostDTO; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public class MarkPostAsRead { + RetrofitHolder retrofitHolder; + + public MarkPostAsRead(RetrofitHolder retrofitHolder) { + this.retrofitHolder = retrofitHolder; + } + + private void setPostAsRead(int post_id, boolean markAsRead, String auth, MarkPostAsReadListener markPostAsReadListener) { + LemmyAPI lemmyAPI = retrofitHolder.getRetrofit().create(LemmyAPI.class); + + lemmyAPI.postRead(new ReadPostDTO(post_id, markAsRead, auth)).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful() + && response.body() != null) { + markPostAsReadListener.onMarkPostAsReadSuccess(); + } else { + markPostAsReadListener.onMarkPostAsReadFailed(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + markPostAsReadListener.onMarkPostAsReadFailed(); + } + }); + } + + public void markPostAsRead(int post_id, String auth, MarkPostAsReadListener markPostAsReadListener) { + setPostAsRead(post_id, true, auth, markPostAsReadListener); + } + + public void markPostAsUnread(int post_id, String auth, MarkPostAsReadListener markPostAsReadListener) { + setPostAsRead(post_id, false, auth, markPostAsReadListener); + } + + public interface MarkPostAsReadListener { + void onMarkPostAsReadSuccess(); + + void onMarkPostAsReadFailed(); + } +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/post/ObjectResolver.java b/app/src/main/java/eu/toldi/infinityforlemmy/post/ObjectResolver.java index 20c44786..73594e8d 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/post/ObjectResolver.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/post/ObjectResolver.java @@ -5,9 +5,6 @@ import org.json.JSONObject; import java.util.Objects; -import javax.inject.Inject; -import javax.inject.Named; - import eu.toldi.infinityforlemmy.RetrofitHolder; import eu.toldi.infinityforlemmy.apis.LemmyAPI; import eu.toldi.infinityforlemmy.comment.Comment; @@ -18,10 +15,12 @@ import retrofit2.Response; public class ObjectResolver { - @Inject - @Named("no_oauth") RetrofitHolder retrofitHolder; + public ObjectResolver(RetrofitHolder retrofitHolder) { + this.retrofitHolder = retrofitHolder; + } + public void resolvePost(String query, String auth, ObjectResolverListener objectResolverListener) { LemmyAPI lemmyAPI = retrofitHolder.getRetrofit().create(LemmyAPI.class); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e440272..3ec53902 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1342,4 +1342,5 @@ Anonymous Account Instance URL cannot be null or empty Could not resolve URL :( + Failed to mark post as read