Skip to content

Commit

Permalink
Migrate empty_state_view to Jetpack Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
toliuweijing authored and Profpatsch committed Nov 20, 2024
1 parent 13585ca commit ec45f32
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 121 deletions.
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 {
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 @@ -45,6 +45,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 @@ -26,6 +26,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 @@ -79,6 +80,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 @@ -64,6 +64,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 @@ -344,6 +346,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 @@ -38,6 +38,7 @@
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.ui.emptystate.EmptyStateUtil;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.debounce.DebounceSavable;
Expand Down Expand Up @@ -123,6 +124,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 @@ -257,6 +258,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package org.schabi.newpipe.ui.emptystate

import android.graphics.Color
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.schabi.newpipe.R
import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.ui.theme.errorHint

@Composable
fun EmptyStateComposable(
spec: EmptyStateSpec,
modifier: Modifier = Modifier,
) = EmptyStateComposable(
modifier = spec.modifier(modifier),
emojiModifier = spec.emojiModifier(),
emojiText = spec.emojiText(),
emojiTextStyle = spec.emojiTextStyle(),
descriptionModifier = spec.descriptionModifier(),
descriptionText = spec.descriptionText(),
descriptionTextStyle = spec.descriptionTextStyle(),
descriptionTextVisibility = spec.descriptionVisibility(),
)

@Composable
private fun EmptyStateComposable(
modifier: Modifier,
emojiModifier: Modifier,
emojiText: String,
emojiTextStyle: TextStyle,
descriptionModifier: Modifier,
descriptionText: String,
descriptionTextStyle: TextStyle,
descriptionTextVisibility: Boolean,
) {
CompositionLocalProvider(
LocalContentColor provides MaterialTheme.colorScheme.errorHint
) {
Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
modifier = emojiModifier,
text = emojiText,
style = emojiTextStyle,
)

if (descriptionTextVisibility) {
Text(
modifier = descriptionModifier,
text = descriptionText,
style = descriptionTextStyle,
)
}
}
}
}

@Preview(showBackground = true, backgroundColor = Color.WHITE.toLong())
@Composable
fun EmptyStateComposableGenericErrorPreview() {
AppTheme {
EmptyStateComposable(EmptyStateSpec.GenericError)
}
}

@Preview(showBackground = true, backgroundColor = Color.WHITE.toLong())
@Composable
fun EmptyStateComposableNoCommentPreview() {
AppTheme {
EmptyStateComposable(EmptyStateSpec.NoComment)
}
}

data class EmptyStateSpec(
val modifier: (Modifier) -> Modifier,
val emojiModifier: () -> Modifier,
val emojiText: @Composable () -> String,
val emojiTextStyle: @Composable () -> TextStyle,
val descriptionText: @Composable () -> String,
val descriptionModifier: () -> Modifier,
val descriptionTextStyle: @Composable () -> TextStyle,
val descriptionVisibility: () -> Boolean = { true },
) {

companion object {

val GenericError =
EmptyStateSpec(
modifier = {
it
.fillMaxWidth()
.heightIn(min = 128.dp)
},
emojiModifier = { Modifier },
emojiText = { "¯\\_(ツ)_/¯" },
emojiTextStyle = { MaterialTheme.typography.titleLarge },
descriptionModifier = {
Modifier
.padding(top = 6.dp)
.padding(horizontal = 16.dp)
},
descriptionText = { stringResource(id = R.string.empty_list_subtitle) },
descriptionTextStyle = { MaterialTheme.typography.bodyMedium }
)

val NoComment =
EmptyStateSpec(
modifier = { it.padding(top = 85.dp) },
emojiModifier = { Modifier.padding(bottom = 10.dp) },
emojiText = { "(╯°-°)╯" },
emojiTextStyle = {
LocalTextStyle.current.merge(
fontFamily = FontFamily.Monospace,
fontSize = 35.sp,
)
},
descriptionModifier = { Modifier },
descriptionText = { stringResource(id = R.string.no_comments) },
descriptionTextStyle = {
LocalTextStyle.current.merge(fontSize = 24.sp)
}
)

val NoSearchResult =
NoComment.copy(
modifier = { it },
emojiText = { "╰(°●°╰)" },
descriptionText = { stringResource(id = R.string.search_no_results) }
)

val NoSearchMaxSizeResult =
NoSearchResult.copy(
modifier = { it.fillMaxSize() },
)

val ContentNotSupported =
NoComment.copy(
modifier = { it.padding(top = 90.dp) },
emojiText = { "(︶︹︺)" },
emojiTextStyle = { LocalTextStyle.current.merge(fontSize = 45.sp) },
descriptionModifier = { Modifier.padding(top = 20.dp) },
descriptionText = { stringResource(id = R.string.content_not_supported) },
descriptionTextStyle = { LocalTextStyle.current.merge(fontSize = 15.sp) },
descriptionVisibility = { false },
)
}
}
Loading

0 comments on commit ec45f32

Please sign in to comment.