Skip to content

Commit

Permalink
made more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
sidsharma2002 committed Sep 6, 2022
1 parent 64c26cd commit e19aa1e
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 150 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
plugins {
id 'kotlin-kapt'
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'

}

android {
Expand Down Expand Up @@ -46,6 +45,11 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation "androidx.room:room-runtime:2.4.3"
implementation "androidx.room:room-ktx:2.4.3"
kapt "androidx.room:room-compiler:2.4.3" // changing these can create db_impl not found exception
kapt "androidx.room:room-compiler-processing:2.4.3"

implementation(project(":spinwill"))

}
59 changes: 44 additions & 15 deletions app/src/main/java/com/example/spinwill/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,52 +1,74 @@
package com.example.spinwill

import android.annotation.SuppressLint
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.work.*
import com.example.spinwill.adapter.WillItemAdapter
import com.example.spinwill.cron.SpinWillWorker
import com.example.spinwill.cron.SpinWillWorkerFactory
import com.example.spinwill.database.local.SpinWillLocalDbImpl
import com.example.spinwill.di.SpinWillInjector
import com.example.spinwill.usecases.SpinWillBitmapLoadUseCaseImpl
import com.example.spinwill.utils.Resource
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import java.util.concurrent.TimeUnit

class MainActivity : AppCompatActivity() {

private lateinit var iv1: ImageView
private lateinit var iv2: ImageView
private val scope = CoroutineScope(Job())
private val injector by lazy {
SpinWillInjector<SpinWillItem>()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

AppWillRoomDatabase.getInstance(this.applicationContext)
setupDependencies()
// setupWork()
// setupWork()
setupUi()
}

private fun setupDependencies() {
SpinWillInjector.init(this, RemoteDatabaseImpl())
SpinWillInjector.setLocalDatabase(
SpinWillLocalDbImpl(SpinWillInjector.getSpinWillItemsDao())
// set context, remoteDb, localDb, dao, bitmap load use-case.
injector.init(this, RemoteDatabaseImpl())

val daoConc = SpinWillDaoConc()
daoConc.setDao(AppWillRoomDatabase.getInstance(this.applicationContext).getSpinWillDao())
injector.setLocalDatabase(
SpinWillLocalDbImpl(daoConc)
)

injector.setBitmapLoadUseCase(
SpinWillBitmapLoadUseCaseImpl(
this.applicationContext,
object : WillItemAdapter<SpinWillItem> {
override fun getRewardImageUrl(item: SpinWillItem): String {
return item.rewardImage
}

override fun setRewardBitmap(item: SpinWillItem, bitmap: Bitmap) {
item.rewardBitmap = bitmap
}
})
)
SpinWillInjector.setBitmapLoadUseCase(SpinWillBitmapLoadUseCaseImpl())
}

private fun setupUi() {
iv1 = findViewById(R.id.iv_1)
iv2 = findViewById(R.id.iv_2)

GlobalScope.launch {
scope.launch {

SpinWillInjector.getRepository().fetchAndUpdateWheelItem()
val result = SpinWillInjector.getRepository().loadBitmapAndSave()
injector.getRepository().fetchAndUpdateWheelItem()
val result = injector.getRepository().loadBitmapAndSave()

if (result is Resource.Success && result.data != null) {
val list = result.data!!
Expand All @@ -55,14 +77,14 @@ class MainActivity : AppCompatActivity() {
if (result.data!!.isNotEmpty()) {
Log.d("MainAct", "result " + list[0].toString())
runOnUiThread {
Log.d("spinwill","bitmap set 44 " + System.currentTimeMillis())
Log.d("spinwill", "bitmap set 44 " + System.currentTimeMillis())
iv1.setImageBitmap(result.data!![0].rewardBitmap)
}
}

if (result.data!!.size >= 2) {
runOnUiThread {
Log.d("spinwill","bitmap set 51 " + System.currentTimeMillis())
Log.d("spinwill", "bitmap set 51 " + System.currentTimeMillis())
iv2.setImageBitmap(result.data!![1].rewardBitmap)
}
}
Expand All @@ -84,14 +106,21 @@ class MainActivity : AppCompatActivity() {
.build()
).addTag("task_update").build()

Configuration.Builder().setMinimumLoggingLevel(Log.DEBUG).build()
Configuration.Builder()
.setMinimumLoggingLevel(Log.DEBUG)
.setWorkerFactory(SpinWillWorkerFactory(injector.getRepository()))
.build()

WorkManager.getInstance(this)
.enqueueUniquePeriodicWork(
"task_update",
ExistingPeriodicWorkPolicy.REPLACE,
workRequest
)
}

override fun onDestroy() {
super.onDestroy()
scope.coroutineContext.cancelChildren()
}
}
5 changes: 2 additions & 3 deletions app/src/main/java/com/example/spinwill/RemoteDatabaseImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package com.example.spinwill

import android.util.Log
import com.example.spinwill.database.remote.SpinWillRemoteDatabase
import com.example.spinwill.models.SpinWillItem
import com.example.spinwill.utils.Resource
import kotlinx.coroutines.delay

class RemoteDatabaseImpl : SpinWillRemoteDatabase {
class RemoteDatabaseImpl : SpinWillRemoteDatabase<SpinWillItem> {
override suspend fun fetch(): Resource<List<SpinWillItem>> {
Log.d("App", "23")
val list = mutableListOf<SpinWillItem>()
Expand All @@ -20,7 +19,7 @@ class RemoteDatabaseImpl : SpinWillRemoteDatabase {
)
)

delay(2000)
delay(1000L)

list.add(
SpinWillItem(
Expand Down
83 changes: 83 additions & 0 deletions app/src/main/java/com/example/spinwill/RoomDb.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example.spinwill

import android.content.Context
import androidx.room.*
import com.example.spinwill.database.local.SpinWillDbActions

@Database(entities = [SpinWillItem::class], version = 1, exportSchema = false)
abstract class AppWillRoomDatabase : RoomDatabase() {

abstract fun getSpinWillDao(): SpinWillDaoImpl

companion object {
@Volatile
private var instance: AppWillRoomDatabase? = null

@JvmStatic
fun getInstance(context: Context): AppWillRoomDatabase {
return instance ?: synchronized(this) {
instance ?: buildDatabase(context).also {
instance = it
}
}
}

// Create the database. See this article for more details:
// https://medium.com/google-developers/7-pro-tips-for-room-fbadea4bfbd1#4785
private fun buildDatabase(context: Context): AppWillRoomDatabase {
return Room.databaseBuilder(
context,
AppWillRoomDatabase::class.java,
"SPINWILL_DB_123"
).fallbackToDestructiveMigration().build()
}
}
}


@Dao
interface SpinWillDaoImpl {
@Query("SELECT * FROM spinwill_item")
suspend fun getAllItems(): List<SpinWillItem>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(willItems: List<SpinWillItem>)

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(willItem: SpinWillItem)

@Delete
suspend fun delete(willItem: SpinWillItem)

@Query("DELETE FROM spinwill_item")
suspend fun deleteAll()
}

class SpinWillDaoConc : SpinWillDbActions<SpinWillItem> {

private lateinit var dao: SpinWillDaoImpl

fun setDao(dao: SpinWillDaoImpl) {
this.dao = dao
}

override suspend fun getAllItems(): List<SpinWillItem> {
return dao.getAllItems()
}

override suspend fun insert(willItem: SpinWillItem) {
return dao.insert(willItem)
}

override suspend fun insertAll(willItems: List<SpinWillItem>) {
return dao.insertAll(willItems)
}

override suspend fun delete(willItem: SpinWillItem) {
return dao.delete(willItem)
}

override suspend fun deleteAll() {
return dao.deleteAll()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.spinwill.models
package com.example.spinwill

import android.graphics.Bitmap
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
Expand All @@ -16,4 +15,4 @@ data class SpinWillItem(
var rewardImage: String = "",
@Ignore
var rewardBitmap: Bitmap? = null
)
)
6 changes: 3 additions & 3 deletions spinwill/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "androidx.work:work-runtime-ktx:2.7.1"

implementation "androidx.room:room-runtime:2.4.3"
implementation "androidx.room:room-ktx:2.4.3"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
// implementation "androidx.room:room-runtime:2.4.3"
// implementation "androidx.room:room-ktx:2.4.3"
// annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'androidx.annotation:annotation:1.4.0'
Expand Down
17 changes: 16 additions & 1 deletion spinwill/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.spinwill">

<application
Expand All @@ -8,6 +9,20 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SpinWill" />
android:theme="@style/Theme.SpinWill" >

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<!-- If you are using androidx.startup to initialize other components -->
<meta-data
android:name="androidx.work.WorkManagerInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.spinwill.adapter

import android.graphics.Bitmap

interface WillItemAdapter<item> {
fun getRewardImageUrl(item: item): String
fun setRewardBitmap(item: item, bitmap: Bitmap)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.spinwill.config

object SpinWillConfig {
private var EXPIRE_TIME: Int = 3 * 60 // 3 hours by default
val SPINWILL_DB = "spinwill_database"

/**
* time in which the data expires and needs to be refreshed,
Expand Down
26 changes: 22 additions & 4 deletions spinwill/src/main/java/com/example/spinwill/cron/SpinWillWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.work.CoroutineWorker
import androidx.work.ListenableWorker
import androidx.work.WorkerFactory
import androidx.work.WorkerParameters
import com.example.spinwill.di.SpinWillInjector
import com.example.spinwill.repository.SpinWillRepository
import com.example.spinwill.utils.Resource

class SpinWillWorker constructor(
class SpinWillWorker<item> constructor(
private val context: Context,
workerParameters: WorkerParameters
workerParameters: WorkerParameters,
private val repository: SpinWillRepository<item>
) : CoroutineWorker(context, workerParameters) {

private val repository by lazy { SpinWillInjector.getRepository() }

override suspend fun doWork(): Result {
Log.d("spinwill", "doWork")
Expand All @@ -37,4 +40,19 @@ class SpinWillWorker constructor(
else
Result.retry()
}
}
}


class SpinWillWorkerFactory<item> constructor(private val repository: SpinWillRepository<item>) :
WorkerFactory() {

override fun createWorker(
appContext: Context,
workerClassName: String,
workerParameters: WorkerParameters
): ListenableWorker? {
// This only handles a single Worker, please don’t do this!!
// See below for a better way using DelegatingWorkerFactory
return SpinWillWorker(appContext, workerParameters, repository)
}
}
Loading

0 comments on commit e19aa1e

Please sign in to comment.