Skip to content

Commit

Permalink
リファクタリング (#46)
Browse files Browse the repository at this point in the history
* ViewModelのonCleared()でCompositeDisposable#clear()を呼び出すように修正
* リスト系アイテムのレイアウトファイル名をitem_〇〇.xmlに変更
* Lint警告の解決
  • Loading branch information
jageishi committed Nov 3, 2019
1 parent c809707 commit 346181d
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import org.ageage.eggplant.R
import org.ageage.eggplant.common.model.Bookmark
import org.ageage.eggplant.databinding.BookmarkItemBinding
import org.ageage.eggplant.databinding.ItemBookmarkBinding
import java.text.SimpleDateFormat
import java.util.*

Expand All @@ -19,9 +19,9 @@ class BookmarksAdapter(

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookmarksHolder {
val binding =
DataBindingUtil.inflate<BookmarkItemBinding>(
DataBindingUtil.inflate<ItemBookmarkBinding>(
LayoutInflater.from(parent.context),
R.layout.bookmark_item,
R.layout.item_bookmark,
parent,
false
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.ageage.eggplant.bookmarks

import androidx.recyclerview.widget.RecyclerView
import org.ageage.eggplant.databinding.BookmarkItemBinding
import org.ageage.eggplant.databinding.ItemBookmarkBinding

class BookmarksHolder(val binding: BookmarkItemBinding) : RecyclerView.ViewHolder(binding.root)
class BookmarksHolder(val binding: ItemBookmarkBinding) : RecyclerView.ViewHolder(binding.root)
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.ageage.eggplant.bookmarks

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import org.ageage.eggplant.common.enums.SortType
import org.ageage.eggplant.common.model.Bookmark
import org.ageage.eggplant.common.repository.BookmarkRepository
Expand All @@ -14,6 +15,8 @@ class BookmarksViewModel(
private val schedulerProvider: BaseSchedulerProvider
) : ViewModel() {

private val disposable = CompositeDisposable()

private val _isLoading = MutableLiveData<Boolean>()
val isLoading: MutableLiveData<Boolean>
get() = _isLoading
Expand All @@ -24,7 +27,11 @@ class BookmarksViewModel(

private var isAlreadyLoaded = false

@SuppressLint("CheckResult")
override fun onCleared() {
super.onCleared()
disposable.clear()
}

fun loadBookmarks(url: String, sortType: SortType, forceLoad: Boolean = false) {
if (isAlreadyLoaded && !forceLoad) {
return
Expand Down Expand Up @@ -57,5 +64,6 @@ class BookmarksViewModel(
}, {
_isLoading.value = false
})
.addTo(disposable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,30 @@ class FeedRepository {
text = xpp.text ?: ""
}
XmlPullParser.END_TAG -> {
if (tagName == "item") {
item?.let { items.add(it) }
} else if (tagName == "title") {
item?.let { it.title = text }
} else if (tagName == "link") {
item?.let {
it.link = text
it.faviconUrl = "http://favicon.hatena.ne.jp/?url=$text"
item?.let {
when (tagName) {
"item" -> {
items.add(it)
}
"title" -> {
it.title = text
}
"link" -> {
it.link = text
it.faviconUrl = "http://favicon.hatena.ne.jp/?url=$text"
}
"description" -> {
it.description = text
}
"bookmarkcount" -> {
it.bookmarkCount = text
}
"imageurl" -> {
it.imageUrl = text
}
else -> {
}
}
} else if (tagName == "description") {
item?.let { it.description = text }
} else if (tagName == "bookmarkcount") {
item?.let { it.bookmarkCount = text }
} else if (tagName == "imageurl") {
item?.let { it.imageUrl = text }
}
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import org.ageage.eggplant.R
import org.ageage.eggplant.common.api.response.Item
import org.ageage.eggplant.common.ui.adapter.viewholder.FeedItemHolder
import org.ageage.eggplant.common.ui.adapter.viewholder.FeedLoadingItemHolder
import org.ageage.eggplant.databinding.FeedItemBinding
import org.ageage.eggplant.databinding.FeedLoadingItemBinding
import org.ageage.eggplant.databinding.ItemFeedBinding

class FeedItemAdapter(
private val context: Context,
Expand All @@ -29,9 +28,9 @@ class FeedItemAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
VIEW_TYPE_FEED -> {
val binding = DataBindingUtil.inflate<FeedItemBinding>(
val binding = DataBindingUtil.inflate<ItemFeedBinding>(
LayoutInflater.from(parent.context),
R.layout.feed_item,
R.layout.item_feed,
parent,
false
)
Expand Down Expand Up @@ -60,9 +59,9 @@ class FeedItemAdapter(

VIEW_TYPE_PROGRESS -> {
return FeedLoadingItemHolder(
DataBindingUtil.inflate<FeedLoadingItemBinding>(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.feed_loading_item,
R.layout.item_feed_loading,
parent,
false
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.ageage.eggplant.common.ui.adapter.viewholder

import androidx.recyclerview.widget.RecyclerView
import org.ageage.eggplant.databinding.FeedItemBinding
import org.ageage.eggplant.databinding.ItemFeedBinding

class FeedItemHolder(val binding: FeedItemBinding) : RecyclerView.ViewHolder(binding.root)
class FeedItemHolder(val binding: ItemFeedBinding) : RecyclerView.ViewHolder(binding.root)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.ageage.eggplant.common.ui.adapter.viewholder

import androidx.recyclerview.widget.RecyclerView
import org.ageage.eggplant.databinding.FeedLoadingItemBinding
import org.ageage.eggplant.databinding.ItemFeedLoadingBinding

class FeedLoadingItemHolder(
binding: FeedLoadingItemBinding
binding: ItemFeedLoadingBinding
) : RecyclerView.ViewHolder(binding.root)
12 changes: 10 additions & 2 deletions app/src/main/java/org/ageage/eggplant/feed/FeedItemsViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.ageage.eggplant.feed

import android.annotation.SuppressLint
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import org.ageage.eggplant.common.api.response.Item
import org.ageage.eggplant.common.enums.Category
import org.ageage.eggplant.common.enums.Mode
Expand All @@ -15,6 +16,8 @@ class FeedItemsViewModel(
private val schedulerProvider: BaseSchedulerProvider
) : ViewModel() {

private val disposable = CompositeDisposable()

private val _items = MutableLiveData<List<Item>>()
val items: LiveData<List<Item>>
get() = _items
Expand All @@ -25,7 +28,11 @@ class FeedItemsViewModel(

private var isAlreadyLoaded = false

@SuppressLint("CheckResult")
override fun onCleared() {
super.onCleared()
disposable.clear()
}

fun loadRss(mode: Mode, category: Category, forceLoad: Boolean = false) {
if (isAlreadyLoaded && !forceLoad) {
return
Expand All @@ -45,5 +52,6 @@ class FeedItemsViewModel(
_isLoading.postValue(false)
}
)
.addTo(disposable)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class SearchResultViewModel(
)

override fun onCleared() {
disposable.clear()
super.onCleared()
disposable.clear()
}

fun search(forceLoad: Boolean = false) {
Expand All @@ -69,7 +69,7 @@ class SearchResultViewModel(
.doOnSubscribe {
_status.postValue(Status.Loading)
}
.doOnSuccess() {
.doOnSuccess {
_status.postValue(Status.Success)
}
.doOnError {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/category_tab_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_selected="false">
<layer-list>
<item android:end="-2dp" android:start="-2dp" android:top="-2dp">
<item android:left="-2dp" android:right="-2dp" android:top="-2dp">
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="@color/gray" />
<gradient android:angle="270" android:endColor="@color/colorSurface" android:startColor="@color/gray" />
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_bookmarks.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<merge 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"
Expand Down Expand Up @@ -27,4 +27,4 @@

</androidx.viewpager.widget.ViewPager>

</FrameLayout>
</merge>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_bookmarks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/bookmark_item" />
tools:listitem="@layout/item_bookmark" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_feed_items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/feed_item" />
tools:listitem="@layout/item_feed" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_search_result.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/feed_item" />
tools:listitem="@layout/item_feed" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 346181d

Please sign in to comment.