Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate empty_state_view xml/view to Jetpack Compose #11463

Open
wants to merge 3 commits into
base: refactor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.compose.ui.platform.ComposeView;

import org.schabi.newpipe.BaseFragment;
import org.schabi.newpipe.R;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;

public class EmptyFragment extends BaseFragment {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. This PR is to have a single purpose. Since applying this API requires firstly migrating EmptyFragment to kotlin, I can start another PR purely for the migration without mixing it into one.

private static final String SHOW_MESSAGE = "SHOW_MESSAGE";
Expand All @@ -26,8 +28,10 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
final Bundle savedInstanceState) {
final boolean showMessage = getArguments().getBoolean(SHOW_MESSAGE);
final View view = inflater.inflate(R.layout.fragment_empty, container, false);
view.findViewById(R.id.empty_state_view).setVisibility(
showMessage ? View.VISIBLE : View.GONE);

final ComposeView composeView = view.findViewById(R.id.empty_state_view);
EmptyStateUtil.setEmptyStateComposable(composeView);
composeView.setVisibility(showMessage ? View.VISIBLE : View.GONE);
return view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
Expand All @@ -20,6 +19,7 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.compose.runtime.MutableState;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;
import androidx.core.view.MenuProvider;
Expand All @@ -44,6 +44,9 @@
import org.schabi.newpipe.ktx.AnimationType;
import org.schabi.newpipe.local.feed.notifications.NotificationHelper;
import org.schabi.newpipe.local.subscription.SubscriptionManager;
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
import org.schabi.newpipe.ui.emptystate.EmptyStateSpecBuilder;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
import org.schabi.newpipe.util.ChannelTabHelper;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.ExtractorHelper;
Expand Down Expand Up @@ -102,6 +105,8 @@ public class ChannelFragment extends BaseStateFragment<ChannelInfo>
private SubscriptionEntity channelSubscription;
private MenuProvider menuProvider;

private MutableState<EmptyStateSpec> emptyStateSpec;

public static ChannelFragment getInstance(final int serviceId, final String url,
final String name) {
final ChannelFragment instance = new ChannelFragment();
Expand Down Expand Up @@ -199,6 +204,10 @@ public View onCreateView(@NonNull final LayoutInflater inflater,
protected void initViews(final View rootView, final Bundle savedInstanceState) {
super.initViews(rootView, savedInstanceState);

emptyStateSpec = EmptyStateUtil.mutableStateOf(
EmptyStateSpec.Companion.getContentNotSupported());
EmptyStateUtil.setEmptyStateComposable(binding.emptyStateView, emptyStateSpec);

tabAdapter = new TabAdapter(getChildFragmentManager());
binding.viewPager.setAdapter(tabAdapter);
binding.tabLayout.setupWithViewPager(binding.viewPager);
Expand Down Expand Up @@ -645,8 +654,10 @@ private void showContentNotSupportedIfNeeded() {
return;
}

binding.errorContentNotSupported.setVisibility(View.VISIBLE);
binding.channelKaomoji.setText("(︶︹︺)");
binding.channelKaomoji.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45f);
emptyStateSpec.setValue(
new EmptyStateSpecBuilder(emptyStateSpec.getValue())
.descriptionVisibility(true)
.build()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.schabi.newpipe.fragments.list.playlist.PlaylistControlViewHolder;
import org.schabi.newpipe.player.playqueue.ChannelTabPlayQueue;
import org.schabi.newpipe.player.playqueue.PlayQueue;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
import org.schabi.newpipe.util.ChannelTabHelper;
import org.schabi.newpipe.util.ExtractorHelper;
import org.schabi.newpipe.util.PlayButtonHelper;
Expand Down Expand Up @@ -78,6 +79,12 @@ public View onCreateView(@NonNull final LayoutInflater inflater,
return inflater.inflate(R.layout.fragment_channel_tab, container, false);
}

@Override
public void onViewCreated(@NonNull final View rootView, final Bundle savedInstanceState) {
super.onViewCreated(rootView, savedInstanceState);
EmptyStateUtil.setEmptyStateComposable(rootView.findViewById(R.id.empty_state_view));
}

@Override
public void onDestroyView() {
super.onDestroyView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.compose.runtime.MutableState;

import org.schabi.newpipe.R;
import org.schabi.newpipe.error.UserAction;
Expand All @@ -19,6 +20,9 @@
import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
import org.schabi.newpipe.info_list.ItemViewMode;
import org.schabi.newpipe.ktx.ViewUtils;
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
import org.schabi.newpipe.ui.emptystate.EmptyStateSpecBuilder;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
import org.schabi.newpipe.util.ExtractorHelper;

import io.reactivex.rxjava3.core.Single;
Expand All @@ -27,7 +31,7 @@
public class CommentsFragment extends BaseListInfoFragment<CommentsInfoItem, CommentsInfo> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've already migrated this fragment to Compose in a separate PR, so I don't think this particular change will be needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. This can wait for your PR.

private final CompositeDisposable disposables = new CompositeDisposable();

private TextView emptyStateDesc;
private MutableState<EmptyStateSpec> emptyStateSpec;

public static CommentsFragment getInstance(final int serviceId, final String url,
final String name) {
Expand All @@ -44,7 +48,10 @@ public CommentsFragment() {
protected void initViews(final View rootView, final Bundle savedInstanceState) {
super.initViews(rootView, savedInstanceState);

emptyStateDesc = rootView.findViewById(R.id.empty_state_desc);
emptyStateSpec = EmptyStateUtil.mutableStateOf(EmptyStateSpec.Companion.getNoComment());
EmptyStateUtil.setEmptyStateComposable(
rootView.findViewById(R.id.empty_state_view),
emptyStateSpec);
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -86,15 +93,23 @@ protected Single<CommentsInfo> loadResult(final boolean forceLoad) {
public void handleResult(@NonNull final CommentsInfo result) {
super.handleResult(result);

emptyStateDesc.setText(
result.isCommentsDisabled()
? R.string.comments_are_disabled
: R.string.no_comments);
if (result.isCommentsDisabled()) {
updateEmptyState(R.string.comments_are_disabled);
} else {
updateEmptyState(R.string.no_comments);
}

ViewUtils.slideUp(requireView(), 120, 150, 0.06f);
disposables.clear();
}

private void updateEmptyState(final @StringRes int descriptionRes) {
final EmptyStateSpec style = new EmptyStateSpecBuilder(emptyStateSpec.getValue())
.descriptionText(descriptionRes)
.build();
emptyStateSpec.setValue(style);
}

/*//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import org.schabi.newpipe.ktx.ExceptionUtils;
import org.schabi.newpipe.local.history.HistoryRecordManager;
import org.schabi.newpipe.settings.NewPipeSettings;
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.DeviceUtils;
import org.schabi.newpipe.util.ExtractorHelper;
Expand Down Expand Up @@ -343,6 +345,10 @@ public void onActivityResult(final int requestCode, final int resultCode, final
protected void initViews(final View rootView, final Bundle savedInstanceState) {
super.initViews(rootView, savedInstanceState);

EmptyStateUtil.setEmptyStateComposable(
searchBinding.emptyStateView,
EmptyStateSpec.Companion.getNoSearchResult());

searchBinding.suggestionsList.setAdapter(suggestionListAdapter);
// animations are just strange and useless, since the suggestions keep changing too much
searchBinding.suggestionsList.setItemAnimator(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
import org.schabi.newpipe.local.holder.RemoteBookmarkPlaylistItemHolder;
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
import org.schabi.newpipe.util.debounce.DebounceSavable;
import org.schabi.newpipe.util.debounce.DebounceSaver;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.debounce.DebounceSavable;
import org.schabi.newpipe.util.debounce.DebounceSaver;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -122,6 +123,7 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {
super.initViews(rootView, savedInstanceState);

itemListAdapter.setUseItemHandle(true);
EmptyStateUtil.setEmptyStateComposable(rootView.findViewById(R.id.empty_state_view));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import org.schabi.newpipe.ktx.slideUp
import org.schabi.newpipe.local.feed.item.StreamItem
import org.schabi.newpipe.local.feed.service.FeedLoadService
import org.schabi.newpipe.local.subscription.SubscriptionManager
import org.schabi.newpipe.ui.emptystate.setEmptyStateComposable
import org.schabi.newpipe.util.DeviceUtils
import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.NavigationHelper
Expand Down Expand Up @@ -132,6 +133,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
override fun onViewCreated(rootView: View, savedInstanceState: Bundle?) {
// super.onViewCreated() calls initListeners() which require the binding to be initialized
_feedBinding = FragmentFeedBinding.bind(rootView)
feedBinding.emptyStateView.setEmptyStateComposable()
super.onViewCreated(rootView, savedInstanceState)

val factory = FeedViewModel.getFactory(requireContext(), groupId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService
import org.schabi.newpipe.local.subscription.services.SubscriptionsImportService.PREVIOUS_EXPORT_MODE
import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard
import org.schabi.newpipe.streams.io.StoredFileHelper
import org.schabi.newpipe.ui.emptystate.setEmptyStateComposable
import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.OnClickGesture
import org.schabi.newpipe.util.ServiceHelper
Expand Down Expand Up @@ -256,6 +257,8 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
binding.itemsList.adapter = groupAdapter
binding.itemsList.itemAnimator = null

binding.emptyStateView.setEmptyStateComposable()

viewModel = ViewModelProvider(this)[SubscriptionViewModel::class.java]
viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(this::handleResult) }
viewModel.feedGroupsLiveData.observe(viewLifecycleOwner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.compose.ui.platform.ComposeView;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -27,6 +28,7 @@
import org.schabi.newpipe.error.UserAction;
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
import org.schabi.newpipe.util.image.CoilHelper;

import java.util.List;
Expand All @@ -40,7 +42,7 @@ public class SelectPlaylistFragment extends DialogFragment {
private OnSelectedListener onSelectedListener = null;

private ProgressBar progressBar;
private TextView emptyView;
private ComposeView emptyView;
private RecyclerView recyclerView;
private Disposable disposable = null;

Expand All @@ -62,6 +64,7 @@ public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup
recyclerView = v.findViewById(R.id.items_list);
emptyView = v.findViewById(R.id.empty_state_view);

EmptyStateUtil.setEmptyStateText(emptyView, R.string.no_playlist_bookmarked_yet);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
final SelectPlaylistAdapter playlistAdapter = new SelectPlaylistAdapter();
recyclerView.setAdapter(playlistAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import androidx.recyclerview.widget.LinearLayoutManager;

import org.schabi.newpipe.databinding.SettingsPreferencesearchFragmentBinding;
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;

import java.util.List;

Expand Down Expand Up @@ -39,6 +41,9 @@ public View onCreateView(
binding = SettingsPreferencesearchFragmentBinding.inflate(inflater, container, false);

binding.searchResults.setLayoutManager(new LinearLayoutManager(getContext()));
EmptyStateUtil.setEmptyStateComposable(
binding.emptyStateView,
EmptyStateSpec.Companion.getNoSearchMaxSizeResult());

adapter = new PreferenceSearchAdapter();
adapter.setOnItemClickListener(this::onItemClicked);
Expand Down
Loading
Loading