Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam McNeilly AdamMc331

View GitHub Profile
@AdamMc331
AdamMc331 / UIImage.kt
Created Nov 30, 2021
Shows how UIImage concept can be used for prod and previews.
View UIImage.kt
sealed class UiImage {
data class LocalImage(val imageRes: Int) : UiImage()
data class RemoteImage(val imageUrl: String) : UiImage()
}
// Component
@Composable
fun UserProfile(
avatar: UiImage,
)
View DramaCategoryFragment.kt
class DramaCategoryFragment : Fragment(R.layout.fragment_drama_category),
MovieListAdapter.FavouriteMovieListener {
// ...
private fun getDramaMovies() {
val movieAPI = MoviesData.defaultInstance()
// ...
@AdamMc331
AdamMc331 / .gitignore
Created Aug 12, 2021
Short example of storing API Keys for an Android repo.
View .gitignore
local.properties
# Don't forget to ignore this from your source control
View OffsetMapping.kt
val cameroonNumberTranslator = object : OffsetMapping {
override fun originalToTransformed(offset: Int): Int {
// [offset [0 - 2] remain the same]
if (offset <= 2) return offset
// [3 - 5] transformed to [4 - 6] respectively
if (offset <= 5) return offset + 1
// [6 - 8] transformed to [8 - 10] respectively
if (offset <= 8 ) return offset + 2
return 11 // Total number of digits, plus two hyphens
}
@AdamMc331
AdamMc331 / Fragment.kt
Created Jun 8, 2021
Example for navigating with ViewModel to Fragment
View Fragment.kt
class Fragment : Fragment() {
fun listenForNavigation() {
lifecycleScope.launchWhenResumed {
val directions = viewModel.navigationChannel.receive()
findNavController().navigate(directions)
}
}
}
@AdamMc331
AdamMc331 / gist:27c6e95d52068b665eb465dd082eb9af
Created Mar 24, 2021
Day/Night Jetpack Compose Preview template. I called this `dayNightPrev`.
View gist:27c6e95d52068b665eb465dd082eb9af
// NOTES: This doesn't help you import it into Intellij, I don't have the time to write that up right now, but if you know how
// to create your own live template, you can copy and paste the below.
@Preview(
name = "Night Mode",
uiMode = Configuration.UI_MODE_NIGHT_YES,
)
@Preview(
name = "Day Mode",
uiMode = Configuration.UI_MODE_NIGHT_NO,
@AdamMc331
AdamMc331 / MissingJsonClassAnnotationDetector.kt
Last active Feb 26, 2021
A custom lint check for verifying any classes using the `@Json` annotation must also use the `@JsonClass` annotation.
View MissingJsonClassAnnotationDetector.kt
package // ...
import com.android.tools.lint.client.api.UElementHandler
import com.android.tools.lint.detector.api.Category
import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.Implementation
import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.JavaContext
import com.android.tools.lint.detector.api.Scope
import com.android.tools.lint.detector.api.Severity
@AdamMc331
AdamMc331 / PrescriptionCardListController.kt
Created Dec 11, 2020
Demonstrating an EpoxyGroupModel implementation and what I would like to do if possible.
View PrescriptionCardListController.kt
/**
* This class is the current implementation.
*
* Notice that in [addPendingPrescriptionCard] and [addActivePrescriptionCard],
* we have relatively identical code:
*
* 1. Create the group.
* 2. Create all the models and add them to the group.
* 3. Add the group to the main controller.
*
@AdamMc331
AdamMc331 / Patient.kt
Created Oct 4, 2020
Shows how we can map a network response object to a UI model & manually check for required fields.
View Patient.kt
data class PatientDTO(
@field:Json(name = "id")
val id: String? = null,
@field:Json(name = "firstName")
val firstName: String? = null,
@field:Json(name = "lastName")
val lastName: String? = null
) {
fun toPatient(): Patient {
val id = this.id.orEmpty()
@AdamMc331
AdamMc331 / DateTesting.kt
Created Sep 25, 2020
Gives an example of how I take a date that is GMT and convert it to the TimeZone that I want.
View DateTesting.kt
/**
* Given a supplied [dateString] that matches our [RFC_3339_FORMAT], convert it to a
* [Calendar] instance for the corresponding [timeZoneString].
*
* The only way I've been able to do this is:
* 1. Take the date string, which is GMT, and convert it to a Date.
* 2. Update the TZ on our SimpleDateFormat, to get the string in the desired TimeZone.
* 3. Reverse engineer that back into a date.
* 4. Return this calendar.
*