public static PowerMenu getHamburgerPowerMenu(Context context, LifecycleOwner lifecycleOwner, OnMenuItemClickListener onMenuItemClickListener) { return new PowerMenu.Builder(context) .addItem(new PowerMenuItem("Novel", true)) .addItem(new PowerMenuItem("Poetry", false)) .addItem(new PowerMenuItem("Art", false)) .addItem(new PowerMenuItem("Journals", false)) .addItem(new PowerMenuItem("Travel", false)) .setLifecycleOwner(lifecycleOwner) .setAnimation(MenuAnimation.SHOWUP_TOP_LEFT) .setMenuRadius(10f) .setMenuShadow(10f) .setTextColor(context.getResources().getColor(R.color.md_grey_800)) .setSelectedTextColor(Color.WHITE) .setMenuColor(Color.WHITE) .setSelectedMenuColor(context.getResources().getColor(R.color.colorPrimary)) .setOnMenuItemClickListener(onMenuItemClickListener) .build(); }
public static CustomPowerMenu getWritePowerMenu(Context context, LifecycleOwner lifecycleOwner, OnMenuItemClickListener onMenuItemClickListener) { ColorDrawable drawable = new ColorDrawable(context.getResources().getColor(R.color.md_blue_grey_300)); return new CustomPowerMenu.Builder<>(context, new CenterMenuAdapter()) .addItem("Novel") .addItem("Poetry") .addItem("Art") .addItem("Journals") .addItem("Travel") .setLifecycleOwner(lifecycleOwner) .setAnimation(MenuAnimation.FADE) .setMenuRadius(10f) .setMenuShadow(10f) .setDivider(drawable) .setDividerHeight(1) .setOnMenuItemClickListener(onMenuItemClickListener) .build(); }
public static void playInfinite(final LifecycleOwner lifecycleOwner, final Context context, final String mp3) { playObservable(lifecycleOwner, context, mp3) .subscribe( new Consumer<Boolean>() { @Override public void accept(Boolean $) throws Exception { play(lifecycleOwner, context, mp3); } }, new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { throwable.printStackTrace(); } }); }
public ClaimItemAdapter( final Context context, final LifecycleOwner owner, final LiveData<List<ClaimItem>> liveItems) { this.layoutInflater = LayoutInflater.from(context); this.itemPresenter = new ItemPresenter(context); liveItems.observe(owner, new Observer<List<ClaimItem>>() { public void onChanged(final List<ClaimItem> claimItems) { ClaimItemAdapter.this.items = (claimItems != null) ? claimItems : Collections.<ClaimItem>emptyList(); ClaimItemAdapter.this.notifyDataSetChanged(); } }); }
public AllowanceOverviewPresenter( final LifecycleOwner lifecycleOwner, final int allowance) { ClaimApplication.getClaimDatabase() .claimItemDao() .selectAll() .observe(lifecycleOwner, new Observer<List<ClaimItem>>() { @Override public void onChanged(final List<ClaimItem> claimItems) { updateSpendStatsCommand.exec(claimItems); } }); this.allowance.set(allowance); }
public ClaimItemAdapter( final Context context, final LifecycleOwner owner, final LiveData<List<ClaimItem>> liveItems) { this.layoutInflater = LayoutInflater.from(context); this.itemPresenter = new ItemPresenter(context); liveItems.observe(owner, new Observer<List<ClaimItem>>() { @Override public void onChanged(final List<ClaimItem> claimItems) { if (!items.isEmpty()) { updateCommand.exec(Pair.create(items, claimItems)); } else { createDisplayListCommand.exec(claimItems); } } }); }
@MainThread public void observe(LifecycleOwner owner, final Observer<T> observer) { if (hasActiveObservers()) { Log.w(TAG, "Multiple observers registered but only one will be notified of changes."); } // Observe the internal MutableLiveData super.observe(owner, new Observer<T>() { @Override public void onChanged(@Nullable T t) { if (mPending.compareAndSet(true, false)) { observer.onChanged(t); } } }); }
@Test public void givenUseCaseWhenExecuteThenRespondErrorCallback() throws Exception{ HandledException mockHandledException = mock(HandledException.class); when(mockUseCase.lifecycleOwner()).thenReturn(mock(LifecycleOwner.class)); when(mockUseCase.run(anyString())).thenThrow(mock(Exception.class)); when(mockUseCase.canRespond()).thenReturn(true); when(mockExceptionController.handle(any(Exception.class))).thenReturn(mockHandledException); when(mockUseCase.exceptionController()).thenReturn(mockExceptionController); when(mockUseCase.canRun()).thenReturn(true); boolean added = useCaseExecutor.execute(mockArgs, mockUseCase, mockSuccessCallback, mockExceptionCallback); verify(mockTasks, times(1)).addUseCase(mockUseCase); assertThat(added, equalTo(true)); verify(mockUseCase, times(1)).run(mockArgs); verify(mockTasks, times(1)).removeUseCase(mockUseCase); verifyZeroInteractions(mockSuccessCallback); verify(mockExceptionCallback, times(1)).onException(errorArgCaptor.capture()); assertThat(errorArgCaptor.getValue(), equalTo(mockHandledException)); }
private void testLifecycle(ActivityController<? extends LifecycleOwner> controller) { LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(controller.get()); TestObserver<Lifecycle.Event> testObserver = provider.lifecycle().test(); controller.create(); controller.start(); controller.resume(); controller.pause(); controller.stop(); controller.destroy(); testObserver.assertValues( Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME, Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP, Lifecycle.Event.ON_DESTROY ); }
private void testBindUntilEvent(ActivityController<? extends LifecycleOwner> controller) { LifecycleProvider<Lifecycle.Event> activity = AndroidLifecycle.createLifecycleProvider(controller.get()); TestObserver<Object> testObserver = observable.compose(activity.bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); controller.create(); testObserver.assertNotComplete(); controller.start(); testObserver.assertNotComplete(); controller.resume(); testObserver.assertNotComplete(); controller.pause(); testObserver.assertNotComplete(); controller.stop(); testObserver.assertComplete(); }
private void testLifecycle(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; ActivityController<?> controller = startFragment(fragment); TestObserver<Lifecycle.Event> testObserver = AndroidLifecycle.createLifecycleProvider(owner).lifecycle().test(); controller.start(); controller.resume(); controller.pause(); controller.stop(); controller.destroy(); testObserver.assertValues( Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME, Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP, Lifecycle.Event.ON_DESTROY ); }
private void testBindUntilEvent(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; ActivityController<?> controller = startFragment(fragment); TestObserver<Object> testObserver = observable.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); testObserver.assertNotComplete(); controller.start(); testObserver.assertNotComplete(); controller.resume(); testObserver.assertNotComplete(); controller.pause(); testObserver.assertNotComplete(); controller.stop(); testObserver.assertComplete(); }
private void loadTodo(int tID) { todosViewModel.findByID(tID).observe((LifecycleOwner) this, new Observer<Todo>() { @Override public void onChanged(@Nullable Todo todo) { thistodo = todo; edit_title.setText(thistodo.getTitle()); edit_desc.setText(thistodo.getDesc()); edit_priority.setProgress(thistodo.getPriority()); deadline = todo.getDeadline_date(); if(deadline != null) edit_deadline.setText(todo.getDeadline_date().toString()); edit_cost.setText(String.valueOf(thistodo.getCost())); edit_time.setText(String.valueOf(thistodo.getTime())); Log.w("projectID", String.valueOf(thistodo.getProject())); loadParentProject(todo.getProject()); } }); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_todo_feed, container, false); recyclerView = v.findViewById(R.id.recyclerView); recyclerViewAdapter = new RecyclerViewAdapter<Todo>(new ArrayList<Todo>(),this, this); recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity().getBaseContext())); recyclerView.setAdapter(recyclerViewAdapter); viewModel = ViewModelProviders.of(this).get(todosViewModel.class); viewModel.getAll().observe((LifecycleOwner) getActivity(), new Observer<List<Todo>>() { @Override public void onChanged(@Nullable List<Todo> todos) { recyclerViewAdapter.addItems(todos); } }); return v; }
private void loadProject(int pID) { projectsViewModel.findByID(pID).observe((LifecycleOwner) this, new Observer<Project>() { @Override public void onChanged(@Nullable Project project) { getSupportActionBar().setTitle(project.getTitle()); getSupportActionBar().setSubtitle(project.getDesc()); thisprojecta = project; edit_title.setText(project.getTitle()); edit_desc.setText(project.getDesc()); edit_priority.setProgress(project.getPriority()); deadline = project.getDead_line(); if(deadline != null) edit_deadline.setText(project.getDead_line().toString()); } }); }
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_projects_feed, container, false); recyclerView = v.findViewById(R.id.recyclerView); recyclerViewAdapter = new RecyclerViewAdapter<Project>(new ArrayList<Project>(), this, this); recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity().getBaseContext())); recyclerView.setAdapter(recyclerViewAdapter); projectsViewModel = MainActivity.getProjectsViewModel(); todosViewModel = MainActivity.getTodosViewModel(); projectsViewModel.getAll().observe((LifecycleOwner) getActivity(), new Observer<List<Project>>() { @Override public void onChanged(@Nullable List<Project> projects) { recyclerViewAdapter.addItems(projects); } }); return v; }
@MainThread public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) { super.observe(owner, t -> { if (mPending.compareAndSet(true, false)) { observer.onChanged(t); } }); }
/** * Subscribes the {@link InstructionView} and {@link SummaryBottomSheet} to the {@link NavigationViewModel}. * <p> * Then, creates an instance of {@link NavigationViewSubscriber}, which takes a presenter and listener. * <p> * The subscriber then subscribes to the view models, setting up the appropriate presenter / listener * method calls based on the {@link android.arch.lifecycle.LiveData} updates. */ private void subscribeViewModels() { instructionView.subscribe(navigationViewModel); summaryBottomSheet.subscribe(navigationViewModel); NavigationViewSubscriber subscriber = new NavigationViewSubscriber(navigationPresenter, navigationViewEventDispatcher); subscriber.subscribe(((LifecycleOwner) getContext()), locationViewModel, routeViewModel, navigationViewModel); }
/** * Adds this view as a lifecycle observer. * This needs to be done earlier than the other observers (prior to the style loading). */ private void initNavigationViewObserver() { try { ((LifecycleOwner) getContext()).getLifecycle().addObserver(this); } catch (ClassCastException exception) { throw new ClassCastException("Please ensure that the provided Context is a valid LifecycleOwner"); } }
/** * Add lifecycle observers to ensure these objects properly * start / stop based on the Android lifecycle. */ private void initLifecycleObservers() { try { ((LifecycleOwner) getContext()).getLifecycle().addObserver(locationLayer); ((LifecycleOwner) getContext()).getLifecycle().addObserver(locationViewModel); } catch (ClassCastException exception) { throw new ClassCastException("Please ensure that the provided Context is a valid LifecycleOwner"); } }
@MainThread public void observe(LifecycleOwner owner, final Observer<T> observer) { if (hasActiveObservers()) { Log.w(TAG, "Multiple observers registered but only one will be notified of changes."); } super.observe(owner, new Observer<T>() { @Override public void onChanged(@Nullable T t) { if (mPending.compareAndSet(true, false)) { observer.onChanged(t); } } }); }
public void observe(LifecycleOwner owner, final SnackbarObserver observer) { super.observe(owner, new Observer<String>() { @Override public void onChanged(@Nullable String message) { if (TextUtils.isEmpty(message)) return; observer.onNewMessage(message); } }); }
public static PowerMenu getProfilePowerMenu(Context context, LifecycleOwner lifecycleOwner, OnMenuItemClickListener onMenuItemClickListener) { return new PowerMenu.Builder(context) .addItem(new PowerMenuItem("Profile", false)) .addItem(new PowerMenuItem("Board", false)) .addItem(new PowerMenuItem("Logout", false)) .setLifecycleOwner(lifecycleOwner) .setAnimation(MenuAnimation.SHOWUP_TOP_RIGHT) .setMenuRadius(10f) .setMenuShadow(10f) .setTextColor(context.getResources().getColor(R.color.md_grey_800)) .setMenuColor(Color.WHITE) .setSelectedEffect(false) .setOnMenuItemClickListener(onMenuItemClickListener) .build(); }
public static CustomPowerMenu getAlertPowerMenu(Context context, LifecycleOwner lifecycleOwner, OnMenuItemClickListener onMenuItemClickListener) { return new CustomPowerMenu.Builder<>(context, new CenterMenuAdapter()) .addItem("You need to login!") .setLifecycleOwner(lifecycleOwner) .setAnimation(MenuAnimation.ELASTIC_CENTER) .setMenuRadius(10f) .setMenuShadow(10f) .setFocusable(true) .setOnMenuItemClickListener(onMenuItemClickListener) .setOnBackgroundClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }).build(); }
public static CustomPowerMenu getIconPowerMenu(Context context, LifecycleOwner lifecycleOwner, OnMenuItemClickListener onMenuItemClickListener) { return new CustomPowerMenu.Builder<>(context, new IconMenuAdapter()) .addItem(new IconPowerMenuItem(context.getResources().getDrawable(R.drawable.ic_wechat), "WeChat")) .addItem(new IconPowerMenuItem(context.getResources().getDrawable(R.drawable.ic_facebook), "Facebook")) .addItem(new IconPowerMenuItem(context.getResources().getDrawable(R.drawable.ic_twitter), "Twitter")) .addItem(new IconPowerMenuItem(context.getResources().getDrawable(R.drawable.ic_line), "Line")) .setLifecycleOwner(lifecycleOwner) .setOnMenuItemClickListener(onMenuItemClickListener) .setAnimation(MenuAnimation.SHOWUP_TOP_RIGHT) .setMenuRadius(10f) .setMenuShadow(10f) .build(); }
public static PowerMenu getDialogPowerMenu(Context context, LifecycleOwner lifecycleOwner) { return new PowerMenu.Builder(context) .setHeaderView(R.layout.layout_dialog_header) .setFooterView(R.layout.layout_dialog_footer) .addItem(new PowerMenuItem("This is DialogPowerMenu", false)) .setLifecycleOwner(lifecycleOwner) .setAnimation(MenuAnimation.SHOW_UP_CENTER) .setMenuRadius(10f) .setMenuShadow(10f) .setWith(600) .setSelectedEffect(false) .build(); }
public static CustomPowerMenu getCustomDialogPowerMenu(Context context, LifecycleOwner lifecycleOwner) { return new CustomPowerMenu.Builder<>(context, new CustomDialogMenuAdapter()) .setHeaderView(R.layout.layout_custom_dialog_header) .setFooterView(R.layout.layout_custom_dialog_footer) .addItem(new NameCardMenuItem(context.getResources().getDrawable(R.drawable.face3), "Sophie", context.getString(R.string.board3))) .setLifecycleOwner(lifecycleOwner) .setAnimation(MenuAnimation.SHOW_UP_CENTER) .setWith(800) .setMenuRadius(10f) .setMenuShadow(10f) .build(); }
@Override public void onStart() { //将 LifecycleObserver 注册给 LifecycleOwner 后 @OnLifecycleEvent 才可以正常使用 if (mRootView != null && mRootView instanceof LifecycleOwner) { ((LifecycleOwner) mRootView).getLifecycle().addObserver(this); if (mModel!= null && mModel instanceof LifecycleObserver){ ((LifecycleOwner) mRootView).getLifecycle().addObserver((LifecycleObserver) mModel); } } if (useEventBus())//如果要使用 Eventbus 请将此方法返回 true EventBus.getDefault().register(this);//注册 Eventbus }
/** * 只有当 {@code mRootView} 不为 null, 并且 {@code mRootView} 实现了 {@link LifecycleOwner} 时, 此方法才会被调用 * 所以当您想在 {@link Service} 以及一些自定义 {@link View} 或自定义类中使用 {@code Presenter} 时 * 您也将不能继续使用 {@link OnLifecycleEvent} 绑定生命周期 * * @param owner link {@link SupportActivity} and {@link Fragment} */ @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) void onDestroy(LifecycleOwner owner) { /** * 注意, 如果在这里调用了 {@link #onDestroy()} 方法, 会出现某些地方引用 {@code mModel} 或 {@code mRootView} 为 null 的情况 * 比如在 {@link RxLifecycle} 终止 {@link Observable} 时, 在 {@link io.reactivex.Observable#doFinally(Action)} 中却引用了 {@code mRootView} 做一些释放资源的操作, 此时会空指针 * 或者如果你声明了多个 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) 时在其他 @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) * 中引用了 {@code mModel} 或 {@code mRootView} 也可能会出现此情况 */ owner.getLifecycle().removeObserver(this); }