Search in sources :

Example 61 with Action1

use of rx.functions.Action1 in project pinpoint by naver.

the class CompletableTestRunner method completableError.

public void completableError() throws Exception {
    final String message = "Hello World";
    final Exception expected = new RuntimeException("expected");
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Exception> actual = new AtomicReference<>();
    Subscription subscription = shoutService.shout(message, expected).subscribeOn(Schedulers.computation()).doOnCompleted(new Action0() {

        @Override
        public void call() {
            latch.countDown();
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            actual.set((Exception) throwable);
            latch.countDown();
        }
    }).subscribe();
    latch.await(500L, TimeUnit.MILLISECONDS);
    subscription.unsubscribe();
    Assert.assertSame(expected, actual.get());
    TestHelper.awaitForSpanDataFlush();
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    // Skip rx java internal traces as they differ between versions and it's too much work to split the tests.
    // Instead, we can verify them indirectly by checking if user methods have been traced.
    verifier.ignoreServiceType("RX_JAVA_INTERNAL");
    verifier.awaitTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"), 20, 500);
    Method subscribeMethod = Completable.class.getDeclaredMethod("subscribe");
    verifier.verifyTrace(event("RX_JAVA", subscribeMethod));
    // event - RX_JAVA_INTERNAL some form of Worker.schedule(Action0)
    verifier.verifyTrace(event(ServiceType.ASYNC.getName(), "Asynchronous Invocation"));
    // event - RX_JAVA_INTERNAL some form of Action0 implementation's call() inside OperatorSubscribeOn that gets scheduled
    Method shoutMethod = EchoRepository.class.getDeclaredMethod("shout", String.class, Exception.class);
    verifier.verifyTrace(event(ServiceType.INTERNAL_METHOD.getName(), shoutMethod, expected));
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Subscription(rx.Subscription)

Example 62 with Action1

use of rx.functions.Action1 in project archi by ivacf.

the class RepositoryViewModel method loadFullUser.

private void loadFullUser(String url) {
    ArchiApplication application = ArchiApplication.get(context);
    GithubService githubService = application.getGithubService();
    subscription = githubService.userFromUrl(url).observeOn(AndroidSchedulers.mainThread()).subscribeOn(application.defaultSubscribeScheduler()).subscribe(new Action1<User>() {

        @Override
        public void call(User user) {
            Log.i(TAG, "Full user data loaded " + user);
            ownerName.set(user.name);
            ownerEmail.set(user.email);
            ownerLocation.set(user.location);
            ownerEmailVisibility.set(user.hasEmail() ? View.VISIBLE : View.GONE);
            ownerLocationVisibility.set(user.hasLocation() ? View.VISIBLE : View.GONE);
            ownerLayoutVisibility.set(View.VISIBLE);
        }
    });
}
Also used : GithubService(uk.ivanc.archimvvm.model.GithubService) Action1(rx.functions.Action1) User(uk.ivanc.archimvvm.model.User) ArchiApplication(uk.ivanc.archimvvm.ArchiApplication)

Example 63 with Action1

use of rx.functions.Action1 in project ListenerMusicPlayer by hefuyicoder.

the class FolderAdapter method setOnPopupMenuListener.

private void setOnPopupMenuListener(final ItemHolder itemHolder, final int position) {
    itemHolder.popupMenu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final PopupMenu menu = new PopupMenu(mContext, v);
            int adapterPosition = itemHolder.getAdapterPosition();
            final FolderInfo folderInfo = arraylist.get(adapterPosition);
            menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch(item.getItemId()) {
                        case R.id.popup_folder_addto_queue:
                            getSongListIdByFolder(folderInfo.folderPath).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<long[]>() {

                                @Override
                                public void call(long[] ids) {
                                    MusicPlayer.addToQueue(mContext, ids, -1, ListenerUtil.IdType.Folder);
                                }
                            });
                            break;
                        case R.id.popup_folder_addto_playlist:
                            getSongListIdByFolder(folderInfo.folderPath).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<long[]>() {

                                @Override
                                public void call(long[] ids) {
                                    ListenerUtil.showAddPlaylistDialog(mContext, ids);
                                }
                            });
                            break;
                        case R.id.popup_folder_delete:
                            new MaterialDialog.Builder(mContext).title(mContext.getResources().getString(R.string.delete_folder)).content("删除文件夹下" + folderInfo.songCount + "首歌曲?").positiveText(R.string.delete).negativeText(R.string.cancel).onPositive(new MaterialDialog.SingleButtonCallback() {

                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    getSongListIdByFolder(folderInfo.folderPath).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<long[]>() {

                                        @Override
                                        public void call(long[] ids) {
                                            ListenerUtil.deleteTracks(mContext, ids);
                                            RxBus.getInstance().post(new MediaUpdateEvent());
                                        }
                                    });
                                }
                            }).onNegative(new MaterialDialog.SingleButtonCallback() {

                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    dialog.dismiss();
                                }
                            }).show();
                            break;
                    }
                    return false;
                }
            });
            menu.inflate(R.menu.popup_folder);
            menu.show();
        }
    });
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Action1(rx.functions.Action1) MediaUpdateEvent(io.hefuyi.listener.event.MediaUpdateEvent) MenuItem(android.view.MenuItem) ImageView(android.widget.ImageView) FastScrollRecyclerView(io.hefuyi.listener.widget.fastscroller.FastScrollRecyclerView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) FolderInfo(io.hefuyi.listener.mvp.model.FolderInfo) DialogAction(com.afollestad.materialdialogs.DialogAction) NonNull(android.support.annotation.NonNull) PopupMenu(android.widget.PopupMenu)

Example 64 with Action1

use of rx.functions.Action1 in project StylishMusicPlayer by ryanhoo.

the class FolderPresenter method addFolders.

@Override
public void addFolders(List<File> folders, final List<Folder> existedFolders) {
    Subscription subscription = Observable.from(folders).filter(new Func1<File, Boolean>() {

        @Override
        public Boolean call(File file) {
            for (Folder folder : existedFolders) {
                if (file.getAbsolutePath().equals(folder.getPath())) {
                    return false;
                }
            }
            return true;
        }
    }).flatMap(new Func1<File, Observable<Folder>>() {

        @Override
        public Observable<Folder> call(File file) {
            Folder folder = new Folder();
            folder.setName(file.getName());
            folder.setPath(file.getAbsolutePath());
            List<Song> musicFiles = FileUtils.musicFiles(file);
            folder.setSongs(musicFiles);
            folder.setNumOfSongs(musicFiles.size());
            return Observable.just(folder);
        }
    }).toList().flatMap(new Func1<List<Folder>, Observable<List<Folder>>>() {

        @Override
        public Observable<List<Folder>> call(List<Folder> folders) {
            return mRepository.create(folders);
        }
    }).doOnNext(new Action1<List<Folder>>() {

        @Override
        public void call(List<Folder> folders) {
            Collections.sort(folders, new Comparator<Folder>() {

                @Override
                public int compare(Folder f1, Folder f2) {
                    return f1.getName().compareToIgnoreCase(f2.getName());
                }
            });
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Folder>>() {

        @Override
        public void onStart() {
            mView.showLoading();
        }

        @Override
        public void onCompleted() {
            mView.hideLoading();
        }

        @Override
        public void onError(Throwable e) {
            mView.hideLoading();
            mView.handleError(e);
        }

        @Override
        public void onNext(List<Folder> allNewFolders) {
            mView.onFoldersAdded(allNewFolders);
        }
    });
    mSubscriptions.add(subscription);
}
Also used : Action1(rx.functions.Action1) Folder(io.github.ryanhoo.music.data.model.Folder) Observable(rx.Observable) List(java.util.List) PlayList(io.github.ryanhoo.music.data.model.PlayList) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription) Func1(rx.functions.Func1) File(java.io.File)

Example 65 with Action1

use of rx.functions.Action1 in project StylishMusicPlayer by ryanhoo.

the class LocalMusicPresenter method onLoadFinished.

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    Subscription subscription = Observable.just(cursor).flatMap(new Func1<Cursor, Observable<List<Song>>>() {

        @Override
        public Observable<List<Song>> call(Cursor cursor) {
            List<Song> songs = new ArrayList<>();
            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToFirst();
                do {
                    Song song = cursorToMusic(cursor);
                    songs.add(song);
                } while (cursor.moveToNext());
            }
            return mRepository.insert(songs);
        }
    }).doOnNext(new Action1<List<Song>>() {

        @Override
        public void call(List<Song> songs) {
            Log.d(TAG, "onLoadFinished: " + songs.size());
            Collections.sort(songs, new Comparator<Song>() {

                @Override
                public int compare(Song left, Song right) {
                    return left.getDisplayName().compareTo(right.getDisplayName());
                }
            });
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<List<Song>>() {

        @Override
        public void onStart() {
            mView.showProgress();
        }

        @Override
        public void onCompleted() {
            mView.hideProgress();
        }

        @Override
        public void onError(Throwable throwable) {
            mView.hideProgress();
            Log.e(TAG, "onError: ", throwable);
        }

        @Override
        public void onNext(List<Song> songs) {
            mView.onLocalMusicLoaded(songs);
            mView.emptyView(songs.isEmpty());
        }
    });
    mSubscriptions.add(subscription);
}
Also used : Action1(rx.functions.Action1) Cursor(android.database.Cursor) Observable(rx.Observable) Song(io.github.ryanhoo.music.data.model.Song) ArrayList(java.util.ArrayList) List(java.util.List) CompositeSubscription(rx.subscriptions.CompositeSubscription) Subscription(rx.Subscription)

Aggregations

Action1 (rx.functions.Action1)106 Test (org.junit.Test)33 Action0 (rx.functions.Action0)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 UiThreadTest (android.support.test.annotation.UiThreadTest)20 Observable (rx.Observable)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)11 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 TestSubscriber (rx.observers.TestSubscriber)10 AllTypes (io.realm.entities.AllTypes)9 List (java.util.List)9 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 OnSubscribe (rx.Observable.OnSubscribe)7 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 IOException (java.io.IOException)6 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)5 Method (java.lang.reflect.Method)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5