Skip to content

Instantly share code, notes, and snippets.

Avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / UncleanMeaningfulNames2.kt
Last active Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 2 - Unclean Code
View UncleanMeaningfulNames2.kt
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 Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Clean Code
View CleanMeaningfulNames1.kt
data class PathParts(val directory: String, val fileName: String)
fun splitPath(path: String) =
PathParts(
path.substringBeforeLast('/', ""),
path.substringAfterLast('/'))
@magdamiu
magdamiu / UncleanMeaningfulNames1.kt
Last active Aug 21, 2021
Clean Code with Kotlin by Magda Miu - Meaningful names 1 - Unclean Code
View UncleanMeaningfulNames1.kt
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])
View build.gradle
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 Jan 5, 2021
Sample of implementing a class for managing SharedPreferences
View ApplicationData.java
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 Dec 29, 2020
Check permissions in Android
View MainActivity.java
// 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 Dec 28, 2020
Setup swipe to refresh
View MainActivity.java
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
View EmailAdapter.java
// Clean all elements of the recycler
public void clear() {
emails.clear();
notifyDataSetChanged();
}
// Add a list of items
public void addAll(List<Email> list) {
emails.addAll(list);
notifyDataSetChanged();
@magdamiu
magdamiu / main_layout.xml
Created Dec 28, 2020
Wrap RecyclerView in a SwipeRefreshLayout
View main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewEmails"
android:layout_width="match_parent"
android:layout_height="match_parent" />
@magdamiu
magdamiu / build.gradle
Created Dec 28, 2020
Add swiperefreshlayout dependency
View build.gradle
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'