Skip to content

Instantly share code, notes, and snippets.

View IamCostello's full-sized avatar

Krzysztof Dąbrowski IamCostello

  • Katowice, Poland
View GitHub Profile
package com.example.kotlinpostapi.views
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.paging.PagedList
package com.example.kotlinpostapi
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlinpostapi.Posts.PostAdapter
import com.example.kotlinpostapi.Posts.PostViewModel
package com.example.kotlinpostapi.repository
import androidx.annotation.MainThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.toLiveData
import com.example.kotlinpostapi.Database.PostDao
import com.example.kotlinpostapi.apiObjects.Post
import com.example.kotlinpostapi.network.PostApiService
import androidx.lifecycle.switchMap
package com.example.kotlinpostapi.repository
import com.example.kotlinpostapi.apiObjects.Post
import com.example.kotlinpostapi.network.PostApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.lang.Exception
import com.example.kotlinpostapi.Result
import java.util.logging.Logger
@IamCostello
IamCostello / Navigation.kt
Created May 31, 2020 19:04
2# Refactor-after
package com.example.kotlinpostapi
import com.example.kotlinpostapi.apiObjects.Post
class Navigation {
interface OnUserClickListener{
fun onUserClick(userId: Int?)
}
interface OnPostClickListener{
@IamCostello
IamCostello / PostAdapter.kt
Created May 31, 2020 19:04
#2 Refactor-before
package com.example.kotlinpostapi.Posts
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.kotlinpostapi.apiObjects.Post
import com.example.kotlinpostapi.databinding.PostViewBinding
class PostAdapter(posts : List<Post>, onUserClickListener: OnUserClickListener) : RecyclerView.Adapter<PostAdapter.PostsViewHolder>(){
@IamCostello
IamCostello / BaseRepository.kt
Created May 31, 2020 18:18
#1 Refactor-after
package com.example.kotlinpostapi.repository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import com.example.kotlinpostapi.util.Result
open class BaseRepository() {
suspend fun<T: Any> handleApiCall(call: suspend () -> T, errMessage: String): Result<T> {
return try{
val response = withContext(Dispatchers.IO) { call.invoke() }
@IamCostello
IamCostello / PostRepository.kt
Created May 31, 2020 18:14
#1 Refactor-before
package com.example.kotlinpostapi.repository
import com.example.kotlinpostapi.apiObjects.Post
import com.example.kotlinpostapi.network.PostApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.lang.Exception
import com.example.kotlinpostapi.Result
import java.util.logging.Logger
@IamCostello
IamCostello / Consumer.java
Created March 28, 2020 15:20
Observer pattern in practice using PropertyChange
package com.company;
class Consumer{
private Repository repository;
public Consumer(Repository repository){
this.repository = repository;
}
public void consume(){
@IamCostello
IamCostello / Data.java
Created March 23, 2020 01:26
Observer Pattern
package com.company;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Data {
private String data;
private PropertyChangeSupport propertyChangeSupport;