mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2025-01-31 13:48:20 +01:00
[Messages] Implement saving list position on message open.
This commit is contained in:
parent
c214b48409
commit
0413dbffa2
@ -20,7 +20,6 @@ import androidx.appcompat.widget.PopupMenu
|
|||||||
import androidx.core.graphics.ColorUtils
|
import androidx.core.graphics.ColorUtils
|
||||||
import androidx.lifecycle.Observer
|
import androidx.lifecycle.Observer
|
||||||
import androidx.navigation.NavOptions
|
import androidx.navigation.NavOptions
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
|
||||||
import com.danimahardhika.cafebar.CafeBar
|
import com.danimahardhika.cafebar.CafeBar
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.mikepenz.iconics.IconicsColor
|
import com.mikepenz.iconics.IconicsColor
|
||||||
@ -69,7 +68,6 @@ import pl.szczodrzynski.edziennik.ui.modules.homework.HomeworkFragment
|
|||||||
import pl.szczodrzynski.edziennik.ui.modules.login.LoginActivity
|
import pl.szczodrzynski.edziennik.ui.modules.login.LoginActivity
|
||||||
import pl.szczodrzynski.edziennik.ui.modules.messages.MessageFragment
|
import pl.szczodrzynski.edziennik.ui.modules.messages.MessageFragment
|
||||||
import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesFragment
|
import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesFragment
|
||||||
import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesListFragmentOld
|
|
||||||
import pl.szczodrzynski.edziennik.ui.modules.messages.compose.MessagesComposeFragment
|
import pl.szczodrzynski.edziennik.ui.modules.messages.compose.MessagesComposeFragment
|
||||||
import pl.szczodrzynski.edziennik.ui.modules.notifications.NotificationsListFragment
|
import pl.szczodrzynski.edziennik.ui.modules.notifications.NotificationsListFragment
|
||||||
import pl.szczodrzynski.edziennik.ui.modules.settings.ProfileManagerFragment
|
import pl.szczodrzynski.edziennik.ui.modules.settings.ProfileManagerFragment
|
||||||
@ -889,9 +887,6 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
|
|||||||
}
|
}
|
||||||
app.profileLoad(id) {
|
app.profileLoad(id) {
|
||||||
MessagesFragment.pageSelection = -1
|
MessagesFragment.pageSelection = -1
|
||||||
MessagesListFragmentOld.tapPositions = intArrayOf(RecyclerView.NO_POSITION, RecyclerView.NO_POSITION)
|
|
||||||
MessagesListFragmentOld.topPositions = intArrayOf(RecyclerView.NO_POSITION, RecyclerView.NO_POSITION)
|
|
||||||
MessagesListFragmentOld.bottomPositions = intArrayOf(RecyclerView.NO_POSITION, RecyclerView.NO_POSITION)
|
|
||||||
|
|
||||||
setDrawerItems()
|
setDrawerItems()
|
||||||
// the drawer profile is updated automatically when the drawer item is clicked
|
// the drawer profile is updated automatically when the drawer item is clicked
|
||||||
@ -916,9 +911,10 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
|
|||||||
loadTarget(target, arguments)
|
loadTarget(target, arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private fun loadTarget(target: NavTarget, arguments: Bundle? = null) {
|
private fun loadTarget(target: NavTarget, args: Bundle? = null) {
|
||||||
d("NavDebug", "loadTarget(target = $target, arguments = $arguments)")
|
d("NavDebug", "loadTarget(target = $target, args = $args)")
|
||||||
|
|
||||||
|
val arguments = args ?: navBackStack.firstOrNull { it.first.id == target.id }?.second ?: Bundle()
|
||||||
bottomSheet.close()
|
bottomSheet.close()
|
||||||
bottomSheet.removeAllContextual()
|
bottomSheet.removeAllContextual()
|
||||||
bottomSheet.toggleGroupEnabled = false
|
bottomSheet.toggleGroupEnabled = false
|
||||||
@ -966,6 +962,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
|
|||||||
navBackStack.removeAt(navBackStack.lastIndex)
|
navBackStack.removeAt(navBackStack.lastIndex)
|
||||||
}
|
}
|
||||||
navTarget = target
|
navTarget = target
|
||||||
|
navArguments = arguments
|
||||||
|
|
||||||
return@let null
|
return@let null
|
||||||
}?.let {
|
}?.let {
|
||||||
@ -975,7 +972,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
|
|||||||
R.anim.task_open_enter,
|
R.anim.task_open_enter,
|
||||||
R.anim.task_open_exit
|
R.anim.task_open_exit
|
||||||
)
|
)
|
||||||
navBackStack.add(navTarget to arguments)
|
navBackStack.add(navTarget to navArguments)
|
||||||
navTarget = target
|
navTarget = target
|
||||||
navArguments = arguments
|
navArguments = arguments
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package pl.szczodrzynski.edziennik.data.db.dao
|
package pl.szczodrzynski.edziennik.data.db.dao
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.room.Dao
|
import androidx.room.*
|
||||||
import androidx.room.Insert
|
import androidx.sqlite.db.SupportSQLiteQuery
|
||||||
import androidx.room.OnConflictStrategy
|
|
||||||
import androidx.room.Query
|
|
||||||
import pl.szczodrzynski.edziennik.data.db.entity.Teacher
|
import pl.szczodrzynski.edziennik.data.db.entity.Teacher
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
@ -18,6 +16,9 @@ interface TeacherDao {
|
|||||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||||
fun addAllIgnore(teacherList: List<Teacher>)
|
fun addAllIgnore(teacherList: List<Teacher>)
|
||||||
|
|
||||||
|
@RawQuery
|
||||||
|
fun query(query: SupportSQLiteQuery): Int
|
||||||
|
|
||||||
@Query("DELETE FROM teachers WHERE profileId = :profileId")
|
@Query("DELETE FROM teachers WHERE profileId = :profileId")
|
||||||
fun clear(profileId: Int)
|
fun clear(profileId: Int)
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
|||||||
class FragmentLazyPagerAdapter(
|
class FragmentLazyPagerAdapter(
|
||||||
fragmentManager: FragmentManager,
|
fragmentManager: FragmentManager,
|
||||||
swipeRefreshLayout: SwipeRefreshLayout,
|
swipeRefreshLayout: SwipeRefreshLayout,
|
||||||
private val fragments: List<Pair<LazyFragment, CharSequence>>
|
val fragments: List<Pair<LazyFragment, CharSequence>>
|
||||||
) : LazyPagerAdapter(fragmentManager, swipeRefreshLayout) {
|
) : LazyPagerAdapter(fragmentManager, swipeRefreshLayout) {
|
||||||
override fun getPage(position: Int) = fragments[position].first
|
override fun getPage(position: Int) = fragments[position].first
|
||||||
override fun getPageTitle(position: Int) = fragments[position].second
|
override fun getPageTitle(position: Int) = fragments[position].second
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
package pl.szczodrzynski.edziennik.ui.modules.base.lazypager
|
package pl.szczodrzynski.edziennik.ui.modules.base.lazypager
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
|
||||||
@ -11,6 +12,7 @@ abstract class LazyFragment : Fragment() {
|
|||||||
private var isPageCreated = false
|
private var isPageCreated = false
|
||||||
internal var position = -1
|
internal var position = -1
|
||||||
internal var swipeRefreshLayoutCallback: ((position: Int, isEnabled: Boolean) -> Unit)? = null
|
internal var swipeRefreshLayoutCallback: ((position: Int, isEnabled: Boolean) -> Unit)? = null
|
||||||
|
internal var onPageDestroy: ((position: Int, outState: Bundle?) -> Unit?)? = null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the page is first shown, or if previous
|
* Called when the page is first shown, or if previous
|
||||||
|
@ -9,6 +9,7 @@ import android.view.LayoutInflater
|
|||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
import androidx.sqlite.db.SimpleSQLiteQuery
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
@ -55,5 +56,9 @@ class LabFragment : Fragment(), CoroutineScope {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b.rodo.onClick {
|
||||||
|
app.db.teacherDao().query(SimpleSQLiteQuery("UPDATE teachers SET teacherSurname = \"\" WHERE profileId = ${App.profileId}"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,20 +52,34 @@ class MessagesFragment : Fragment(), CoroutineScope {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val args = arguments
|
||||||
|
|
||||||
val pagerAdapter = FragmentLazyPagerAdapter(
|
val pagerAdapter = FragmentLazyPagerAdapter(
|
||||||
fragmentManager ?: return,
|
fragmentManager ?: return,
|
||||||
b.refreshLayout,
|
b.refreshLayout,
|
||||||
listOf(
|
listOf(
|
||||||
MessagesListFragment().apply {
|
MessagesListFragment().apply {
|
||||||
|
onPageDestroy = this@MessagesFragment.onPageDestroy
|
||||||
arguments = Bundle("messageType" to Message.TYPE_RECEIVED)
|
arguments = Bundle("messageType" to Message.TYPE_RECEIVED)
|
||||||
|
args?.getBundle("page0")?.let {
|
||||||
|
arguments?.putAll(it)
|
||||||
|
}
|
||||||
} to getString(R.string.messages_tab_received),
|
} to getString(R.string.messages_tab_received),
|
||||||
|
|
||||||
MessagesListFragment().apply {
|
MessagesListFragment().apply {
|
||||||
|
onPageDestroy = this@MessagesFragment.onPageDestroy
|
||||||
arguments = Bundle("messageType" to Message.TYPE_SENT)
|
arguments = Bundle("messageType" to Message.TYPE_SENT)
|
||||||
|
args?.getBundle("page1")?.let {
|
||||||
|
arguments?.putAll(it)
|
||||||
|
}
|
||||||
} to getString(R.string.messages_tab_sent),
|
} to getString(R.string.messages_tab_sent),
|
||||||
|
|
||||||
MessagesListFragment().apply {
|
MessagesListFragment().apply {
|
||||||
|
onPageDestroy = this@MessagesFragment.onPageDestroy
|
||||||
arguments = Bundle("messageType" to Message.TYPE_DELETED)
|
arguments = Bundle("messageType" to Message.TYPE_DELETED)
|
||||||
|
args?.getBundle("page2")?.let {
|
||||||
|
arguments?.putAll(it)
|
||||||
|
}
|
||||||
} to getString(R.string.messages_tab_deleted)
|
} to getString(R.string.messages_tab_deleted)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -93,4 +107,15 @@ class MessagesFragment : Fragment(), CoroutineScope {
|
|||||||
|
|
||||||
activity.gainAttentionFAB()
|
activity.gainAttentionFAB()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val onPageDestroy = { position: Int, outState: Bundle? ->
|
||||||
|
arguments?.putBundle("page$position", outState)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
(b.viewPager.adapter as? FragmentLazyPagerAdapter)?.fragments?.forEach {
|
||||||
|
it.first.onDestroy()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import android.view.ViewGroup
|
|||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.lifecycle.Observer
|
import androidx.lifecycle.Observer
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
import androidx.recyclerview.widget.RecyclerView.NO_POSITION
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
@ -50,6 +51,8 @@ class MessagesListFragment : LazyFragment(), CoroutineScope {
|
|||||||
|
|
||||||
override fun onPageCreated(): Boolean { startCoroutineTimer(100L) {
|
override fun onPageCreated(): Boolean { startCoroutineTimer(100L) {
|
||||||
val messageType = arguments.getInt("messageType", Message.TYPE_RECEIVED)
|
val messageType = arguments.getInt("messageType", Message.TYPE_RECEIVED)
|
||||||
|
var topPosition = arguments.getInt("topPosition", NO_POSITION)
|
||||||
|
var bottomPosition = arguments.getInt("bottomPosition", NO_POSITION)
|
||||||
|
|
||||||
teachers = withContext(Dispatchers.Default) {
|
teachers = withContext(Dispatchers.Default) {
|
||||||
app.db.teacherDao().getAllNow(App.profileId)
|
app.db.teacherDao().getAllNow(App.profileId)
|
||||||
@ -87,6 +90,16 @@ class MessagesListFragment : LazyFragment(), CoroutineScope {
|
|||||||
adapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
setSwipeToRefresh(messageType in Message.TYPE_RECEIVED..Message.TYPE_SENT && items.isNullOrEmpty())
|
setSwipeToRefresh(messageType in Message.TYPE_RECEIVED..Message.TYPE_SENT && items.isNullOrEmpty())
|
||||||
|
|
||||||
|
(b.list.layoutManager as? LinearLayoutManager)?.let { layoutManager ->
|
||||||
|
if (topPosition != NO_POSITION && topPosition > layoutManager.findLastCompletelyVisibleItemPosition()) {
|
||||||
|
b.list.scrollToPosition(topPosition)
|
||||||
|
} else if (bottomPosition != NO_POSITION && bottomPosition < layoutManager.findFirstCompletelyVisibleItemPosition()) {
|
||||||
|
b.list.scrollToPosition(bottomPosition)
|
||||||
|
}
|
||||||
|
topPosition = NO_POSITION
|
||||||
|
bottomPosition = NO_POSITION
|
||||||
|
}
|
||||||
|
|
||||||
// show/hide relevant views
|
// show/hide relevant views
|
||||||
b.progressBar.isVisible = false
|
b.progressBar.isVisible = false
|
||||||
if (items.isNullOrEmpty()) {
|
if (items.isNullOrEmpty()) {
|
||||||
@ -98,4 +111,13 @@ class MessagesListFragment : LazyFragment(), CoroutineScope {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}; return true }
|
}; return true }
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
if (!isAdded) return
|
||||||
|
onPageDestroy?.invoke(position, Bundle(
|
||||||
|
"topPosition" to (b.list.layoutManager as? LinearLayoutManager)?.findFirstCompletelyVisibleItemPosition(),
|
||||||
|
"bottomPosition" to (b.list.layoutManager as? LinearLayoutManager)?.findLastCompletelyVisibleItemPosition()
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,13 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="Set last 10 as unseen"
|
android:text="Set last 10 as unseen"
|
||||||
android:textAllCaps="false" />
|
android:textAllCaps="false" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/rodo"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Duh rodo button"
|
||||||
|
android:textAllCaps="false" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
</layout>
|
</layout>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user