-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Koin Dependency Injection, Navigation component
- Loading branch information
Jonathan Imperato
committed
Feb 24, 2021
1 parent
39c8467
commit accca81
Showing
22 changed files
with
316 additions
and
53 deletions.
There are no files selected for viewing
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
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
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
12 changes: 0 additions & 12 deletions
12
app/src/main/java/com/macoev/roomsample/RecyclerViewBindings.kt
This file was deleted.
Oops, something went wrong.
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
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
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/macoev/roomsample/application/App.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.macoev.roomsample.application | ||
|
||
import android.app.Application | ||
import com.macoev.roomsample.viewmodel.UserViewModel | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.android.ext.koin.androidLogger | ||
import org.koin.core.context.startKoin | ||
import org.koin.dsl.module | ||
|
||
class App : Application() { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
startKoin { | ||
androidLogger() | ||
androidContext(this@App) | ||
modules(listOf(viewModelModule)) | ||
} | ||
} | ||
} | ||
val viewModelModule = module { | ||
factory { UserViewModel( get()) } | ||
} |
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
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
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
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
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/macoev/roomsample/fragment/MainFragment.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.macoev.roomsample.fragment | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.os.bundleOf | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.distinctUntilChanged | ||
import androidx.navigation.fragment.findNavController | ||
import com.macoev.roomsample.R | ||
import com.macoev.roomsample.databinding.MainFragmentBinding | ||
import com.macoev.roomsample.viewmodel.UserViewModel | ||
import org.koin.androidx.viewmodel.ext.android.sharedViewModel | ||
import org.koin.core.parameter.parametersOf | ||
|
||
class MainFragment : Fragment() { | ||
|
||
private var binding: MainFragmentBinding?=null | ||
private val model: UserViewModel by sharedViewModel() | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
binding = MainFragmentBinding.inflate(inflater, container, false) | ||
return binding!!.root | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
binding!!.viewModel = model | ||
model.selectedUser.distinctUntilChanged().observe(requireActivity(), { | ||
it?.let { | ||
findNavController().navigate(MainFragmentDirections.actionMainFragmentToUserDetailFragment()) | ||
} | ||
}) | ||
} | ||
|
||
companion object { | ||
fun newInstance() = MainFragment() | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/macoev/roomsample/fragment/UserDetailFragment.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.macoev.roomsample.fragment | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import com.macoev.roomsample.databinding.UserDetailFragmentBinding | ||
import com.macoev.roomsample.viewmodel.UserViewModel | ||
import org.koin.androidx.viewmodel.ext.android.sharedViewModel | ||
|
||
class UserDetailFragment : Fragment() { | ||
|
||
companion object { | ||
fun newInstance() = UserDetailFragment() | ||
} | ||
|
||
private lateinit var binding: UserDetailFragmentBinding | ||
private val viewModel: UserViewModel by sharedViewModel() | ||
// private val args: UserDetailFragmentArguments by navArgs() | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
binding = UserDetailFragmentBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onActivityCreated(savedInstanceState: Bundle?) { | ||
super.onActivityCreated(savedInstanceState) | ||
binding.viewModel = viewModel | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/macoev/roomsample/utils/RecyclerViewBindings.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.macoev.roomsample.utils | ||
|
||
import android.view.MotionEvent | ||
import androidx.databinding.BindingAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
|
||
object RecyclerViewBindings { | ||
@JvmStatic | ||
@BindingAdapter("adapter") | ||
fun <VH : RecyclerView.ViewHolder> adapter( | ||
recyclerView: RecyclerView, | ||
adapter: RecyclerView.Adapter<VH> | ||
) { | ||
recyclerView.adapter = adapter | ||
} | ||
|
||
@JvmStatic | ||
@BindingAdapter("itemTap") | ||
fun itemTap(recyclerView: RecyclerView, click: (Int) -> Boolean) { | ||
recyclerView.addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() { | ||
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean { | ||
val child = recyclerView.findChildViewUnder(e.x, e.y) | ||
child?.let { | ||
val pos = rv.getChildAdapterPosition(it) | ||
return click(pos) | ||
} | ||
return false | ||
} | ||
|
||
}) | ||
} | ||
} |
Oops, something went wrong.