Skip to content

Commit

Permalink
Added handler injection as dagger2 issue artem-zinnatullin#214 has be…
Browse files Browse the repository at this point in the history
…en fixed.
  • Loading branch information
zsavely committed May 18, 2016
1 parent 3910762 commit 00ea9fa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;

import com.artemzin.qualitymatters.ApplicationModule;
import com.artemzin.qualitymatters.QualityMattersApp;

import javax.inject.Inject;
import javax.inject.Named;

@SuppressWarnings("PMD.AbstractClassWithoutAnyMethod")
public abstract class BaseFragment extends Fragment {

// Due to bug (https://github.com/google/dagger/issues/214) in Dagger 2 we can not inject handler here, sorry.
@NonNull
private static final Handler MAIN_THREAD_HANDLER = new Handler(Looper.getMainLooper());
@Inject
@Named(ApplicationModule.MAIN_THREAD_HANDLER)
Handler mainThreadHandler;

protected void runOnUiThreadIfFragmentAlive(@NonNull Runnable runnable) {
if (Looper.myLooper() == Looper.getMainLooper() && isFragmentAlive()) {
runnable.run();
} else {
MAIN_THREAD_HANDLER.post(() -> {
mainThreadHandler.post(() -> {
if (isFragmentAlive()) {
runnable.run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.artemzin.qualitymatters.ui.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
Expand All @@ -10,7 +9,6 @@
import android.view.View;
import android.view.ViewGroup;

import com.artemzin.qualitymatters.ApplicationModule;
import com.artemzin.qualitymatters.QualityMattersApp;
import com.artemzin.qualitymatters.R;
import com.artemzin.qualitymatters.api.entities.Item;
Expand All @@ -28,7 +26,6 @@
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
Expand All @@ -55,10 +52,6 @@ public class ItemsFragment extends BaseFragment implements ItemsView {

ItemsAdapter itemsAdapter;

@Inject
@Named(ApplicationModule.MAIN_THREAD_HANDLER)
Handler mainThreadHandler;

@Inject
ItemsPresenter itemsPresenter;

Expand Down

0 comments on commit 00ea9fa

Please sign in to comment.