[UI/Login] Make Lab accessible from login page.

This commit is contained in:
Kuba Szczodrzyński 2022-09-17 15:00:34 +02:00
parent a85f935eb4
commit a5d0f4212d
No known key found for this signature in database
GPG Key ID: 70CB8A85BA1633CB
8 changed files with 88 additions and 19 deletions

View File

@ -8,6 +8,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -17,6 +18,8 @@ import pl.szczodrzynski.edziennik.MainActivity
import pl.szczodrzynski.edziennik.databinding.TemplateFragmentBinding import pl.szczodrzynski.edziennik.databinding.TemplateFragmentBinding
import pl.szczodrzynski.edziennik.ext.addOnPageSelectedListener import pl.szczodrzynski.edziennik.ext.addOnPageSelectedListener
import pl.szczodrzynski.edziennik.ui.base.lazypager.FragmentLazyPagerAdapter import pl.szczodrzynski.edziennik.ui.base.lazypager.FragmentLazyPagerAdapter
import pl.szczodrzynski.edziennik.ui.login.LoginActivity
import pl.szczodrzynski.edziennik.utils.SwipeRefreshLayoutNoTouch
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
class LabFragment : Fragment(), CoroutineScope { class LabFragment : Fragment(), CoroutineScope {
@ -26,7 +29,7 @@ class LabFragment : Fragment(), CoroutineScope {
} }
private lateinit var app: App private lateinit var app: App
private lateinit var activity: MainActivity private lateinit var activity: AppCompatActivity
private lateinit var b: TemplateFragmentBinding private lateinit var b: TemplateFragmentBinding
private val job: Job = Job() private val job: Job = Job()
@ -36,11 +39,14 @@ class LabFragment : Fragment(), CoroutineScope {
// local/private variables go here // local/private variables go here
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity = (getActivity() as MainActivity?) ?: return null activity = (getActivity() as AppCompatActivity?) ?: return null
context ?: return null context ?: return null
app = activity.application as App app = activity.application as App
b = TemplateFragmentBinding.inflate(inflater) b = TemplateFragmentBinding.inflate(inflater)
b.refreshLayout.setParent(activity.swipeRefreshLayout) when (activity) {
is MainActivity -> b.refreshLayout.setParent((activity as MainActivity).swipeRefreshLayout)
is LoginActivity -> b.refreshLayout.setParent((activity as LoginActivity).swipeRefreshLayout)
}
b.refreshLayout.isEnabled = false b.refreshLayout.isEnabled = false
return b.root return b.root
} }

View File

@ -9,6 +9,7 @@ import android.os.Process
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SimpleSQLiteQuery
import com.chuckerteam.chucker.api.Chucker import com.chuckerteam.chucker.api.Chucker
@ -35,7 +36,7 @@ class LabPageFragment : LazyFragment(), CoroutineScope {
} }
private lateinit var app: App private lateinit var app: App
private lateinit var activity: MainActivity private lateinit var activity: AppCompatActivity
private lateinit var b: LabFragmentBinding private lateinit var b: LabFragmentBinding
private val job: Job = Job() private val job: Job = Job()
@ -45,7 +46,7 @@ class LabPageFragment : LazyFragment(), CoroutineScope {
// local/private variables go here // local/private variables go here
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity = (getActivity() as MainActivity?) ?: return null activity = (getActivity() as AppCompatActivity?) ?: return null
context ?: return null context ?: return null
app = activity.application as App app = activity.application as App
b = LabFragmentBinding.inflate(inflater) b = LabFragmentBinding.inflate(inflater)
@ -55,6 +56,16 @@ class LabPageFragment : LazyFragment(), CoroutineScope {
override fun onPageCreated(): Boolean { override fun onPageCreated(): Boolean {
b.app = app b.app = app
if (app.profile.id == 0) {
b.last10unseen.isVisible = false
b.fullSync.isVisible = false
b.clearProfile.isVisible = false
b.rodo.isVisible = false
b.removeHomework.isVisible = false
b.unarchive.isVisible = false
b.profile.isVisible = false
}
b.last10unseen.onClick { b.last10unseen.onClick {
launch(Dispatchers.Default) { launch(Dispatchers.Default) {
val events = app.db.eventDao().getAllNow(App.profileId) val events = app.db.eventDao().getAllNow(App.profileId)
@ -139,7 +150,8 @@ class LabPageFragment : LazyFragment(), CoroutineScope {
b.profile += profiles.map { TextInputDropDown.Item(it.id.toLong(), "${it.id} ${it.name} archived ${it.archived}", tag = it) } b.profile += profiles.map { TextInputDropDown.Item(it.id.toLong(), "${it.id} ${it.name} archived ${it.archived}", tag = it) }
b.profile.select(app.profileId.toLong()) b.profile.select(app.profileId.toLong())
b.profile.setOnChangeListener { b.profile.setOnChangeListener {
activity.loadProfile(it.id.toInt()) if (activity is MainActivity)
(activity as MainActivity).loadProfile(it.id.toInt())
return@setOnChangeListener true return@setOnChangeListener true
} }

View File

@ -8,6 +8,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -22,6 +23,7 @@ import pl.szczodrzynski.edziennik.ext.input
import pl.szczodrzynski.edziennik.ext.set import pl.szczodrzynski.edziennik.ext.set
import pl.szczodrzynski.edziennik.ext.startCoroutineTimer import pl.szczodrzynski.edziennik.ext.startCoroutineTimer
import pl.szczodrzynski.edziennik.ui.base.lazypager.LazyFragment import pl.szczodrzynski.edziennik.ui.base.lazypager.LazyFragment
import pl.szczodrzynski.edziennik.ui.login.LoginActivity
import pl.szczodrzynski.edziennik.utils.SimpleDividerItemDecoration import pl.szczodrzynski.edziennik.utils.SimpleDividerItemDecoration
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -31,7 +33,7 @@ class LabProfileFragment : LazyFragment(), CoroutineScope {
} }
private lateinit var app: App private lateinit var app: App
private lateinit var activity: MainActivity private lateinit var activity: AppCompatActivity
private lateinit var b: TemplateListPageFragmentBinding private lateinit var b: TemplateListPageFragmentBinding
private val job: Job = Job() private val job: Job = Job()
@ -45,7 +47,7 @@ class LabProfileFragment : LazyFragment(), CoroutineScope {
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity = (getActivity() as MainActivity?) ?: return null activity = (getActivity() as AppCompatActivity?) ?: return null
context ?: return null context ?: return null
app = activity.application as App app = activity.application as App
b = TemplateListPageFragmentBinding.inflate(inflater) b = TemplateListPageFragmentBinding.inflate(inflater)
@ -142,7 +144,10 @@ class LabProfileFragment : LazyFragment(), CoroutineScope {
.show() .show()
} }
catch (e: Exception) { catch (e: Exception) {
activity.error(ApiError.fromThrowable(TAG, e)) if (activity is MainActivity)
(activity as MainActivity).error(ApiError.fromThrowable(TAG, e))
if (activity is LoginActivity)
(activity as LoginActivity).error(ApiError.fromThrowable(TAG, e))
} }
}) })

View File

@ -20,6 +20,7 @@ import pl.szczodrzynski.edziennik.data.api.models.ApiError
import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.LoginStore
import pl.szczodrzynski.edziennik.databinding.LoginActivityBinding import pl.szczodrzynski.edziennik.databinding.LoginActivityBinding
import pl.szczodrzynski.edziennik.ui.error.ErrorSnackbar import pl.szczodrzynski.edziennik.ui.error.ErrorSnackbar
import pl.szczodrzynski.edziennik.utils.SwipeRefreshLayoutNoTouch
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
class LoginActivity : AppCompatActivity(), CoroutineScope { class LoginActivity : AppCompatActivity(), CoroutineScope {
@ -32,6 +33,7 @@ class LoginActivity : AppCompatActivity(), CoroutineScope {
lateinit var navOptions: NavOptions lateinit var navOptions: NavOptions
val nav by lazy { Navigation.findNavController(this, R.id.nav_host_fragment) } val nav by lazy { Navigation.findNavController(this, R.id.nav_host_fragment) }
val errorSnackbar: ErrorSnackbar by lazy { ErrorSnackbar(this) } val errorSnackbar: ErrorSnackbar by lazy { ErrorSnackbar(this) }
val swipeRefreshLayout: SwipeRefreshLayoutNoTouch by lazy { b.swipeRefreshLayout }
private val job: Job = Job() private val job: Job = Job()
override val coroutineContext: CoroutineContext override val coroutineContext: CoroutineContext

View File

@ -66,6 +66,8 @@ class LoginChooserFragment : Fragment(), CoroutineScope {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
if (!isAdded) return if (!isAdded) return
val adapter = LoginChooserAdapter(activity, this::onLoginModeClicked)
b.versionText.setText( b.versionText.setText(
R.string.login_chooser_version_format, R.string.login_chooser_version_format,
app.buildManager.versionName, app.buildManager.versionName,
@ -73,10 +75,33 @@ class LoginChooserFragment : Fragment(), CoroutineScope {
) )
b.versionText.onClick { b.versionText.onClick {
app.buildManager.showVersionDialog(activity) app.buildManager.showVersionDialog(activity)
if (!App.devMode)
return@onClick
if (adapter.items.firstOrNull { it is LoginInfo.Register && it.internalName == "lab" } != null)
return@onClick
adapter.items.add(
index = 0,
element = LoginInfo.Register(
loginType = 999999,
internalName = "lab",
registerName = R.string.menu_lab,
registerLogo = R.drawable.face_2,
loginModes = listOf(
LoginInfo.Mode(
loginMode = 0,
name = 0,
icon = 0,
guideText = 0,
credentials = listOf(),
errorCodes = mapOf()
)
)
)
)
adapter.notifyItemInserted(0)
b.list.smoothScrollToPosition(0)
} }
val adapter = LoginChooserAdapter(activity, this::onLoginModeClicked)
LoginInfo.chooserList = LoginInfo.chooserList LoginInfo.chooserList = LoginInfo.chooserList
?: LoginInfo.list.toMutableList() ?: LoginInfo.list.toMutableList()
@ -164,7 +189,7 @@ class LoginChooserFragment : Fragment(), CoroutineScope {
anim.fillAfter = true anim.fillAfter = true
activity.getRootView().startAnimation(anim) activity.getRootView().startAnimation(anim)
b.list.smoothScrollToPosition(0) adapter.items.removeAll { it !is LoginInfo.Register }
adapter.items.add( adapter.items.add(
LoginInfo.Register( LoginInfo.Register(
loginType = 74, loginType = 74,
@ -183,7 +208,8 @@ class LoginChooserFragment : Fragment(), CoroutineScope {
) )
) )
) )
adapter.notifyItemInserted(adapter.items.size - 1) adapter.notifyDataSetChanged()
b.list.smoothScrollToPosition(adapter.items.size - 1)
} }
when { when {
@ -216,6 +242,11 @@ class LoginChooserFragment : Fragment(), CoroutineScope {
return return
} }
if (loginType.internalName == "lab") {
nav.navigate(R.id.labFragment, null, activity.navOptions)
return
}
if (!app.config.privacyPolicyAccepted) { if (!app.config.privacyPolicyAccepted) {
MaterialAlertDialogBuilder(activity) MaterialAlertDialogBuilder(activity)
.setTitle(R.string.privacy_policy) .setTitle(R.string.privacy_policy)

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -19,6 +19,11 @@
android:orientation="vertical" android:orientation="vertical"
android:visibility="visible"> android:visibility="visible">
<pl.szczodrzynski.edziennik.utils.SwipeRefreshLayoutNoTouch
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment <fragment
android:id="@+id/nav_host_fragment" android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment" android:name="androidx.navigation.fragment.NavHostFragment"
@ -26,6 +31,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
app:defaultNavHost="false" app:defaultNavHost="false"
app:navGraph="@navigation/nav_login" /> app:navGraph="@navigation/nav_login" />
</pl.szczodrzynski.edziennik.utils.SwipeRefreshLayoutNoTouch>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton

View File

@ -22,6 +22,9 @@
<action <action
android:id="@+id/action_loginChooserFragment_to_loginEggsFragment" android:id="@+id/action_loginChooserFragment_to_loginEggsFragment"
app:destination="@id/loginEggsFragment" /> app:destination="@id/loginEggsFragment" />
<action
android:id="@+id/action_loginChooserFragment_to_labFragment"
app:destination="@id/labFragment" />
</fragment> </fragment>
<!-- eggs --> <!-- eggs -->
<fragment <fragment
@ -95,4 +98,8 @@
android:id="@+id/loginFinishFragment" android:id="@+id/loginFinishFragment"
android:name="pl.szczodrzynski.edziennik.ui.login.LoginFinishFragment" android:name="pl.szczodrzynski.edziennik.ui.login.LoginFinishFragment"
android:label="LoginFinishFragment" /> android:label="LoginFinishFragment" />
<fragment
android:id="@+id/labFragment"
android:name="pl.szczodrzynski.edziennik.ui.debug.LabFragment"
android:label="LabFragment" />
</navigation> </navigation>