Skip to content

Instantly share code, notes, and snippets.

View magdamiu's full-sized avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / CleanMeaningfulNames3.kt
Created August 21, 2021 12:35
Clean Code with Kotlin by Magda Miu - Meaningful names 3 - Clean Code
users.filter{ user -> user.job == Job.Developer }
.map{ developer -> developer.birthDate.dayOfMonth }
.filter { birthDay -> birthDay <= 10 }
.min()
@magdamiu
magdamiu / UncleanMeaningfulNames3.kt
Created August 21, 2021 12:35
Clean Code with Kotlin by Magda Miu - Meaningful names 3 - Unclean Code
users.filter{ it.job == Job.Developer }
.map{ it.birthDate.dayOfMonth }
.filter{ it <= 10 }
.min()
@magdamiu
magdamiu / CleanMeaningfulNames2.kt
Last active August 21, 2021 12:36
Clean Code with Kotlin by Magda Miu - Meaningful names 2 - Clean Code
class Book(val title: String?, val publishYear: Int?)
fun displayBookDetails(book: Book) {
val title = book.title ?:
throw IllegalArgumentException("Title required")
val publishYear = book.publishYear ?: return
println("$title: $publishYear")
}
@magdamiu
magdamiu / UncleanMeaningfulNames2.kt
Last active August 21, 2021 12:36
Clean Code with Kotlin by Magda Miu - Meaningful names 2 - Unclean Code
class Book(val title: String?, val publishYear: Int?)
fun displayBookDetails(book: Book) {
val title = book.title
if (title == null)
throw IllegalArgumentException("Title required")
val publishYear = book.publishYear
if (publishYear == null) return
println("$title: $publishYear")
@magdamiu
magdamiu / CleanMeaningfulNames1.kt
Last active August 21, 2021 12:37
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Clean Code
data class PathParts(val directory: String, val fileName: String)
fun splitPath(path: String) =
PathParts(
path.substringBeforeLast('/', ""),
path.substringAfterLast('/'))
@magdamiu
magdamiu / UncleanMeaningfulNames1.kt
Last active August 21, 2021 12:37
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Unclean Code
data class GetFile(val d: String, val n: String)
val pattern = Regex("(.+)/([^/]*)")
fun files(ph: String): PathParts {
val match = pattern.matchEntire(ph)
?: return PathParts("", ph)
return PathParts(match.groupValues[1],
match.groupValues[2])
@magdamiu
magdamiu / build.gradle
Created January 12, 2021 17:50
gradle for Room
def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
@magdamiu
magdamiu / ApplicationData.java
Last active January 5, 2021 18:52
Sample of implementing a class for managing SharedPreferences
import android.content.Context;
import android.content.SharedPreferences;
public class ApplicationData {
private static final String APP_KEY = "android_course_key";
// save a String value by key
public static void setStringValueInSharedPreferences(Context context, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(ApplicationData.APP_KEY,
Context.MODE_PRIVATE);
@magdamiu
magdamiu / MainActivity.java
Last active December 29, 2020 18:06
Check permissions in Android
// manifest add => <uses-permission android:name="android.permission.CAMERA" />
import static android.Manifest.permission.CAMERA;
private static final int REQUEST_CODE_CAMERA = 23;
if (ContextCompat.checkSelfPermission(StylesActivity.this, CAMERA) != PackageManager.PERMISSION_GRANTED) {
// unhappy path
ActivityCompat.requestPermissions(StylesActivity.this, new String[]{CAMERA}, REQUEST_CODE_CAMERA);
@magdamiu
magdamiu / MainActivity.java
Created December 28, 2020 21:50
Setup swipe to refresh
private void setupSwipeToRefresh() {
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Make sure you call swipeRefreshLayout.setRefreshing(false)
// once the network request has completed successfully.
inbox();
}
});
// Configure the refreshing colors