Skip to content

Instantly share code, notes, and snippets.

View oliverspryn's full-sized avatar
☀️
Everyday is a great day!

Oliver Spryn oliverspryn

☀️
Everyday is a great day!
View GitHub Profile
@oliverspryn
oliverspryn / PhotosControllerTest.kt
Last active October 21, 2021 21:54
An incorrect example of how to unit test the PhotosController
class PhotosControllerTest : Spek({
describe("The PhotosController") {
var mockPhotosApiService: PhotosApiService? = null
var mockSchedulerForwarder: SchedulerForwarder? = null
var uut: PhotosController? = null
beforeEachTest {
// uut and supporting classes setup and mocked here
}
@oliverspryn
oliverspryn / PhotosController.kt
Created October 21, 2021 21:21
A simple unit under test
class PhotosController @Inject constructor(
private val compositeDisposable: CompositeDisposable,
private val photosApiService: PhotosApiService,
private val schedulerForwarder: SchedulerForwarder
) : PhotoListItemViewMvc.Listener {
@VisibleForTesting
var photosAdapter: PhotosAdapter? = null
fun getPhotos() {
@oliverspryn
oliverspryn / .gitignore
Created October 14, 2021 16:32
Git Ignore file for .idea Folders
# Inspired, in part, by:
# - https://olv.to/idea-folder
# - https://olv.to/idea-ignore
# Discarding
*.iml
*.xml
caches
libraries
@oliverspryn
oliverspryn / HttpNavigator.kt
Created July 21, 2021 11:58
The HTTP navigator, highlight only its differences between itself and the telephone navigator
@Navigator.Name("http")
class HttpNavigator(
private val context: Context
) : Navigator<HttpNavigator.Destination>() {
override fun navigate(
destination: Destination,
args: Bundle?,
navOptions: NavOptions?,
navigatorExtras: Extras?
@oliverspryn
oliverspryn / navigation.kt
Created July 21, 2021 11:57
Example code to route to a URL with the navigation graph
val args = MyFragmentDirections.actionMyFragmentToHttp(
"https://ddg.co/"
)
Navigation.findNavController(view).navigate(args)
@oliverspryn
oliverspryn / nav_graph.xml
Created July 21, 2021 11:56
Proposed web URL/HTTP navigator schema
<http
android:id="@+id/http">
<argument
android:name="url"
app:argType="string"
app:nullable="false" />
</http>
@oliverspryn
oliverspryn / nav_graph.xml
Created July 21, 2021 11:55
A simplified navigation graph, which shows the telephone destination in use
<navigation>
<fragment
android:id="@+id/my_fragment"
android:name="com.example.MyFragment"
android:label="My Fragment"
tools:layout="@layout/fragment_my">
<action
android:id="@+id/my_fragment_to_telephone"
app:destination="@+id/telephone" />
@oliverspryn
oliverspryn / activity_main.xml
Created July 21, 2021 11:53
A fragment container view which implements our custom navigation host
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="package.name.to.your.CustomNavHostFragment"
...
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
@oliverspryn
oliverspryn / CustomNavHostFragment.kt
Created July 21, 2021 11:52
A navigation host fragment with support for the TelephoneNavigator
class CustomNavHostFragment : NavHostFragment() {
override fun onCreateNavController(navController: NavController) {
super.onCreateNavController(navController)
activity?.let {
navController.navigatorProvider += TelephoneNavigator(it)
}
}
}
@oliverspryn
oliverspryn / TelephoneNavigator.kt
Created July 21, 2021 11:51
A more robust navigation approach, with error handling
override fun navigate(
destination: Destination,
args: Bundle?,
navOptions: NavOptions?,
navigatorExtras: Extras?
): NavDestination? {
val phoneNumber: String = args?.getString("phoneNumber")
if (phoneNumber.isNullOrBlank()) return null // Solves #1
val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phoneNumber"))