Search in sources :

Example 11 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class QueueFragment method onCreate.

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ShuttleApplication.getInstance().getAppComponent().plus(new FragmentModule(this)).inject(this);
    setHasOptionsMenu(true);
    adapter = new ViewModelAdapter();
}
Also used : FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)

Example 12 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class SuggestedFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ShuttleApplication.getInstance().getAppComponent().plus(new FragmentModule(this)).inject(this);
    adapter = new ViewModelAdapter();
    mostPlayedRecyclerView = new HorizontalRecyclerView("SuggestedFragment - mostPlayed");
    favoriteRecyclerView = new HorizontalRecyclerView("SuggestedFragment - favorite");
}
Also used : FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) HorizontalRecyclerView(com.simplecity.amp_library.ui.modelviews.HorizontalRecyclerView)

Example 13 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class BaseDetailFragment method onCreate.

@Override
public void onCreate(final Bundle icicle) {
    super.onCreate(icicle);
    adapter = new ViewModelAdapter();
    horizontalRecyclerView = new HorizontalRecyclerView("BaseDetail - horizontal");
    detailPresenter = new DetailPresenter(this, this);
    setHasOptionsMenu(true);
    setEnterSharedElementCallback(enterSharedElementCallback);
    if (requestManager == null) {
        requestManager = Glide.with(this);
    }
    isFirstLoad = true;
}
Also used : ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) HorizontalRecyclerView(com.simplecity.amp_library.ui.modelviews.HorizontalRecyclerView)

Example 14 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class ArtworkDialog method build.

public static MaterialDialog build(Context context, ArtworkProvider artworkProvider) {
    @SuppressLint("InflateParams") View customView = LayoutInflater.from(context).inflate(R.layout.dialog_artwork, null);
    ViewModelAdapter adapter = new ViewModelAdapter();
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
    RecyclerView recyclerView = customView.findViewById(R.id.recyclerView);
    recyclerView.addItemDecoration(new SpacesItemDecoration(16));
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setItemViewCacheSize(0);
    recyclerView.setRecyclerListener(new RecyclerListener());
    adapter.items.add(0, new ArtworkLoadingView());
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);
    ArtworkView.GlideListener glideListener = artworkView -> {
        int index = adapter.items.indexOf(artworkView);
        if (index != -1) {
            adapter.removeItem(index);
        }
    };
    List<ViewModel> viewModels = new ArrayList<>();
    UserSelectedArtwork userSelectedArtwork = ((ShuttleApplication) context.getApplicationContext()).userSelectedArtwork.get(artworkProvider.getArtworkKey());
    if (userSelectedArtwork != null) {
        File file = null;
        if (userSelectedArtwork.path != null) {
            file = new File(userSelectedArtwork.path);
        }
        ArtworkView artworkView = new ArtworkView(userSelectedArtwork.type, artworkProvider, glideListener, file, true);
        artworkView.setSelected(true);
        viewModels.add(artworkView);
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.MEDIA_STORE) {
        viewModels.add(new ArtworkView(ArtworkProvider.Type.MEDIA_STORE, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.TAG) {
        viewModels.add(new ArtworkView(ArtworkProvider.Type.TAG, artworkProvider, glideListener));
    }
    if (userSelectedArtwork == null || userSelectedArtwork.type != ArtworkProvider.Type.REMOTE) {
        viewModels.add(new ArtworkView(ArtworkProvider.Type.REMOTE, artworkProvider, glideListener));
    }
    // Dummy Folder ArtworkView - will be replaced or removed depending on availability of folder images
    ArtworkView folderView = new ArtworkView(ArtworkProvider.Type.FOLDER, null, null);
    viewModels.add(folderView);
    ArtworkView.ClickListener listener = artworkView -> {
        Stream.of(viewModels).filter(viewModel -> viewModel instanceof ArtworkView).forEachIndexed((i, viewModel) -> ((ArtworkView) viewModel).setSelected(viewModel == artworkView));
        adapter.notifyItemRangeChanged(0, adapter.getItemCount(), 0);
    };
    Stream.of(viewModels).filter(viewModel -> viewModel instanceof ArtworkView).forEach(viewModel -> ((ArtworkView) viewModel).setListener(listener));
    adapter.setItems(viewModels);
    Observable.fromCallable(artworkProvider::getFolderArtworkFiles).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(files -> {
        adapter.removeItem(adapter.items.indexOf(folderView));
        if (files != null) {
            Stream.of(files).filter(file -> userSelectedArtwork == null || !file.getPath().equals(userSelectedArtwork.path)).forEach(file -> adapter.addItem(new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, false)));
        }
    }, error -> LogUtils.logException(TAG, "Error getting artwork files", error));
    return new MaterialDialog.Builder(context).title(R.string.artwork_edit).customView(customView, false).autoDismiss(false).positiveText(context.getString(R.string.save)).onPositive((dialog, which) -> {
        ArtworkView checkedView = ArtworkDialog.getCheckedView(adapter.items);
        if (checkedView != null) {
            ArtworkModel artworkModel = checkedView.getItem();
            ContentValues values = new ContentValues();
            values.put(CustomArtworkTable.COLUMN_KEY, artworkProvider.getArtworkKey());
            values.put(CustomArtworkTable.COLUMN_TYPE, artworkModel.type);
            values.put(CustomArtworkTable.COLUMN_PATH, artworkModel.file == null ? null : artworkModel.file.getPath());
            context.getContentResolver().insert(CustomArtworkTable.URI, values);
            ((ShuttleApplication) context.getApplicationContext()).userSelectedArtwork.put(artworkProvider.getArtworkKey(), new UserSelectedArtwork(artworkModel.type, artworkModel.file == null ? null : artworkModel.file.getPath()));
        } else {
            context.getContentResolver().delete(CustomArtworkTable.URI, CustomArtworkTable.COLUMN_KEY + "='" + artworkProvider.getArtworkKey().replaceAll("'", "\''") + "'", null);
            ((ShuttleApplication) context.getApplicationContext()).userSelectedArtwork.remove(artworkProvider.getArtworkKey());
        }
        dialog.dismiss();
    }).negativeText(context.getString(R.string.close)).onNegative((dialog, which) -> dialog.dismiss()).neutralText(context.getString(R.string.artwork_gallery)).onNeutral((dialog, which) -> RxImagePicker.with(context).requestImage(Sources.GALLERY).flatMap(uri -> {
        // The directory will be shuttle/custom_artwork/key_hashcode/currentSystemTime.artwork
        // We want the directory to be based on the key, so we can delete old artwork, and the
        // filename to be unique, because it's used for Glide caching.
        File dir = new File(context.getFilesDir() + "/shuttle/custom_artwork/" + artworkProvider.getArtworkKey().hashCode() + "/");
        // Create dir if necessary
        if (!dir.exists()) {
            dir.mkdirs();
        } else {
            // Delete any existing artwork for this key.
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (String child : children) {
                    new File(dir, child).delete();
                }
            }
        }
        File file = new File(dir.getPath() + System.currentTimeMillis() + ".artwork");
        try {
            file.createNewFile();
            if (file.exists()) {
                return RxImageConverters.uriToFile(context, uri, file);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }).filter(file -> file != null && file.exists()).subscribe(file -> {
        // If we've already got user-selected artwork in the adapter, remove it.
        if (adapter.getItemCount() != 0) {
            File aFile = ((ArtworkView) adapter.items.get(0)).file;
            if (aFile != null && aFile.getPath().contains(artworkProvider.getArtworkKey())) {
                adapter.removeItem(0);
            }
        }
        ArtworkView artworkView = new ArtworkView(ArtworkProvider.Type.FOLDER, artworkProvider, glideListener, file, true);
        artworkView.setSelected(true);
        adapter.addItem(0, artworkView);
        recyclerView.scrollToPosition(0);
    }, error -> LogUtils.logException(TAG, "Error picking from gallery", error))).cancelable(false).build();
}
Also used : R(com.simplecity.amp_library.R) Context(android.content.Context) Stream(com.annimon.stream.Stream) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ArrayList(java.util.ArrayList) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) SuppressLint(android.annotation.SuppressLint) View(android.view.View) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) RxImageConverters(com.mlsdev.rximagepicker.RxImageConverters) RxImagePicker(com.mlsdev.rximagepicker.RxImagePicker) ArtworkModel(com.simplecity.amp_library.model.ArtworkModel) SpacesItemDecoration(com.simplecity.amp_library.ui.views.recyclerview.SpacesItemDecoration) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) LayoutInflater(android.view.LayoutInflater) ArtworkLoadingView(com.simplecity.amp_library.ui.modelviews.ArtworkLoadingView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) IOException(java.io.IOException) ArtworkProvider(com.simplecity.amp_library.model.ArtworkProvider) CustomArtworkTable(com.simplecity.amp_library.sql.databases.CustomArtworkTable) File(java.io.File) RecyclerView(android.support.v7.widget.RecyclerView) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) ContentValues(android.content.ContentValues) Nullable(android.support.annotation.Nullable) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Sources(com.mlsdev.rximagepicker.Sources) ContentValues(android.content.ContentValues) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) ArrayList(java.util.ArrayList) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) IOException(java.io.IOException) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ArtworkLoadingView(com.simplecity.amp_library.ui.modelviews.ArtworkLoadingView) ArtworkModel(com.simplecity.amp_library.model.ArtworkModel) View(android.view.View) ArtworkView(com.simplecity.amp_library.ui.modelviews.ArtworkView) ArtworkLoadingView(com.simplecity.amp_library.ui.modelviews.ArtworkLoadingView) RecyclerView(android.support.v7.widget.RecyclerView) UserSelectedArtwork(com.simplecity.amp_library.model.UserSelectedArtwork) SuppressLint(android.annotation.SuppressLint) RecyclerView(android.support.v7.widget.RecyclerView) SpacesItemDecoration(com.simplecity.amp_library.ui.views.recyclerview.SpacesItemDecoration) File(java.io.File) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)

Example 15 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class QueuePagerFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_queue_pager, container, false);
    unbinder = ButterKnife.bind(this, rootView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    if (ShuttleUtils.isLandscape(getContext())) {
        textProtectionScrim.setVisibility(View.GONE);
    }
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(viewModelAdapter);
    SnapHelper snapHelper = new PagerSnapHelper() {

        @Override
        public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
            int snapPosition = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
            if (snapPosition < viewModelAdapter.items.size()) {
                Observable.timer(200, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(o -> {
                    if (mediaManager.getQueuePosition() != snapPosition) {
                        mediaManager.setQueuePosition(snapPosition);
                    }
                }, throwable -> LogUtils.logException(TAG, "Error setting queue position", throwable));
            }
            return snapPosition;
        }
    };
    snapHelper.attachToRecyclerView(recyclerView);
    recyclerView.addOnScrollListener(new RecyclerViewPreloader<>(new ListPreloader.PreloadModelProvider<QueuePagerItemView>() {

        @Override
        public List<QueuePagerItemView> getPreloadItems(int position) {
            QueuePagerItemView queuePagerItemView = (QueuePagerItemView) viewModelAdapter.items.get(position);
            return Collections.singletonList(queuePagerItemView);
        }

        @Override
        public GenericRequestBuilder getPreloadRequestBuilder(QueuePagerItemView item) {
            return requestManager.load(item.song).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(PlaceholderProvider.getInstance(getContext()).getPlaceHolderDrawable(item.song.name, true, settingsManager));
        }
    }, (item, adapterPosition, perItemPosition) -> imageSize, 3));
    recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            // This null check doesn't make sense to me, but there was an NPE here..
            if (recyclerView != null) {
                imageSize = new int[] { recyclerView.getWidth(), recyclerView.getHeight() };
                recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
            }
            return false;
        }
    });
    return rootView;
}
Also used : R(com.simplecity.amp_library.R) Context(android.content.Context) Bundle(android.os.Bundle) SnapHelper(android.support.v7.widget.SnapHelper) ButterKnife(butterknife.ButterKnife) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) NonNull(android.support.annotation.NonNull) RequestManagerProvider(com.simplecity.amp_library.ui.common.RequestManagerProvider) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) BindView(butterknife.BindView) Inject(javax.inject.Inject) GenericRequestBuilder(com.bumptech.glide.GenericRequestBuilder) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) RequestManager(com.bumptech.glide.RequestManager) ListPreloader(com.bumptech.glide.ListPreloader) DiskCacheStrategy(com.bumptech.glide.load.engine.DiskCacheStrategy) View(android.view.View) Observable(io.reactivex.Observable) Unbinder(butterknife.Unbinder) PlaceholderProvider(com.simplecity.amp_library.utils.PlaceholderProvider) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) LogUtils(com.simplecity.amp_library.utils.LogUtils) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) LayoutInflater(android.view.LayoutInflater) AndroidSupportInjection(dagger.android.support.AndroidSupportInjection) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewGroup(android.view.ViewGroup) RecyclerViewPreloader(com.simplecity.amp_library.glide.preloader.RecyclerViewPreloader) BaseFragment(com.simplecity.amp_library.ui.common.BaseFragment) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) List(java.util.List) ViewTreeObserver(android.view.ViewTreeObserver) Collections(java.util.Collections) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) BindView(butterknife.BindView) View(android.view.View) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) SnapHelper(android.support.v7.widget.SnapHelper) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) ViewTreeObserver(android.view.ViewTreeObserver)

Aggregations

ViewModelAdapter (com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)16 RecyclerView (android.support.v7.widget.RecyclerView)7 ViewModel (com.simplecityapps.recycler_adapter.model.ViewModel)7 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)5 R (com.simplecity.amp_library.R)5 List (java.util.List)5 LayoutInflater (android.view.LayoutInflater)4 View (android.view.View)4 FragmentModule (com.simplecity.amp_library.dagger.module.FragmentModule)4 Observable (io.reactivex.Observable)4 SuppressLint (android.annotation.SuppressLint)3 Context (android.content.Context)3 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)3 Stream (com.annimon.stream.Stream)3 Bundle (android.os.Bundle)2 GridLayoutManager (android.support.v7.widget.GridLayoutManager)2 PagerSnapHelper (android.support.v7.widget.PagerSnapHelper)2 SnapHelper (android.support.v7.widget.SnapHelper)2 ViewGroup (android.view.ViewGroup)2 ViewTreeObserver (android.view.ViewTreeObserver)2