Skip to content

Commit

Permalink
タブが正常に更新されない問題を修正した
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Apr 29, 2020
1 parent 2569a1c commit 9173f45
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class MiApplication : Application(), MiCore {
.addMigrations(MIGRATION_33_34)
.addMigrations(MIGRATION_34_35)
.addMigrations(MIGRATION_35_36)
.addMigrations(MIGRATION_36_37)
.build()
//connectionInstanceDao = database.connectionInstanceDao()
mAccountDao = database.accountDao()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import jp.panta.misskeyandroidclient.model.notes.NoteRequest
import jp.panta.misskeyandroidclient.model.notes.NoteRequestSettingDao
import jp.panta.misskeyandroidclient.model.notes.reaction.*

@Database(entities = [EncryptedConnectionInformation::class, NoteRequest.Setting::class, ReactionHistory::class, Account::class, ReactionUserSetting::class], version = 36)
@Database(entities = [EncryptedConnectionInformation::class, NoteRequest.Setting::class, ReactionHistory::class, Account::class, ReactionUserSetting::class], version = 37)
@TypeConverters(NoteRequest.NoteTypeConverter::class, DateConverter::class)
abstract class DataBase : RoomDatabase(){
//abstract fun connectionInstanceDao(): ConnectionInstanceDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ val MIGRATION_35_36 = object : Migration(35, 36){
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("create table 'reaction_user_setting'('reaction' TEXT not null, 'instance_domain' TEXT not null, 'weight' INTEGER not null, primary key('reaction', 'instance_domain'))")
}
}

val MIGRATION_36_37 = object : Migration(36, 37){
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("alter table setting add weight INTEGER")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ data class NoteRequest(
val noteId: String? = null,
var accountId: String? = null,
var antennaId: String? = null,
var listId: String? = null
var listId: String? = null,
var weight: Int? = null
): Serializable{
@PrimaryKey(autoGenerate = true)
var id: Long? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import jp.panta.misskeyandroidclient.KeyStore
import jp.panta.misskeyandroidclient.MiApplication
import jp.panta.misskeyandroidclient.R
Expand Down Expand Up @@ -57,13 +61,19 @@ class TabFragment : Fragment(), ScrollableTop{
}

if(mPagerAdapter == null){
mPagerAdapter = TimelinePagerAdapter(activity?.supportFragmentManager, emptyList())
mPagerAdapter = TimelinePagerAdapter(this, emptyList())
viewPager.adapter = mPagerAdapter
}
mPagerAdapter?.requestBaseList = settings
mPagerAdapter?.notifyDataSetChanged()
tabLayout.setupWithViewPager(viewPager)
//mPagerAdapter = mPagerAdapter?:
mPagerAdapter?.setList(settings.sortedBy {
it.weight
})
//mPagerAdapter?.notifyDataSetChanged()

val mediator = TabLayoutMediator(tabLayout, viewPager){ tab: TabLayout.Tab, position: Int ->
tab.text = mPagerAdapter?.getPageTitle(position)
}
mediator.attach()


if(settings.size <= 1){
tabLayout.visibility = View.GONE
Expand All @@ -82,17 +92,16 @@ class TabFragment : Fragment(), ScrollableTop{
}
}

class TimelinePagerAdapter(supportFragmentManager: FragmentManager?, var requestBaseList: List<NoteRequest.Setting>) : FragmentPagerAdapter(supportFragmentManager!!, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
class TimelinePagerAdapter(fragment: Fragment, list: List<NoteRequest.Setting>) : FragmentStateAdapter(fragment){
var requestBaseList: List<NoteRequest.Setting> = list
private set
private var oldRequestBaseSetting = requestBaseList

val scrollableTopFragments = ArrayList<ScrollableTop>()

override fun getCount(): Int {
return requestBaseList.size
}

override fun getItem(p0: Int): Fragment {
Log.d("getItem", "$p0, ${requestBaseList[p0].type}")
val item = requestBaseList[p0]
override fun createFragment(position: Int): Fragment {
Log.d("getItem", "$position, ${requestBaseList[position].type}")
val item = requestBaseList[position]
val noteId = item.noteId
val fragment = if(item.type == NoteType.DETAIL && noteId != null){
NoteDetailFragment.newInstance(noteId)
Expand All @@ -105,11 +114,46 @@ class TabFragment : Fragment(), ScrollableTop{
return fragment
}

override fun getPageTitle(position: Int): CharSequence? {
fun getPageTitle(position: Int): String{
val requestBase = requestBaseList[position]
return requestBase.title
}

override fun getItemCount(): Int = requestBaseList.size

private val diffCallback = object : DiffUtil.Callback(){
override fun areContentsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
return oldRequestBaseSetting[oldItemPosition] == requestBaseList[newItemPosition]
}

override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldRequestBaseSetting[oldItemPosition].id == requestBaseList[newItemPosition].id
}

override fun getNewListSize(): Int {
return requestBaseList.size
}

override fun getOldListSize(): Int {
return oldRequestBaseSetting.size
}
}

fun setList(list: List<NoteRequest.Setting>){
oldRequestBaseSetting = requestBaseList
requestBaseList = list

val result = DiffUtil.calculateDiff(diffCallback)
result.dispatchUpdatesTo(this)
}

override fun getItemId(position: Int): Long {
return requestBaseList[position].id?: super.getItemId(position)
}

}

override fun showTop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class TabSettingFragment : Fragment(){
val list = if(ar.pages.isNullOrEmpty()){
defaultTabVisibleSettings()
}else{
ar.pages.map{nrt ->
ar.pages.sortedBy {
it.weight
}.map{nrt ->
SettingTab.FromSetting(nrt)
}
}
Expand Down Expand Up @@ -144,14 +146,12 @@ class TabSettingFragment : Fragment(){
it.toSetting()
} ?: return@launch

selectedList.forEach {
it.id = null

selectedList.forEachIndexed { index, setting ->
setting.weight = index
}

/*for(n in selectedList.indices){
selectedList[n].id = n.toLong()
}*/
//dao.insertAll(selectedList)

miApplication?.replaceAllPagesInCurrentAccount(selectedList)
Log.d("TabSettingFragment", "設定完了")
}
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/layout/fragment_tab.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.tabs.TabLayout
Expand All @@ -27,7 +28,7 @@
android:elevation="4dp"
android:visibility="gone"
/>
<androidx.viewpager.widget.ViewPager
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintBottom_toBottomOf="parent"
Expand Down

0 comments on commit 9173f45

Please sign in to comment.