WIP: Correctly initialize NewPlayerViewModel

We need to pass the viewModel to the view after inflating our player
for the UI to render.
This commit is contained in:
Profpatsch 2024-12-20 19:13:36 +01:00
parent c013a3ed65
commit b351692eea
2 changed files with 17 additions and 6 deletions

View File

@ -58,8 +58,6 @@ import androidx.preference.PreferenceManager;
import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.bottomsheet.BottomSheetBehavior;
import net.newpipe.newplayer.NewPlayer;
import org.schabi.newpipe.databinding.ActivityMainBinding; import org.schabi.newpipe.databinding.ActivityMainBinding;
import org.schabi.newpipe.databinding.DrawerHeaderBinding; import org.schabi.newpipe.databinding.DrawerHeaderBinding;
import org.schabi.newpipe.databinding.DrawerLayoutBinding; import org.schabi.newpipe.databinding.DrawerLayoutBinding;
@ -110,9 +108,6 @@ public class MainActivity extends AppCompatActivity {
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release");
@Inject
NewPlayer newPlayer;
private ActivityMainBinding mainBinding; private ActivityMainBinding mainBinding;
private DrawerHeaderBinding drawerHeaderBinding; private DrawerHeaderBinding drawerHeaderBinding;
private DrawerLayoutBinding drawerLayoutBinding; private DrawerLayoutBinding drawerLayoutBinding;

View File

@ -54,6 +54,9 @@ import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwnerKt;
import androidx.lifecycle.ViewModelProvider;
import androidx.media3.common.util.UnstableApi;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.evernote.android.state.State; import com.evernote.android.state.State;
@ -65,6 +68,10 @@ import com.google.android.material.tabs.TabLayout;
import net.newpipe.newplayer.NewPlayer; import net.newpipe.newplayer.NewPlayer;
import net.newpipe.newplayer.data.PlayMode; import net.newpipe.newplayer.data.PlayMode;
import net.newpipe.newplayer.ui.ContentScale;
import net.newpipe.newplayer.ui.NewPlayerView;
import net.newpipe.newplayer.uiModel.NewPlayerViewModel;
import net.newpipe.newplayer.uiModel.NewPlayerViewModelImpl;
import org.schabi.newpipe.App; import org.schabi.newpipe.App;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
@ -138,7 +145,9 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.schedulers.Schedulers;
import kotlinx.coroutines.CoroutineScopeKt;
@UnstableApi
@AndroidEntryPoint @AndroidEntryPoint
public final class VideoDetailFragment public final class VideoDetailFragment
extends BaseStateFragment<StreamInfo> extends BaseStateFragment<StreamInfo>
@ -240,6 +249,7 @@ public final class VideoDetailFragment
// private Player player; // private Player player;
@Inject @Inject
NewPlayer newPlayer; NewPlayer newPlayer;
NewPlayerViewModel newPlayerViewModel;
// private final PlayerHolder playerHolder = PlayerHolder.getInstance(); // private final PlayerHolder playerHolder = PlayerHolder.getInstance();
// /*////////////////////////////////////////////////////////////////////////// // /*//////////////////////////////////////////////////////////////////////////
@ -316,6 +326,10 @@ public final class VideoDetailFragment
public void onCreate(final Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
newPlayerViewModel = new ViewModelProvider(this).get(NewPlayerViewModelImpl.class);
newPlayerViewModel.setNewPlayer(this.newPlayer);
newPlayerViewModel.setContentFitMode(ContentScale.FIT_INSIDE);
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
showComments = prefs.getBoolean(getString(R.string.show_comments_key), true); showComments = prefs.getBoolean(getString(R.string.show_comments_key), true);
showRelatedItems = prefs.getBoolean(getString(R.string.show_next_video_key), true); showRelatedItems = prefs.getBoolean(getString(R.string.show_next_video_key), true);
@ -1267,8 +1281,10 @@ public final class VideoDetailFragment
// setup the surface view height, so that it fits the video correctly // setup the surface view height, so that it fits the video correctly
setHeightThumbnail(); setHeightThumbnail();
getLayoutInflater().inflate( final View v = getLayoutInflater().inflate(
R.layout.fragment_newplayer_view, binding.playerPlaceholder); R.layout.fragment_newplayer_view, binding.playerPlaceholder);
final NewPlayerView npv = v.findViewById(R.id.embedded_player_newplayer);
npv.setViewModel(newPlayerViewModel);
// player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> { // player.UIs().get(MainPlayerUi.class).ifPresent(playerUi -> {
// // sometimes binding would be null here, even though getView() != null above u.u // // sometimes binding would be null here, even though getView() != null above u.u