Skip to content

Instantly share code, notes, and snippets.

View skydoves's full-sized avatar
💡
Practice is the only shortcut

Jaewoong Eum skydoves

💡
Practice is the only shortcut
View GitHub Profile
@skydoves
skydoves / SearchActivityViewModel.kt
Created March 15, 2018 14:53
SearchActivityViewModel
class SearchActivityViewModel @Inject
constructor(private val githubUserRepository: GithubUserRepository, private val historyRepository: HistoryRepository): ViewModel() {
val login: MutableLiveData<String> = MutableLiveData()
var githubUserLiveData: LiveData<Resource<GithubUser>> = MutableLiveData()
val historiesLiveData: MutableLiveData<List<History>> = MutableLiveData()
val toast: MutableLiveData<String> = MutableLiveData()
init {
@skydoves
skydoves / InjectionExampleMainActivity.kt
Last active March 15, 2018 15:05
InjectionExampleMainActivity
@Inject lateinit var viewModelFactory: AppViewModelFactory
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(MainActivityViewModel::class.java) }
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
@skydoves
skydoves / History.kt
Created March 16, 2018 01:35
History Model
@Entity(tableName = "SearchHistory")
data class History(
@PrimaryKey val search: String,
val history: Long
)
@skydoves
skydoves / HistoryDao.kt
Created March 16, 2018 01:37
HistoryDao
@Dao
interface HistoryDao {
@Query("SELECT* FROM SearchHistory ORDER BY history DESC LIMIT 20")
fun selectRecentHistoryList(): LiveData<List<History>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertHistory(history: History)
@Query("DELETE FROM SearchHistory WHERE search = :search")
fun deleteHistory(search: String)
@skydoves
skydoves / AppDatabase.kt
Created March 16, 2018 01:39
AppDatabase
@Database(entities = [(History::class), (Follower::class), (GithubUser::class)], version = 2)
abstract class AppDatabase: RoomDatabase() {
abstract fun historyDao(): HistoryDao
abstract fun githubUserDao(): GithubUserDao
abstract fun followersDao(): FollowersDao
}
@skydoves
skydoves / loadFromDb.kt
Created March 16, 2018 01:43
loadFromDb
override
fun loadFromDb(): LiveData<List<Follower>> {
return followersDao.getFollowers(user, page, isFollowers)
}
@skydoves
skydoves / GithubUserRepository.kt
Last active March 16, 2018 01:59
GithubUserRepository
@Singleton
class GithubUserRepository @Inject
constructor(val githubUserDao: GithubUserDao, val service: GithubService) {
@InjectPreference lateinit var profile: Preference_UserProfile
init {
Timber.d("Injection GithubUserRepository")
PreferenceComponent_PrefAppComponent.getInstance().inject(this)
}
@skydoves
skydoves / Profile.kt
Created March 16, 2018 02:08
Profile Entity
@PreferenceEntity(name = "UserProfile")
open class Profile {
@KeyName(name = "name") @JvmField val userName = "skydoves"
@KeyName(name = "menuPosition") @JvmField val selectedPosition = 0
}
@skydoves
skydoves / PrefAppComponent.kt
Created March 16, 2018 02:09
PrefAppComponent
@PreferenceComponent(entities = [(Profile::class)])
interface PrefAppComponent {
fun inject(target: SearchActivityViewModel)
fun inject(target: DetailActivityViewModel)
fun inject(target: GithubUserRepository)
}
@skydoves
skydoves / PreferenceComponent_PrefAppComponent.kt
Created March 16, 2018 02:13
PreferenceComponent_PrefAppComponent
@InjectPreference lateinit var profile: Preference_UserProfile
init {
Timber.d("Injection GithubUserRepository")
PreferenceComponent_PrefAppComponent.getInstance().inject(this)
}