Last active
June 9, 2020 20:48
-
-
Save Zhuinden/7306e127637fbcbea06f0b113d038a78 to your computer and use it in GitHub Desktop.
Single-Activity: Create Login Credentials (Simple-Stack)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateLoginCredentialsFragment : KeyedFragment(R.layout.create_login_credentials_fragment) { | |
private val viewModel by lazy { lookup<RegistrationViewModel>() } | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val binding = CreateLoginCredentialsFragmentBinding.bind(view) | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Parcelize | |
class CreateLoginCredentialsKey : DefaultFragmentKey(), ScopeKey.Child { | |
override fun instantiateFragment(): Fragment = CreateLoginCredentialsFragment() | |
override fun getParentScopes(): List<String> = listOf(ServiceProvider.REGISTRATION_TAG) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegistrationViewModel( | |
private val authenticationManager: AuthenticationManager, | |
private val backstack: Backstack | |
) : Bundleable, ScopedServices.Registered { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity(), SimpleStateChanger.NavigationHandler { | |
private lateinit var fragmentStateChanger: DefaultFragmentStateChanger | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val binding = MainActivityBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
fragmentStateChanger = | |
DefaultFragmentStateChanger(supportFragmentManager, R.id.rootContainer) | |
Navigator.configure() | |
.setStateChanger(SimpleStateChanger(this)) | |
.setScopedServices(ServiceProvider()) | |
.setGlobalServices( | |
GlobalServices.builder() | |
.add(AuthenticationManager) | |
.build() | |
) | |
.install(this, binding.rootContainer, LoginKey()) | |
} | |
override fun onBackPressed() { | |
if (!Navigator.onBackPressed(this)) { | |
super.onBackPressed() | |
} | |
} | |
override fun onNavigationEvent(stateChange: StateChange) { | |
fragmentStateChanger.handleStateChange(stateChange) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ServiceProvider : DefaultServiceProvider() { | |
companion object { | |
const val REGISTRATION_TAG = "registration" | |
} | |
override fun bindServices(serviceBinder: ServiceBinder) { | |
super.bindServices(serviceBinder) | |
val scope = serviceBinder.scopeTag | |
with(serviceBinder) { | |
when (scope) { | |
REGISTRATION_TAG -> { | |
add(RegistrationViewModel(lookup(), backstack)) | |
} | |
else -> { | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment