Skip to content

Commit

Permalink
Adding Dagger to search
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu committed Oct 26, 2018
1 parent 7b502a5 commit 3f7ae5f
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 17 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/io/plaidapp/core/data/BaseDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public interface OnDataLoadedCallback<T> {
void onDataLoaded(T data);
}

void setOnDataLoadedCallback(OnDataLoadedCallback<T> onDataLoadedCallback) {
public void setOnDataLoadedCallback(OnDataLoadedCallback<T> onDataLoadedCallback) {
this.onDataLoadedCallback = onDataLoadedCallback;
}

final void onDataLoaded(T data) {
public final void onDataLoaded(T data) {
onDataLoadedCallback.onDataLoaded(data);
}

Expand Down
5 changes: 5 additions & 0 deletions search/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/

apply plugin: 'com.android.dynamic-feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion versions.compileSdk
Expand All @@ -39,4 +41,7 @@ repositories {

dependencies {
implementation project(':app')

kapt "com.google.dagger:dagger-compiler:${versions.dagger}"

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
*
*/

package io.plaidapp.core.data;
package io.plaidapp.search;

import android.content.Context;
import io.plaidapp.core.data.BaseDataManager;
import io.plaidapp.core.data.LoadSourceCallback;
import io.plaidapp.core.data.PlaidItem;
import io.plaidapp.core.data.Result;
import io.plaidapp.core.data.Source;
import io.plaidapp.core.designernews.Injection;
import io.plaidapp.core.designernews.domain.SearchStoriesUseCase;
import io.plaidapp.core.dribbble.data.ShotsRepository;
Expand All @@ -37,17 +42,20 @@ public class SearchDataManager extends BaseDataManager<List<? extends PlaidItem>
implements LoadSourceCallback {

private final SearchStoriesUseCase searchStoriesUseCase;
@Inject ShotsRepository shotsRepository;
private ShotsRepository shotsRepository;

// state
private String query = "";
private int page = 1;

@Inject public SearchDataManager(Context context,
OnDataLoadedCallback<List<? extends PlaidItem>> onDataLoadedCallback) {
@Inject public SearchDataManager(
Context context,
OnDataLoadedCallback<List<? extends PlaidItem>> onDataLoadedCallback,
ShotsRepository shotsRepository) {
super();
setOnDataLoadedCallback(onDataLoadedCallback);
searchStoriesUseCase = Injection.provideSearchStoriesUseCase(context);
this.shotsRepository = shotsRepository;
}

public void searchFor(String query) {
Expand Down
48 changes: 48 additions & 0 deletions search/src/main/java/io/plaidapp/search/dagger/Injector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.search.dagger

import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.core.data.BaseDataManager
import io.plaidapp.core.data.PlaidItem
import io.plaidapp.ui.PlaidApplication
import io.plaidapp.ui.search.SearchActivity

/**
* Injector for SearchActivity.
*
* TODO: Convert to extension function once [SearchActivity] is converted to Kotlin.
*/
object Injector {

@JvmStatic
fun inject(
activity: SearchActivity,
dataLoadedCallback: BaseDataManager.OnDataLoadedCallback<List<PlaidItem>>
) {
DaggerSearchComponent.builder()
.coreComponent(PlaidApplication.coreComponent(activity))
.dataManagerModule(DataManagerModule(activity))
.dataLoadedModule(OnDataLoadedModule(dataLoadedCallback))
.filterAdapterModule(FilterAdapterModule(activity))
.searchModule(SearchModule(activity))
.build()
.inject(activity)
}
}
44 changes: 44 additions & 0 deletions search/src/main/java/io/plaidapp/search/dagger/SearchComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.search.dagger

import dagger.Component
import io.plaidapp.core.dagger.CoreComponent
import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.ui.search.SearchActivity

/**
* Dagger component for the [SearchActivity].
*/
@Component(modules = [SearchModule::class], dependencies = [CoreComponent::class])
interface SearchComponent {

fun inject(activity: SearchActivity)

@Component.Builder
interface Builder {

fun build(): SearchComponent
fun coreComponent(module: CoreComponent): Builder
fun dataManagerModule(module: DataManagerModule): Builder
fun dataLoadedModule(module: OnDataLoadedModule): Builder
fun filterAdapterModule(module: FilterAdapterModule): Builder
fun searchModule(module: SearchModule): Builder
}
}
65 changes: 65 additions & 0 deletions search/src/main/java/io/plaidapp/search/dagger/SearchModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2018 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.plaidapp.search.dagger

import android.app.Activity
import android.content.Context
import com.bumptech.glide.util.ViewPreloadSizeProvider
import dagger.Module
import dagger.Provides
import io.plaidapp.R
import io.plaidapp.core.dagger.DataManagerModule
import io.plaidapp.core.dagger.FilterAdapterModule
import io.plaidapp.core.dagger.OnDataLoadedModule
import io.plaidapp.core.dagger.dribbble.DribbbleDataModule
import io.plaidapp.core.data.BaseDataManager
import io.plaidapp.core.data.PlaidItem
import io.plaidapp.core.data.pocket.PocketUtils
import io.plaidapp.core.dribbble.data.ShotsRepository
import io.plaidapp.core.dribbble.data.api.model.Shot
import io.plaidapp.search.SearchDataManager

@Module(
includes = [
DataManagerModule::class,
DribbbleDataModule::class,
FilterAdapterModule::class,
OnDataLoadedModule::class
]
)
class SearchModule(private val activity: Activity) {
@Provides
fun context(): Context = activity

@Provides
fun activity(): Activity = activity

@Provides
fun columns(): Int = activity.resources.getInteger(R.integer.num_columns)

@Provides
fun viewPreloadSizeProvider() = ViewPreloadSizeProvider<Shot>()

@Provides
fun isPocketInstalled() = PocketUtils.isPocketInstalled(activity)

@Provides
fun provideSearchDataManager(
onDataLoadedCallback: BaseDataManager.OnDataLoadedCallback<List<PlaidItem>>,
shotsRepository: ShotsRepository
): SearchDataManager = SearchDataManager(context(), onDataLoadedCallback, shotsRepository)
}
32 changes: 21 additions & 11 deletions search/src/main/java/io/plaidapp/ui/search/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import androidx.annotation.TransitionRes;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.InputType;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
Expand All @@ -42,11 +39,18 @@
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.inputmethod.EditorInfo;
import android.widget.*;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.TextView;
import androidx.annotation.TransitionRes;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.ViewPreloadSizeProvider;
import io.plaidapp.core.data.SearchDataManager;
import io.plaidapp.core.data.pocket.PocketUtils;
import io.plaidapp.search.SearchDataManager;
import io.plaidapp.core.dribbble.data.api.model.Shot;
import io.plaidapp.core.ui.FeedAdapter;
import io.plaidapp.core.ui.recyclerview.InfiniteScrollListener;
Expand All @@ -56,8 +60,10 @@
import io.plaidapp.core.util.ShortcutHelper;
import io.plaidapp.core.util.TransitionUtils;
import io.plaidapp.search.R;
import io.plaidapp.search.dagger.Injector;
import io.plaidapp.ui.search.transitions.CircularReveal;

import javax.inject.Inject;
import java.util.List;

public class SearchActivity extends Activity {
Expand All @@ -80,20 +86,24 @@ public class SearchActivity extends Activity {
private View resultsScrim;
private int columns;
private float appBarElevation;
SearchDataManager dataManager;
FeedAdapter adapter;
private TextView noResults;
private SparseArray<Transition> transitions = new SparseArray<>();
private boolean focusQuery = true;

@Inject
SearchDataManager dataManager;

@Inject
FeedAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
bindResources();
setupSearchView();

dataManager = new SearchDataManager(this, data -> {
Injector.inject(this, data -> {
if (data != null && data.size() > 0) {
if (results.getVisibility() != View.VISIBLE) {
TransitionManager.beginDelayedTransition(container,
Expand All @@ -110,9 +120,9 @@ protected void onCreate(Bundle savedInstanceState) {
setNoResultsVisibility(View.VISIBLE);
}
});

ViewPreloadSizeProvider<Shot> shotPreloadSizeProvider = new ViewPreloadSizeProvider<>();
adapter = new FeedAdapter(this, dataManager, columns, PocketUtils.isPocketInstalled(this),
shotPreloadSizeProvider);

setExitSharedElementCallback(FeedAdapter.createSharedElementReenterCallback(this));
results.setAdapter(adapter);
results.setItemAnimator(new SlideInItemAnimator());
Expand Down

0 comments on commit 3f7ae5f

Please sign in to comment.