Fixing issues with reading info for kbin magazines

This commit fixes the issue where the information for Kbin/Mbin magazines weren't displayed properly on community pages. The problem was related to magazines having a different actor id (/m/ instead of /c/)

Closes #182
This commit is contained in:
Balazs Toldi 2023-11-08 13:19:28 +01:00
parent 67115e0dd6
commit 16dfc65c64
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
5 changed files with 71 additions and 10 deletions

View File

@ -274,6 +274,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
private boolean disableImagePreview;
private boolean hideSubredditDescription;
private boolean isKbinMagazine = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -557,10 +558,14 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
}
return true;
});
String actorID = (!isKbinMagazine) ? LemmyUtils.qualifiedCommunityName2ActorId(qualifiedName) : LemmyUtils.qualifiedMagazineName2ActorId(qualifiedName);
mSubredditViewModel = new ViewModelProvider(this,
new SubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, LemmyUtils.qualifiedCommunityName2ActorId(qualifiedName)))
new SubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, actorID))
.get(SubredditViewModel.class);
addObserverToLiveData();
}
private void addObserverToLiveData() {
mSubredditViewModel.getSubredditLiveData().observe(this, subredditData -> {
if (subredditData != null) {
isNsfwSubreddit = subredditData.isNSFW();
@ -768,6 +773,14 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
communityName = communityData.getTitle();
}
mCommunityStats = communityData.getCommunityStats();
if (communityData.getActorId().contains("/m/")) {
isKbinMagazine = true;
if (mSubredditViewModel != null) {
// Remove current observer
mSubredditViewModel.getSubredditLiveData().removeObservers(ViewSubredditDetailActivity.this);
addObserverToLiveData();
}
}
setupVisibleElements();
communityId = communityData.getId();
ViewSubredditDetailActivity.this.communityData = communityData;

View File

@ -130,6 +130,8 @@ public class SidebarFragment extends Fragment {
private boolean mDisableImagePreview;
private String communityQualifiedName;
private boolean isKbinMagazine = false;
private LinearLayoutManagerBugFixed linearLayoutManager;
private int markdownColor;
private String sidebarDescription;
@ -137,6 +139,8 @@ public class SidebarFragment extends Fragment {
@State
CommunityStats mCommunityStats;
private BasicUserRecyclerViewAdapter moderatorAdapter;
private Markwon markwon;
private MarkwonAdapter markwonAdapter;
public SidebarFragment() {
// Required empty public constructor
@ -227,9 +231,9 @@ public class SidebarFragment extends Fragment {
urlMenuBottomSheetFragment.show(getChildFragmentManager(), null);
return true;
};
Markwon markwon = MarkdownUtils.createFullRedditMarkwon(activity,
markwon = MarkdownUtils.createFullRedditMarkwon(activity,
miscPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener, mDisableImagePreview);
MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter();
markwonAdapter = MarkdownUtils.createTablesAdapter();
linearLayoutManager = new LinearLayoutManagerBugFixed(activity);
recyclerView.setLayoutManager(linearLayoutManager);
@ -252,8 +256,17 @@ public class SidebarFragment extends Fragment {
moderatorsRecyclerView.setAdapter(moderatorAdapter);
mSubredditViewModel = new ViewModelProvider(activity,
new SubredditViewModel.Factory(activity.getApplication(), mRedditDataRoomDatabase, LemmyUtils.qualifiedCommunityName2ActorId(communityQualifiedName)))
new SubredditViewModel.Factory(activity.getApplication(), mRedditDataRoomDatabase, (!isKbinMagazine) ? LemmyUtils.qualifiedCommunityName2ActorId(communityQualifiedName) :
LemmyUtils.qualifiedMagazineName2ActorId(communityQualifiedName)))
.get(SubredditViewModel.class);
addLiveDataObserver();
swipeRefreshLayout.setOnRefreshListener(this::fetchSubredditData);
return rootView;
}
private void addLiveDataObserver() {
mSubredditViewModel.getSubredditLiveData().observe(getViewLifecycleOwner(), subredditData -> {
if (subredditData != null) {
sidebarDescription = subredditData.getSidebarDescription();
@ -276,10 +289,6 @@ public class SidebarFragment extends Fragment {
fetchSubredditData();
}
});
swipeRefreshLayout.setOnRefreshListener(this::fetchSubredditData);
return rootView;
}
@Override
@ -296,6 +305,12 @@ public class SidebarFragment extends Fragment {
swipeRefreshLayout.setRefreshing(false);
mCommunityStats = subredditData.getCommunityStats();
moderatorAdapter.setUsers(subredditData.getModerators());
if (subredditData.getActorId().contains("/m/")) {
isKbinMagazine = true;
mSubredditViewModel.getSubredditLiveData().removeObservers(getViewLifecycleOwner());
mSubredditViewModel.setSubredditName(LemmyUtils.qualifiedMagazineName2ActorId(communityQualifiedName));
addLiveDataObserver();
}
InsertSubredditData.insertSubredditData(mExecutor, new Handler(), mRedditDataRoomDatabase,
subredditData, () -> swipeRefreshLayout.setRefreshing(false));
}

View File

@ -51,7 +51,7 @@ public class ParseSubredditData {
String name = community.getString("name");
String description = "";
if (!community.isNull("description")) {
description = community.getString("description");
description = community.getString("description").trim();
}
boolean removed = community.getBoolean("removed");

View File

@ -5,6 +5,7 @@ import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
@ -14,16 +15,40 @@ public class SubredditViewModel extends AndroidViewModel {
private SubredditRepository mSubredditRepository;
private LiveData<SubredditData> mSubredditLiveData;
private MutableLiveData<String> mSubredditNameLiveData;
private RedditDataRoomDatabase mRedditDataRoomDatabase;
public SubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) {
super(application);
mSubredditRepository = new SubredditRepository(redditDataRoomDatabase, id);
mSubredditLiveData = mSubredditRepository.getSubredditLiveData();
mSubredditNameLiveData = new MutableLiveData<>(id);
mRedditDataRoomDatabase = redditDataRoomDatabase;
}
public LiveData<SubredditData> getSubredditLiveData() {
return mSubredditLiveData;
}
public LiveData<String> getSubredditNameLiveData() {
return mSubredditNameLiveData;
}
public void setSubredditName(String subredditName) {
mSubredditNameLiveData.setValue(subredditName);
updateSubredditRepository();
}
private void updateSubredditRepository() {
String subredditName = mSubredditNameLiveData.getValue();
if (subredditName != null) {
mSubredditRepository = new SubredditRepository(mRedditDataRoomDatabase, subredditName);
mSubredditLiveData = mSubredditRepository.getSubredditLiveData();
}
}
public void insert(SubredditData subredditData) {
mSubredditRepository.insert(subredditData);
}

View File

@ -31,6 +31,14 @@ public class LemmyUtils {
return "https://" + domain + "/u/" + userName;
}
public static String qualifiedMagazineName2ActorId(String qualifiedName) {
String[] splitQualifiedName = qualifiedName.split("@");
String userName = splitQualifiedName[0];
String domain = splitQualifiedName[1];
return "https://" + domain + "/m/" + userName;
}
public static Long dateStringToMills(String dateStr) {
long postTimeMillis = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {