Last active
April 27, 2020 01:27
-
-
Save Iamsdt/b11440ed910f678f83b275afc00a27d2 to your computer and use it in GitHub Desktop.
Dependency Injection with KOIN to Androidx (WorkManager and ViewModel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
// Koin for Android | |
implementation 'org.koin:koin-android:0.9.3' | |
implementation "org.koin:koin-androidx-viewmodel:1.0.0-beta-3" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.lifecycle.Observer | |
import kotlinx.android.synthetic.main.activity_main.* | |
import org.koin.androidx.viewmodel.ext.android.viewModel | |
class MainActivity : AppCompatActivity() { | |
private val viewModel: MainVM by viewModel() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setSupportActionBar(toolbar) | |
viewModel.getData().observe(this, Observer { | |
//observe data | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.ViewModel | |
import com.iamsdt.myapplication.db.WordTableDao | |
class MainVM(private val wordTableDao: WordTableDao):ViewModel(){ | |
fun getData() = wordTableDao.getRandomData() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Application | |
import org.koin.android.ext.android.startKoin | |
class MyApp:Application(){ | |
override fun onCreate() { | |
super.onCreate() | |
startKoin(this, listOf(dbModule, vmModule)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.room.Database | |
import androidx.room.RoomDatabase | |
@Database(entities = [WordTable::class], version = 1, | |
exportSchema = false) | |
abstract class MyDatabase:RoomDatabase(){ | |
abstract val wordTableDao: WordTableDao | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Application | |
import androidx.room.Room | |
import com.iamsdt.myapplication.db.MyDatabase | |
import com.iamsdt.myapplication.db.WordTableDao | |
import org.koin.android.ext.koin.androidContext | |
import org.koin.androidx.viewmodel.ext.koin.viewModel | |
import org.koin.dsl.module.module | |
val dbModule = module { | |
single { | |
Room.databaseBuilder(androidContext(), MyDatabase::class.java, | |
"SS").build() | |
} | |
single { get<MyDatabase>().wordTableDao } | |
} | |
val vmModule = module { | |
viewModel { | |
MainVM(get() as WordTableDao) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.work.Worker | |
import com.iamsdt.myapplication.db.WordTable | |
import com.iamsdt.myapplication.db.WordTableDao | |
import org.koin.standalone.KoinComponent | |
import org.koin.standalone.inject | |
class MyWorker : Worker(), KoinComponent { | |
val wordTableDao: WordTableDao by inject() | |
override fun doWork(): Result { | |
val table = WordTable() | |
wordTableDao.add(table) | |
return Result.SUCCESS | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.room.Entity | |
import androidx.room.PrimaryKey | |
@Entity | |
class WordTable( | |
@PrimaryKey(autoGenerate = true) | |
var id: Int = 0, | |
var word: String = "", | |
var des: String = "", | |
var bookmark: Boolean = false, | |
var addByUser: Boolean = false, | |
var uploaded:Boolean = false | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.LiveData | |
import androidx.room.* | |
@Dao | |
interface WordTableDao { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
fun add(table: WordTable): Long | |
@Update | |
fun update(table: WordTable): Int | |
@Delete | |
fun delete(pageTable: WordTable): Int | |
@Query("Select * From WordTable") | |
fun getAllList(): List<WordTable> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment