Skip to content

Instantly share code, notes, and snippets.

View louis993546's full-sized avatar
😂

Louis Tsai louis993546

😂
View GitHub Profile
buildscript {
ext.kotlin_version = '1.3.0'
ext.support_library_version = '27.1.1'
...
}
...
abstract class NavigationFragment : Fragment() {
lateinit var navigationDelegate: NavigationDelegate
override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is NavigationDelegate) {
navigationDelegate = context
} else {
TODO("Make a custom error or something to be clear what this is suppose to do")
}
}
interface NavigationDelegate {
//TODO need a way to pass parameter
fun navigate(from: Scene, to: Scene)
}
enum class Scene {
ENTER_LOFT,
CREATION,
JOINING,
WHAT_IS_LOFT
}
dependencies {
implementation project(path: ':common')
implementation project(path: ':api')
implementation project(path: ':onboarding')
...
}
org.gradle.parallel=true
sealed class Transition {
class WhatIsToEnter : Transition()
class EnterToCreation : Transition()
class EnterToJoining : Transition()
class Joining2WaitForConfirmation : Transition()
}
override fun navigate(transition: Transition) {
val actionId = when (transition) {
is Transition.WhatIsToEnter -> R.id.action_whatIsLoftFragment_to_enterLoftFragment
is Transition.EnterToCreation -> R.id.action_enterLoftFragment_to_creationFragment
is Transition.EnterToJoining -> R.id.action_enterLoftFragment_to_joiningFragment
is Transition.Joining2WaitForConfirmation ->
R.id.action_joiningFragment_to_waitForConfirmationFragment
}
findNavController(R.id.nav_host_fragment).navigate(actionId)
}
class SomethingActivity extends AppCompatActivity {
@Override
void onCreate() {
super.onCreate();
setContentView(R.layout.activity_something);
}
@Override
void onResume() {
retrofitService.someApiEndpoint().enqueue(this);
@louis993546
louis993546 / Adapter.kt
Created May 14, 2019 23:03
Using Factory pattern for RecyclerView
class SomeAdapter(
val factory1: ViewHolderFactory1,
val factory2: ViewHOlderFactory2
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun getItemViewType(position: Int) = if (position == 0) 1 else 2
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = when (viewType) {
1 -> factory1.createViewHolder(parent)
2 -> factory2.createViewHolder(parent)
else -> error("This should not be possible")
interface ViewHolderFactory {
fun createViewHolder(parent: ViewGroup, onClick: () -> Unit): ViewHolder
}
class ViewHolderFactoryImpl1 : ViewHolderFactory {
override fun createViewHolder(parent: ViewGroup, onClick: () -> Unit): ViewHolder = TODO("")
}
class ViewHolderFactoryImpl2 : ViewHolderFactory {
override fun createViewHolder(parent: ViewGroup, onClick: () -> Unit): ViewHolder = TODO("")