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

Use dependency injection for ItemLauncher, KeyProcessor and ReportingHelper #3572

Merged
merged 5 commits into from
May 14, 2024
Merged
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 @@ -47,6 +47,7 @@ class SocketHandler(
private val playbackControllerContainer: PlaybackControllerContainer,
private val navigationRepository: NavigationRepository,
private val audioManager: AudioManager,
private val itemLauncher: ItemLauncher,
) {
private val coroutineScope = CoroutineScope(Dispatchers.IO)

Expand Down Expand Up @@ -217,7 +218,7 @@ class SocketHandler(
BaseItemKind.USER_VIEW,
BaseItemKind.COLLECTION_FOLDER -> {
val item by api.userLibraryApi.getItem(itemId = itemId)
ItemLauncher.launchUserView(item)
itemLauncher.launchUserView(item)
}

else -> navigationRepository.navigate(Destinations.itemDetails(itemId))
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/org/jellyfin/androidtv/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.jellyfin.androidtv.data.repository.UserViewsRepository
import org.jellyfin.androidtv.data.repository.UserViewsRepositoryImpl
import org.jellyfin.androidtv.data.service.BackgroundService
import org.jellyfin.androidtv.ui.ScreensaverViewModel
import org.jellyfin.androidtv.ui.itemhandling.ItemLauncher
import org.jellyfin.androidtv.ui.navigation.Destinations
import org.jellyfin.androidtv.ui.navigation.NavigationRepository
import org.jellyfin.androidtv.ui.navigation.NavigationRepositoryImpl
Expand All @@ -35,7 +36,9 @@ import org.jellyfin.androidtv.ui.search.SearchViewModel
import org.jellyfin.androidtv.ui.startup.ServerAddViewModel
import org.jellyfin.androidtv.ui.startup.StartupViewModel
import org.jellyfin.androidtv.ui.startup.UserLoginViewModel
import org.jellyfin.androidtv.util.KeyProcessor
import org.jellyfin.androidtv.util.MarkdownRenderer
import org.jellyfin.androidtv.util.apiclient.ReportingHelper
import org.jellyfin.androidtv.util.sdk.legacy
import org.jellyfin.apiclient.AppInfo
import org.jellyfin.apiclient.android
Expand Down Expand Up @@ -75,7 +78,7 @@ val appModule = module {
get<JellyfinSdk>().createApi()
}

single { SocketHandler(get(), get(), get(), get(), get(), get(), get()) }
single { SocketHandler(get(), get(), get(), get(), get(), get(), get(), get()) }

// Old apiclient
single {
Expand Down Expand Up @@ -126,6 +129,9 @@ val appModule = module {
single { BackgroundService(get(), get(), get(), get(), get()) }

single { MarkdownRenderer(get()) }
single { ItemLauncher() }
single { KeyProcessor() }
single { ReportingHelper() }

factory { (context: Context) -> SearchFragmentDelegate(context, get()) }
factory { (context: Context) -> SearchFragmentDelegate(context, get(), get()) }
}
23 changes: 17 additions & 6 deletions app/src/main/java/org/jellyfin/androidtv/ui/ProgramGridCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.jellyfin.androidtv.R;
import org.jellyfin.androidtv.preference.LiveTvPreferences;
import org.jellyfin.androidtv.ui.livetv.LiveTvGuide;
import org.jellyfin.androidtv.util.InfoLayoutHelper;
import org.jellyfin.androidtv.util.TimeUtils;
import org.jellyfin.androidtv.util.Utils;
import org.jellyfin.androidtv.util.sdk.BaseItemExtensionsKt;
Expand Down Expand Up @@ -74,23 +73,23 @@
LiveTvPreferences liveTvPreferences = get(LiveTvPreferences.class);

if (liveTvPreferences.get(LiveTvPreferences.Companion.getShowNewIndicator()) && BaseItemExtensionsKt.isNew(ModelCompat.asSdk(program)) && (!liveTvPreferences.get(LiveTvPreferences.Companion.getShowPremiereIndicator()) || !Utils.isTrue(program.getIsPremiere()))) {
InfoLayoutHelper.addBlockText(context, mInfoRow, context.getString(R.string.lbl_new), 10, Color.GRAY, R.drawable.dark_green_gradient);
addBlockText(context.getString(R.string.lbl_new), 10, Color.GRAY, R.drawable.dark_green_gradient);
}

if (liveTvPreferences.get(LiveTvPreferences.Companion.getShowPremiereIndicator()) && Utils.isTrue(program.getIsPremiere())) {
InfoLayoutHelper.addBlockText(context, mInfoRow, context.getString(R.string.lbl_premiere), 10, Color.GRAY, R.drawable.dark_green_gradient);
addBlockText(context.getString(R.string.lbl_premiere), 10, Color.GRAY, R.drawable.dark_green_gradient);
}

if (liveTvPreferences.get(LiveTvPreferences.Companion.getShowRepeatIndicator()) && Utils.isTrue(program.getIsRepeat())) {
InfoLayoutHelper.addBlockText(context, mInfoRow, context.getString(R.string.lbl_repeat), 10, Color.GRAY, androidx.leanback.R.color.lb_default_brand_color);
addBlockText(context.getString(R.string.lbl_repeat), 10, Color.GRAY, androidx.leanback.R.color.lb_default_brand_color);

Check warning

Code scanning / Android Lint

Using private resources Warning

The resource @color/lb_default_brand_color is marked as private in androidx.leanback:leanback-preference:1.1.0-rc01
}

if (program.getOfficialRating() != null && !program.getOfficialRating().equals("0")) {
InfoLayoutHelper.addBlockText(context, mInfoRow, program.getOfficialRating(), 10, Color.BLACK, R.drawable.block_text_bg);
addBlockText(program.getOfficialRating(), 10, Color.BLACK, R.drawable.block_text_bg);
}

if (liveTvPreferences.get(LiveTvPreferences.Companion.getShowHDIndicator()) && Utils.isTrue(program.getIsHD())) {
InfoLayoutHelper.addBlockText(context, mInfoRow, "HD", 10, Color.BLACK, R.drawable.block_text_bg);
addBlockText("HD", 10, Color.BLACK, R.drawable.block_text_bg);
}

if (program.getSeriesTimerId() != null) {
Expand All @@ -111,6 +110,18 @@

}

private void addBlockText(String text, int size, int textColor, int backgroundRes) {
TextView view = new TextView(getContext());
view.setTextSize(size);
view.setTextColor(textColor);
view.setText(" " + text + " ");

Check warning

Code scanning / Android Lint

TextView Internationalization Warning

Do not concatenate text displayed with setText. Use resource string with placeholders.
view.setBackgroundResource(backgroundRes);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
params.setMargins(0, Utils.convertDpToPixel(getContext(), -2), 0, 0);
view.setLayoutParams(params);
mInfoRow.addView(view);
}

public void setCellBackground() {
LiveTvPreferences liveTvPreferences = get(LiveTvPreferences.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class BrowseFolderFragment : BrowseSupportFragment(), RowLoader {
protected var rows = mutableListOf<BrowseRowDef>()

private val backgroundService by inject<BackgroundService>()
private val itemLauncher by inject<ItemLauncher>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -47,7 +48,7 @@ abstract class BrowseFolderFragment : BrowseSupportFragment(), RowLoader {
// Add event listeners
onItemViewClickedListener = OnItemViewClickedListener { _: Presenter.ViewHolder?, item: Any?, _: RowPresenter.ViewHolder?, row: Row ->
if (item is BaseRowItem) {
ItemLauncher.launch(
itemLauncher.launch(
item,
(row as ListRow).adapter as ItemRowAdapter,
item.index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public class BrowseGridFragment extends Fragment implements View.OnKeyListener {
private final Lazy<UserRepository> userRepository = inject(UserRepository.class);
private final Lazy<CustomMessageRepository> customMessageRepository = inject(CustomMessageRepository.class);
private final Lazy<NavigationRepository> navigationRepository = inject(NavigationRepository.class);
private final Lazy<ItemLauncher> itemLauncher = inject(ItemLauncher.class);
private final Lazy<KeyProcessor> keyProcessor = inject(KeyProcessor.class);

private int mCardsScreenEst = 0;
private int mCardsScreenStride = 0;
Expand Down Expand Up @@ -207,7 +209,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_UP) return false;
return KeyProcessor.HandleKey(keyCode, mCurrentItem, mActivity);
return keyProcessor.getValue().handleKey(keyCode, mCurrentItem, mActivity);
}

@Override
Expand Down Expand Up @@ -966,7 +968,7 @@ public void onItemClicked(final Presenter.ViewHolder itemViewHolder, Object item
RowPresenter.ViewHolder rowViewHolder, Row row) {

if (!(item instanceof BaseRowItem)) return;
ItemLauncher.launch((BaseRowItem) item, mAdapter, ((BaseRowItem) item).getIndex(), requireContext());
itemLauncher.getValue().launch((BaseRowItem) item, mAdapter, ((BaseRowItem) item).getIndex(), requireContext());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public class EnhancedBrowseFragment extends Fragment implements RowLoader, View.
private final Lazy<CustomMessageRepository> customMessageRepository = inject(CustomMessageRepository.class);
private final Lazy<NavigationRepository> navigationRepository = inject(NavigationRepository.class);
private final Lazy<ApiClient> api = inject(ApiClient.class);
private final Lazy<ItemLauncher> itemLauncher = inject(ItemLauncher.class);
private final Lazy<KeyProcessor> keyProcessor = inject(KeyProcessor.class);

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -352,7 +354,7 @@ protected void setupEventListeners() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_UP) return false;
return KeyProcessor.HandleKey(keyCode, mCurrentItem, requireActivity());
return keyProcessor.getValue().handleKey(keyCode, mCurrentItem, requireActivity());
}

private void refreshCurrentItem() {
Expand Down Expand Up @@ -464,7 +466,7 @@ private final class ItemViewClickedListener implements OnItemViewClickedListener
public void onItemClicked(final Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
if (!(item instanceof BaseRowItem)) return;

ItemLauncher.launch((BaseRowItem) item, (ItemRowAdapter) ((ListRow) row).getAdapter(), ((BaseRowItem) item).getIndex(), requireContext());
itemLauncher.getValue().launch((BaseRowItem) item, (ItemRowAdapter) ((ListRow) row).getAdapter(), ((BaseRowItem) item).getIndex(), requireContext());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class HomeRowsFragment : RowsSupportFragment(), AudioEventListener, View.OnKeyLi
private val dataRefreshService by inject<DataRefreshService>()
private val customMessageRepository by inject<CustomMessageRepository>()
private val navigationRepository by inject<NavigationRepository>()
private val itemLauncher by inject<ItemLauncher>()
private val keyProcessor by inject<KeyProcessor>()

private val helper by lazy { HomeFragmentHelper(requireContext(), userRepository, userViewsRepository) }

Expand Down Expand Up @@ -184,7 +186,7 @@ class HomeRowsFragment : RowsSupportFragment(), AudioEventListener, View.OnKeyLi

override fun onKey(v: View?, keyCode: Int, event: KeyEvent?): Boolean {
if (event?.action != KeyEvent.ACTION_UP) return false
return KeyProcessor.HandleKey(keyCode, currentItem, activity)
return keyProcessor.handleKey(keyCode, currentItem, activity)
}

override fun onResume() {
Expand Down Expand Up @@ -261,7 +263,7 @@ class HomeRowsFragment : RowsSupportFragment(), AudioEventListener, View.OnKeyLi
row: Row?,
) {
if (item !is BaseRowItem) return
ItemLauncher.launch(item, (row as ListRow).adapter as ItemRowAdapter, item.index, requireContext())
itemLauncher.launch(item, (row as ListRow).adapter as ItemRowAdapter, item.index, requireContext())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public class FullDetailsFragment extends Fragment implements RecordingIndicatorV
private Lazy<MarkdownRenderer> markdownRenderer = inject(MarkdownRenderer.class);
private final Lazy<CustomMessageRepository> customMessageRepository = inject(CustomMessageRepository.class);
final Lazy<NavigationRepository> navigationRepository = inject(NavigationRepository.class);
private final Lazy<ItemLauncher> itemLauncher = inject(ItemLauncher.class);
private final Lazy<KeyProcessor> keyProcessor = inject(KeyProcessor.class);

@Nullable
@Override
Expand Down Expand Up @@ -319,7 +321,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_UP) return false;

if (mCurrentItem != null) {
return KeyProcessor.HandleKey(keyCode, mCurrentItem, requireActivity());
return keyProcessor.getValue().handleKey(keyCode, mCurrentItem, requireActivity());
} else if ((keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) && BaseItemExtensionsKt.canPlay(ModelCompat.asSdk(mBaseItem))) {
//default play action
Long pos = mBaseItem.getUserData().getPlaybackPositionTicks() / 10000;
Expand Down Expand Up @@ -1515,7 +1517,7 @@ public void onItemClicked(final Presenter.ViewHolder itemViewHolder, Object item
RowPresenter.ViewHolder rowViewHolder, Row row) {

if (!(item instanceof BaseRowItem)) return;
ItemLauncher.launch((BaseRowItem) item, (ItemRowAdapter) ((ListRow)row).getAdapter(), ((BaseRowItem)item).getIndex(), requireContext());
itemLauncher.getValue().launch((BaseRowItem) item, (ItemRowAdapter) ((ListRow)row).getAdapter(), ((BaseRowItem)item).getIndex(), requireContext());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public class ItemListFragment extends Fragment implements View.OnKeyListener {
private Lazy<MediaManager> mediaManager = inject(MediaManager.class);
private Lazy<VideoQueueManager> videoQueueManager = inject(VideoQueueManager.class);
private Lazy<NavigationRepository> navigationRepository = inject(NavigationRepository.class);
private final Lazy<ItemLauncher> itemLauncher = inject(ItemLauncher.class);

@Nullable
@Override
Expand Down Expand Up @@ -274,7 +275,7 @@ private void showMenu(final ItemRowView row, boolean showOpen) {
open.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
ItemLauncher.launch(new BaseRowItem(row.getItem()), null, 0, requireContext());
itemLauncher.getValue().launch(new BaseRowItem(row.getItem()), null, 0, requireContext());
return true;
}
});
Expand Down
Loading