-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
218 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
app/src/main/kotlin/com/markodevcic/newsreader/search/SearchActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.markodevcic.newsreader.search | ||
|
||
import android.app.SearchManager | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.support.v7.app.AppCompatActivity | ||
import android.support.v7.widget.DividerItemDecoration | ||
import android.support.v7.widget.LinearLayoutManager | ||
import android.support.v7.widget.RecyclerView | ||
import android.view.View | ||
import com.markodevcic.newsreader.R | ||
import com.markodevcic.newsreader.articles.ArticlesViewHolder | ||
import com.markodevcic.newsreader.injection.Injector | ||
import com.squareup.picasso.Picasso | ||
import kotlinx.android.synthetic.main.activity_search.* | ||
import kotlinx.android.synthetic.main.content_main.* | ||
import kotlinx.coroutines.experimental.Job | ||
import kotlinx.coroutines.experimental.android.UI | ||
import kotlinx.coroutines.experimental.launch | ||
import javax.inject.Inject | ||
|
||
class SearchActivity : AppCompatActivity() { | ||
|
||
private val job = Job() | ||
|
||
@Inject | ||
lateinit var presenter: SearchPresenter | ||
private var adapter: SearchAdapter? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_search) | ||
|
||
Injector.appComponent.inject(this) | ||
|
||
setSupportActionBar(toolbar) | ||
supportActionBar?.setHomeButtonEnabled(true) | ||
|
||
setupArticlesView() | ||
|
||
if (Intent.ACTION_SEARCH == intent.action) { | ||
val query = intent.getStringExtra(SearchManager.QUERY) | ||
supportActionBar?.title = query.capitalize() | ||
launch(UI + job) { | ||
progressBar.visibility = View.VISIBLE | ||
val articles = presenter.search(query) | ||
adapter = SearchAdapter(articles) | ||
articlesView.adapter = adapter | ||
progressBar.visibility = View.GONE | ||
} | ||
} else { | ||
throw IllegalStateException("this activity should be called from search view") | ||
} | ||
} | ||
|
||
private fun setupArticlesView() { | ||
articlesView.setHasFixedSize(true) | ||
articlesView.isDrawingCacheEnabled = true | ||
articlesView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL)) | ||
articlesView.layoutManager = LinearLayoutManager(this@SearchActivity) | ||
articlesView.addOnScrollListener(object : RecyclerView.OnScrollListener() { | ||
override fun onScrollStateChanged(recyclerView: RecyclerView?, scrollState: Int) { | ||
super.onScrollStateChanged(recyclerView, scrollState) | ||
val picasso = Picasso.with(this@SearchActivity.applicationContext) | ||
if (scrollState == RecyclerView.SCROLL_STATE_IDLE) { | ||
picasso.resumeTag(ArticlesViewHolder) | ||
} else { | ||
picasso.pauseTag(ArticlesViewHolder) | ||
} | ||
} | ||
}) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/kotlin/com/markodevcic/newsreader/search/SearchAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.markodevcic.newsreader.search | ||
|
||
import android.support.v7.widget.RecyclerView | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import com.markodevcic.newsreader.R | ||
import com.markodevcic.newsreader.articles.ArticlesViewHolder | ||
import com.markodevcic.newsreader.data.Article | ||
|
||
class SearchAdapter(private val articles: List<Article>) : RecyclerView.Adapter<ArticlesViewHolder>() { | ||
|
||
init { | ||
setHasStableIds(true) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticlesViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
val view = inflater.inflate(R.layout.item_article, parent, false) | ||
return ArticlesViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ArticlesViewHolder, position: Int) { | ||
holder.bind(articles[position]) | ||
} | ||
|
||
override fun getItemCount(): Int = articles.size | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/kotlin/com/markodevcic/newsreader/search/SearchPresenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.markodevcic.newsreader.search | ||
|
||
import com.markodevcic.newsreader.data.Article | ||
import com.markodevcic.newsreader.sync.SyncUseCase | ||
import javax.inject.Inject | ||
|
||
class SearchPresenter @Inject constructor(private val syncUseCase: SyncUseCase) { | ||
|
||
suspend fun search(query: String): List<Article> = syncUseCase.search(query) | ||
} |
2 changes: 2 additions & 0 deletions
2
app/src/main/kotlin/com/markodevcic/newsreader/sync/SyncUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package com.markodevcic.newsreader.sync | ||
|
||
import com.markodevcic.newsreader.data.Article | ||
import com.markodevcic.newsreader.data.Source | ||
|
||
interface SyncUseCase { | ||
suspend fun downloadSourcesAsync(categories: Collection<String>) | ||
suspend fun downloadArticlesAsync(source: Source): Int | ||
suspend fun search(query: String): List<Article> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.design.widget.CoordinatorLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="com.markodevcic.newsreader.search.SearchActivity"> | ||
|
||
<android.support.design.widget.AppBarLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:theme="@style/AppTheme.AppBarOverlay"> | ||
|
||
<android.support.v7.widget.Toolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
android:background="?attr/colorPrimary" | ||
app:popupTheme="@style/AppTheme.PopupOverlay"> | ||
|
||
</android.support.v7.widget.Toolbar> | ||
|
||
</android.support.design.widget.AppBarLayout> | ||
|
||
<include layout="@layout/content_main"/> | ||
|
||
<ProgressBar | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/progressBar" | ||
android:layout_gravity="center"/> | ||
</android.support.design.widget.CoordinatorLayout> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<searchable xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:label="@string/search_label" | ||
android:hint="@string/search_label" | ||
android:searchSuggestSelection=" ?" | ||
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" | ||
android:voiceLanguage="en" | ||
|
||
> | ||
</searchable> |