Skip to content

Commit

Permalink
Merge pull request #1028 from toastkidjp/refactor/#605_clean_20230612
Browse files Browse the repository at this point in the history
Refactor/#605 clean 20230612
  • Loading branch information
toastkidjp committed Jun 24, 2023
2 parents 707df17 + aad8f96 commit 4c1fffc
Show file tree
Hide file tree
Showing 32 changed files with 313 additions and 352 deletions.
3 changes: 1 addition & 2 deletions about/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
* which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
*/
import com.android.build.gradle.tasks.GenerateBuildConfig
import property.BuildTool
import property.LibraryVersion
import com.android.build.gradle.tasks.GenerateBuildConfig

plugins {
id("com.android.library")
id("kotlin-android")
id("org.jetbrains.kotlin.kapt")
id("jacoco.definition")
}

Expand Down
3 changes: 2 additions & 1 deletion api/src/test/java/jp/toastkid/api/rss/model/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.io.FileNotFoundException
import java.io.InputStream

/**
Expand Down Expand Up @@ -94,5 +95,5 @@ class ParserTest {

private fun readStream(filePath: String): InputStream =
javaClass.classLoader?.getResourceAsStream(filePath)
?: throw RuntimeException()
?: throw FileNotFoundException("Resource $filePath is cannot found.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class WikipediaApiTest {
@InjectMockKs
private lateinit var wikipediaApi: WikipediaApi

@MockK
private lateinit var urlDecider: UrlDecider
private val baseUrl = "https://en.wikipedia.org/"

@MockK
private lateinit var requestBuilder: Retrofit.Builder
Expand All @@ -53,8 +52,6 @@ class WikipediaApiTest {
fun setUp() {
MockKAnnotations.init(this)

every { urlDecider.invoke() }.returns("https://en.wikipedia.org/")

mockkConstructor(Retrofit.Builder::class)
every { anyConstructed<Retrofit.Builder>().baseUrl(any<String>()) }.returns(requestBuilder)
every { requestBuilder.addConverterFactory(any()) }.returns(requestBuilder)
Expand All @@ -77,7 +74,6 @@ class WikipediaApiTest {
fun testInvoke() {
wikipediaApi.invoke()

verify(exactly = 1) { urlDecider.invoke() }
verify(exactly = 1) { anyConstructed<Retrofit.Builder>().baseUrl(any<String>()) }
verify(exactly = 1) { requestBuilder.addConverterFactory(any()) }
verify(exactly = 1) { requestBuilder.build() }
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${BuildTool.kotlinVersion}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:${LibraryVersion.coroutines}")
implementation("com.jakewharton.timber:timber:${LibraryVersion.timber}")
implementation("com.squareup.okhttp3:okhttp:${LibraryVersion.okhttp}")
implementation("com.squareup.retrofit2:retrofit:2.6.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")
implementation("org.jsoup:jsoup:${LibraryVersion.jsoup}")

implementation("org.burnoutcrew.composereorderable:reorderable:0.9.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class WebClipIconLoader(
?.firstOrNull()
?: return

downloadApi.invoke(webClipUrl.toString(), {
downloadApi.invoke(webClipUrl.toString()) {
it?.use { stream ->
val bitmap = BitmapFactory.decodeStream(stream)
val longer = if (bitmap.width > bitmap.height) bitmap.width else bitmap.height
val sampling = 128.0 / longer.toDouble()
bitmapCompressor
.invoke(bitmapScaling.invoke(bitmap, sampling, sampling), file)
}
})
}
}

private fun fetchDocument(url: URL) = try {
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/jp/toastkid/yobidashi/main/ui/AppBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import androidx.compose.foundation.layout.width
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -51,7 +50,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.roundToInt

@OptIn(ExperimentalMaterial3Api::class)
@Composable
internal fun AppBar() {
val activity = LocalContext.current as? ComponentActivity ?: return
Expand Down Expand Up @@ -156,7 +154,6 @@ private fun OverflowMenu(
) {
val openOptionMenu = remember { mutableStateOf(false) }
val context = LocalContext.current
val preferenceApplier = PreferenceApplier(context)
val contentViewModel = (context as? ViewModelStoreOwner)?.let {
viewModel(ContentViewModel::class.java, context)
}
Expand All @@ -174,7 +171,7 @@ private fun OverflowMenu(
OptionMenu(
titleId = R.string.reset_button_position,
action = {
preferenceApplier.clearMenuFabPosition()
PreferenceApplier(context).clearMenuFabPosition()
contentViewModel?.resetMenuFabPosition()
}),
OptionMenu(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import android.content.Context
import android.util.LruCache
import jp.toastkid.api.suggestion.SuggestionApi
import jp.toastkid.data.repository.factory.RepositoryFactory
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.yobidashi.libs.network.NetworkChecker
import jp.toastkid.yobidashi.browser.UrlItem
import jp.toastkid.yobidashi.browser.bookmark.model.BookmarkRepository
import jp.toastkid.yobidashi.browser.history.ViewHistoryRepository
import jp.toastkid.yobidashi.search.favorite.FavoriteSearchRepository
import jp.toastkid.yobidashi.search.history.SearchHistoryRepository
import jp.toastkid.yobidashi.search.url_suggestion.UrlItemQueryUseCase
import jp.toastkid.yobidashi.search.viewmodel.SearchUiViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
Expand All @@ -27,27 +27,29 @@ import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class QueryingUseCase(
private val searchUiViewModel: SearchUiViewModel,
private val preferenceApplier: PreferenceApplier,
private val urlItemQueryUseCase: UrlItemQueryUseCase,
private val bookmarkRepository: BookmarkRepository,
private val viewHistoryRepository: ViewHistoryRepository,
private val favoriteSearchRepository: FavoriteSearchRepository,
private val searchHistoryRepository: SearchHistoryRepository,
private val contextSupplier: () -> Context,
private val suggestionApi: SuggestionApi = SuggestionApi(),
private val channel: Channel<String> = Channel(),
private val cache: LruCache<String, List<String>> = LruCache<String, List<String>>(30),
private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.Default
private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.Default,
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
) {

private var disposables = Job()

private fun invoke(keyword: String) {
if (preferenceApplier.isEnableSearchHistory) {
CoroutineScope(Dispatchers.IO).launch {
searchUiViewModel.searchHistories.clear()
searchUiViewModel.searchHistories.addAll(
val viewModel = viewModel ?: return

if (viewModel.isEnableSearchHistory()) {
CoroutineScope(Dispatchers.IO).launch(disposables) {
viewModel.searchHistories.clear()
viewModel.searchHistories.addAll(
if (keyword.isBlank()) {
searchHistoryRepository.find(5)
} else {
Expand All @@ -57,10 +59,10 @@ class QueryingUseCase(
}
}

if (preferenceApplier.isEnableFavoriteSearch) {
CoroutineScope(backgroundDispatcher).launch {
searchUiViewModel.favoriteSearchItems.clear()
searchUiViewModel.favoriteSearchItems.addAll(
if (viewModel.isEnableFavoriteSearch()) {
CoroutineScope(backgroundDispatcher).launch(disposables) {
viewModel.favoriteSearchItems.clear()
viewModel.favoriteSearchItems.addAll(
if (keyword.isBlank()) {
favoriteSearchRepository.find(5)
} else {
Expand All @@ -70,33 +72,41 @@ class QueryingUseCase(
}
}

if (preferenceApplier.isEnableViewHistory) {
urlItemQueryUseCase.invoke(keyword)
}
if (viewModel.isEnableViewHistory()) {
CoroutineScope(backgroundDispatcher).launch {
val newItems = mutableListOf<UrlItem>()

if (preferenceApplier.isEnableSuggestion) {
querySuggestions(keyword)
}
}
withContext(ioDispatcher) {
if (keyword.isBlank()) {
return@withContext
}
bookmarkRepository.search("%$keyword%", ITEM_LIMIT).forEach { newItems.add(it) }
}

private fun querySuggestions(keyword: String) {
if (cache.snapshot().containsKey(keyword)) {
searchUiViewModel.suggestions.clear()
searchUiViewModel.suggestions.addAll(cache.get(keyword))
return
}
withContext(ioDispatcher) {
viewHistoryRepository.search("%$keyword%", ITEM_LIMIT).forEach { newItems.add(it) }
}

if (cannotUseNetwork(contextSupplier())) {
return
viewModel.urlItems.clear()
viewModel.urlItems.addAll(newItems)
}
}

suggestionApi.fetchAsync(keyword) { suggestions ->
searchUiViewModel.suggestions.clear()
if (suggestions.isEmpty()) {
return@fetchAsync
if (viewModel.isEnableSuggestion()) {
if (cache.snapshot().containsKey(keyword)) {
viewModel.suggestions.clear()
viewModel.suggestions.addAll(cache.get(keyword))
return
}

suggestionApi.fetchAsync(keyword) { suggestions ->
viewModel.suggestions.clear()
if (suggestions.isEmpty()) {
return@fetchAsync
}
cache.put(keyword, suggestions)
viewModel.suggestions.addAll(suggestions)
}
cache.put(keyword, suggestions)
searchUiViewModel.suggestions.addAll(suggestions)
}
}

Expand All @@ -120,28 +130,25 @@ class QueryingUseCase(
channel.close()
}

private fun cannotUseNetwork(context: Context) =
NetworkChecker().isNotAvailable(context) ||
(PreferenceApplier(context).wifiOnly && NetworkChecker().isUnavailableWiFi(context))
private var viewModel: SearchUiViewModel? = null

fun setViewModel(searchUiViewModel: SearchUiViewModel) {
viewModel = searchUiViewModel
}

companion object {
fun make(viewModel: SearchUiViewModel, context: Context): QueryingUseCase {
// TODO extract
/**
* Item limit.
*/
private const val ITEM_LIMIT = 3

fun make(context: Context): QueryingUseCase {
val repositoryFactory = RepositoryFactory()
return QueryingUseCase(
viewModel,
PreferenceApplier(context),
UrlItemQueryUseCase(
{
viewModel.urlItems.clear()
viewModel.urlItems.addAll(it)
},
RepositoryFactory().bookmarkRepository(context),
RepositoryFactory().viewHistoryRepository(context),
{ }
),
RepositoryFactory().favoriteSearchRepository(context),
RepositoryFactory().searchHistoryRepository(context),
{ context }
repositoryFactory.bookmarkRepository(context),
repositoryFactory.viewHistoryRepository(context),
repositoryFactory.favoriteSearchRepository(context),
repositoryFactory.searchHistoryRepository(context)
)
}

Expand Down

0 comments on commit 4c1fffc

Please sign in to comment.