Skip to content

Commit

Permalink
Start working on the navigation example page.
Browse files Browse the repository at this point in the history
  • Loading branch information
pandulapeter committed Oct 11, 2020
1 parent 21495dc commit e85d7de
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 20 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/com/pandulapeter/beagle/appDemo/Modules.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.pandulapeter.beagle.appDemo

import com.pandulapeter.beagle.appDemo.data.networking.ktor.KtorRemoteSongSource
import com.pandulapeter.beagle.appDemo.data.ModuleRepository
import com.pandulapeter.beagle.appDemo.data.networking.retrofit.OkHttpRemoteSongSource
import com.pandulapeter.beagle.appDemo.data.SongRepository
import com.pandulapeter.beagle.appDemo.data.networking.ktor.KtorRemoteSongSource
import com.pandulapeter.beagle.appDemo.data.networking.retrofit.OkHttpRemoteSongSource
import com.pandulapeter.beagle.appDemo.feature.main.about.AboutViewModel
import com.pandulapeter.beagle.appDemo.feature.main.about.licences.LicencesViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.ExamplesViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.analytics.AnalyticsViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.authentication.AuthenticationViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.valueWrappers.ValueWrappersViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.mockDataGenerator.MockDataGeneratorViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.navigation.NavigationViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.networkRequestInterceptor.NetworkRequestInterceptorViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.overlay.OverlayViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.simpleSetup.SimpleSetupViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.staticData.StaticDataViewModel
import com.pandulapeter.beagle.appDemo.feature.main.examples.valueWrappers.ValueWrappersViewModel
import com.pandulapeter.beagle.appDemo.feature.main.playground.PlaygroundViewModel
import com.pandulapeter.beagle.appDemo.feature.main.playground.addModule.AddModuleViewModel
import com.pandulapeter.beagle.appDemo.feature.main.setup.SetupViewModel
Expand All @@ -40,6 +41,7 @@ private val featureModule = module {
viewModel { AnalyticsViewModel() }
viewModel { MockDataGeneratorViewModel() }
viewModel { OverlayViewModel() }
viewModel { NavigationViewModel() }
viewModel { PlaygroundViewModel(get()) }
viewModel { AddModuleViewModel(get()) }
viewModel { AboutViewModel(androidContext()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import com.pandulapeter.beagle.appDemo.R
import com.pandulapeter.beagle.appDemo.data.model.CaseStudy
import com.pandulapeter.beagle.appDemo.feature.main.examples.analytics.AnalyticsFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.authentication.AuthenticationFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.valueWrappers.ValueWrappersFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.list.ExamplesAdapter
import com.pandulapeter.beagle.appDemo.feature.main.examples.list.ExamplesListItem
import com.pandulapeter.beagle.appDemo.feature.main.examples.mockDataGenerator.MockDataGeneratorFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.navigation.NavigationFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.networkRequestInterceptor.NetworkRequestInterceptorFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.overlay.OverlayFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.simpleSetup.SimpleSetupFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.staticData.StaticDataFragment
import com.pandulapeter.beagle.appDemo.feature.main.examples.valueWrappers.ValueWrappersFragment
import com.pandulapeter.beagle.appDemo.feature.shared.ListFragment
import com.pandulapeter.beagle.appDemo.utils.TransitionType
import com.pandulapeter.beagle.appDemo.utils.createTextModule
Expand Down Expand Up @@ -62,6 +63,7 @@ class ExamplesFragment : ListFragment<ExamplesViewModel, ExamplesListItem>(R.str
CaseStudy.AUTHENTICATION -> navigateTo(AuthenticationFragment.Companion::newInstance)
CaseStudy.MOCK_DATA_GENERATOR -> navigateTo(MockDataGeneratorFragment.Companion::newInstance)
CaseStudy.OVERLAY -> navigateTo(OverlayFragment.Companion::newInstance)
CaseStudy.NAVIGATION -> navigateTo(NavigationFragment.Companion::newInstance)
else -> binding.root.showSnackbar(caseStudy.title)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.pandulapeter.beagle.appDemo.feature.main.examples.navigation

import androidx.lifecycle.viewModelScope
import com.pandulapeter.beagle.appDemo.R
import com.pandulapeter.beagle.appDemo.feature.main.examples.ExamplesDetailFragment
import com.pandulapeter.beagle.appDemo.feature.shared.list.BaseAdapter
import com.pandulapeter.beagle.appDemo.feature.shared.list.ListItem
import com.pandulapeter.beagle.appDemo.utils.createTextModule
import com.pandulapeter.beagle.common.contracts.module.Module
import com.pandulapeter.beagle.modules.DividerModule
import com.pandulapeter.beagle.modules.HeaderModule
import com.pandulapeter.beagle.modules.PaddingModule
import org.koin.androidx.viewmodel.ext.android.viewModel

class NavigationFragment : ExamplesDetailFragment<NavigationViewModel, ListItem>(R.string.case_study_navigation_title) {

override val viewModel by viewModel<NavigationViewModel>()
private var currentPage
get() = viewModel.currentPage
set(value) {
viewModel.currentPage = value
refreshBeagle()
}

override fun createAdapter() = object : BaseAdapter<ListItem>(
scope = viewModel.viewModelScope
) {}

override fun getBeagleModules(): List<Module<*>> = mutableListOf<Module<*>>().apply {
addHeaderSection()
when (currentPage) {
NavigationViewModel.Page.MAIN -> addMainSection()
NavigationViewModel.Page.SECTION_1 -> addSection1()
NavigationViewModel.Page.SECTION_2 -> addSection2()
NavigationViewModel.Page.SECTION_3 -> addSection3()
}
}

private fun MutableList<Module<*>>.addHeaderSection() {
add(
HeaderModule(
title = R.string.case_study_navigation_header_title,
subtitle = R.string.case_study_navigation_header_subtitle,
text = R.string.case_study_navigation_header_text
)
)
add(DividerModule("divider"))
add(PaddingModule())
}

private fun MutableList<Module<*>>.addMainSection() {
add(
createTextModule(
textResourceId = R.string.case_study_navigation_header_section_1,
onItemSelected = { currentPage = NavigationViewModel.Page.SECTION_1 }
)
)
add(
createTextModule(
textResourceId = R.string.case_study_navigation_header_section_2,
onItemSelected = { currentPage = NavigationViewModel.Page.SECTION_2 }
)
)
add(
createTextModule(
textResourceId = R.string.case_study_navigation_header_section_3,
onItemSelected = { currentPage = NavigationViewModel.Page.SECTION_3 }
)
)
}

private fun MutableList<Module<*>>.addCloseButton() {
add(
createTextModule(
textResourceId = R.string.case_study_navigation_close_section,
icon = R.drawable.ic_close,
onItemSelected = { currentPage = NavigationViewModel.Page.MAIN }
)
)
}

private fun MutableList<Module<*>>.addSection1() {
addCloseButton()
add(createTextModule(R.string.case_study_navigation_header_section_1_detail))
}

private fun MutableList<Module<*>>.addSection2() {
addCloseButton()
add(createTextModule(R.string.case_study_navigation_header_section_2_detail))
}

private fun MutableList<Module<*>>.addSection3() {
addCloseButton()
add(createTextModule(R.string.case_study_navigation_header_section_3_detail))
}

companion object {
fun newInstance() = NavigationFragment()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.pandulapeter.beagle.appDemo.feature.main.examples.navigation

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.pandulapeter.beagle.appDemo.R
import com.pandulapeter.beagle.appDemo.feature.shared.ListViewModel
import com.pandulapeter.beagle.appDemo.feature.shared.list.ListItem
import com.pandulapeter.beagle.appDemo.feature.shared.list.TextViewHolder

class NavigationViewModel : ListViewModel<ListItem>() {

var currentPage = Page.MAIN

override val items: LiveData<List<ListItem>> = MutableLiveData(
listOf(
TextViewHolder.UiModel(R.string.case_study_navigation_text_1)
)
)

enum class Page {
MAIN,
SECTION_1,
SECTION_2,
SECTION_3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ class OverlayFragment : ExamplesDetailFragment<OverlayViewModel, ListItem>(R.str
alpha = 192
}
}
private var isSwitchEnabled = false

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
isSwitchEnabled = savedInstanceState?.getBoolean(IS_SWITCH_ENABLED) ?: isSwitchEnabled
super.onViewCreated(view, savedInstanceState)
Beagle.addOverlayListener(
listener = this,
Expand All @@ -48,21 +46,16 @@ class OverlayFragment : ExamplesDetailFragment<OverlayViewModel, ListItem>(R.str
createTextModule(R.string.case_study_overlay_hint),
SwitchModule(
text = getText(R.string.case_study_overlay_enable),
initialValue = isSwitchEnabled,
initialValue = viewModel.isSwitchEnabled,
onValueChanged = {
isSwitchEnabled = it
viewModel.isSwitchEnabled = it
Beagle.invalidateOverlay()
}
)
)

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean(IS_SWITCH_ENABLED, isSwitchEnabled)
}

override fun onDrawOver(canvas: Canvas, insets: Insets) {
if (isSwitchEnabled) {
if (viewModel.isSwitchEnabled) {
val usableWidth = canvas.width - insets.left - insets.right
val usableHeight = canvas.height - insets.top - insets.bottom
canvas.drawCircle(
Expand All @@ -75,8 +68,6 @@ class OverlayFragment : ExamplesDetailFragment<OverlayViewModel, ListItem>(R.str
}

companion object {
private const val IS_SWITCH_ENABLED = "isSwitchEnabled"

fun newInstance() = OverlayFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.pandulapeter.beagle.appDemo.feature.shared.list.TextViewHolder

class OverlayViewModel : ListViewModel<ListItem>() {

var isSwitchEnabled = false

override val items: LiveData<List<ListItem>> = MutableLiveData(
listOf(
TextViewHolder.UiModel(R.string.case_study_overlay_text_1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class StaticDataFragment : ExamplesDetailFragment<StaticDataViewModel, ListItem>
PaddingModule(),
PaddingModule(),
createTextModule(R.string.case_study_static_data_module_text_3),
DividerModule(id = "divider"),
DividerModule("divider"),
createTextModule(R.string.case_study_static_data_module_text_4),
KeyValueListModule(
id = "keyValueList",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import com.pandulapeter.beagle.modules.TextModule
fun createTextModule(
@StringRes textResourceId: Int,
@DrawableRes icon: Int? = null,
id: String = "text_$textResourceId"
id: String = "text_$textResourceId",
onItemSelected: (() -> Unit)? = null
) = TextModule(
id = id,
text = textResourceId,
icon = icon
icon = icon,
onItemSelected = onItemSelected
)

fun createSectionHeaderModule(
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,19 @@
<string name="case_study_overlay_text_3">Multiple <i>OverlayListener</i> implementations can draw on the canvas: invalidating basically invokes all registered listeners in order.\n\nA more useful example of overlay listeners is implemented in the built-in <i>KeylineOverlaySwitch</i> module.</string>

<!-- Case studies / Navigation -->
<string name="case_study_navigation_title">Navigation within the menu</string>
<string name="case_study_navigation_title">Navigating within the menu</string>
<string name="case_study_navigation_description">Create submenus to better organize the modules.</string>
<string name="case_study_navigation_text_1">Coming soon</string>
<string name="case_study_navigation_header_title">Debug menu</string>
<string name="case_study_navigation_header_subtitle">Navigation example</string>
<string name="case_study_navigation_header_text">You can navigate within this debug menu!</string>
<string name="case_study_navigation_header_section_1">Section 1</string>
<string name="case_study_navigation_header_section_1_detail">Welcome to Section 1!</string>
<string name="case_study_navigation_header_section_2">Section 2</string>
<string name="case_study_navigation_header_section_2_detail">Welcome to Section 2!</string>
<string name="case_study_navigation_header_section_3">Section 3</string>
<string name="case_study_navigation_header_section_3_detail">Welcome to Section 3!</string>
<string name="case_study_navigation_close_section">Close section</string>

<!-- Case studies / Bug reporting tool -->
<string name="case_study_bug_reporting_tool_title">Bug reporting tool</string>
Expand Down

0 comments on commit e85d7de

Please sign in to comment.