Skip to content

Commit

Permalink
Merge pull request #3 from theozgurr/cancel_scheduled_work_impl
Browse files Browse the repository at this point in the history
Cancel scheduled work impl
  • Loading branch information
10zgurr authored Mar 27, 2022
2 parents 6ccf878 + e67911b commit 147fa50
Show file tree
Hide file tree
Showing 21 changed files with 541 additions and 129 deletions.
7 changes: 7 additions & 0 deletions .idea/misc.xml

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Add it in your root build.gradle at the end of repositories:
Add it in the your app dependencies:
</br>
<pre>dependencies {
implementation 'com.github.theozgurr:NotificationMan:1.0.6'
implementation 'com.github.theozgurr:NotificationMan:1.0.7'
}</pre>
</br>

Expand Down
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ android {

defaultConfig {
applicationId "com.notification.man"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 31
versionCode 1
versionName "1.0.6"
versionName "1.0.7"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'

implementation project(':notificationmanlib')
}
48 changes: 36 additions & 12 deletions app/src/main/java/com/notification/man/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.notification.man

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.notification.man.databinding.ActivityMainBinding
import com.notificationman.library.NotificationMan
import com.notificationman.library.NotificationTypes
import com.notificationman.library.model.NotificationTypes
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

Expand All @@ -21,22 +23,44 @@ class MainActivity : AppCompatActivity() {
setContentView(binding.root)

binding.buttonFire.setOnClickListener {
fireNotificationMan()
val classPath = "com.notification.man.MainActivity"
val title = binding.editTextTitle.text.toString().trim()
val desc = binding.editTextDesc.text.toString().trim()
val timeInterval = binding.editTextTimeInterval.text.toString().trim().toLongOrNull()
fireNotificationMan(
classPath = classPath,
title = title,
desc = desc,
timeInterval = timeInterval
)
}

binding.buttonCoolDownLast.setOnClickListener {
lifecycleScope.launch {
NotificationMan.coolDownLatestFire(this@MainActivity)
}
}

binding.buttonCoolDownAll.setOnClickListener {
lifecycleScope.launch {
NotificationMan.coolDownAllFires(this@MainActivity)
}
}
}

private fun fireNotificationMan() {
val classPath = "com.notification.man.MainActivity" // make sure class path match with your project architecture
val title = binding.editTextTitle.text.toString().trim()
val desc = binding.editTextDesc.text.toString().trim()
val timeInterval = binding.editTextTimeInterval.text.toString().trim().toLongOrNull()
private fun fireNotificationMan(
classPath: String,
title: String?,
desc: String?,
timeInterval: Long?,
) {
NotificationMan
.Builder(this, classPath)
.setTitle(title) // optional
.setDescription(desc) // optional
.setThumbnailUrl(THUMBNAIL_URL) // optional
.setTimeInterval(timeInterval) // needs secs - default is 5 secs
.setNotificationType(NotificationTypes.IMAGE.type) // optional - default type is TEXT
.setTitle(title)
.setDescription(desc)
.setThumbnailUrl(THUMBNAIL_URL)
.setTimeInterval(timeInterval)
.setNotificationType(NotificationTypes.IMAGE.type)
.fire()
}
}
22 changes: 20 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_margin="16dp"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatEditText
Expand Down Expand Up @@ -37,10 +37,28 @@
android:id="@+id/button_fire"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginTop="24dp"
android:text="Fire"
android:textColor="@android:color/black" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_cool_down_last"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="Cool Down Last"
android:textAllCaps="false"
android:textColor="@android:color/black" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_cool_down_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Cool Down All"
android:textAllCaps="false"
android:textColor="@android:color/black" />

</LinearLayout>

</FrameLayout>
25 changes: 23 additions & 2 deletions notificationmanlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
buildToolsVersion '30.0.3'

defaultConfig {
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 31

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -28,6 +28,18 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

testOptions {
animationsDisabled = true
}

packagingOptions {
exclude 'win32-x86/attach_hotspot_windows.dll'
exclude 'win32-x86-64/attach_hotspot_windows.dll'
exclude 'META-INF/licenses/ASM'
exclude 'META-INF/AL2.0'
exclude 'META-INF/LGPL2.1'
}
}

dependencies {
Expand All @@ -36,6 +48,9 @@ dependencies {
// kotlin stdlib
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

// coroutines test
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2'

// androidx
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
Expand All @@ -45,7 +60,13 @@ dependencies {
// junit
testImplementation 'junit:junit:4.13.2'

// WorkManager
// work manager
implementation 'androidx.work:work-runtime-ktx:2.7.1'
androidTestImplementation 'androidx.work:work-testing:2.7.1'

// data store
implementation 'androidx.datastore:datastore-preferences:1.0.0'

// gson
implementation 'com.google.code.gson:gson:2.9.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.notificationman.library.datastore

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.preferencesDataStoreFile
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.notificationman.library.extensions.getRandomUUID
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.test.*
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.Is.`is`
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class AppDataStoreImplTest {

companion object {
private const val NOTIFICATION_MAN_FAKE_PREFERENCES = "notification_man_fake_preferences"
}

private val testContext: Context = ApplicationProvider.getApplicationContext()
private val testCoroutineDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher + Job())
private val testDataStore: DataStore<Preferences> =
PreferenceDataStoreFactory.create(
scope = testCoroutineScope,
produceFile = { testContext.preferencesDataStoreFile(NOTIFICATION_MAN_FAKE_PREFERENCES) }
)
private lateinit var appDataStoreImpl: AppDataStoreImpl

@Before
fun setUp() {
Dispatchers.setMain(testCoroutineDispatcher)
appDataStoreImpl = AppDataStoreImpl(testDataStore)
}

@Test
fun test_appDataStoreImpl_saveWorkerId() {
testCoroutineScope.runBlockingTest {
with(appDataStoreImpl) {
val id = getRandomUUID()
saveWorkerId(id)
val actual = getFirstWorkerId()
assertThat(actual, `is`(id))
}
}
}

@Test
fun test_appDataStoreImpl_deleteWorkerId() {
testCoroutineScope.runBlockingTest {
with(appDataStoreImpl) {
val id1 = getRandomUUID()
val id2 = getRandomUUID()
val id3 = getRandomUUID()
saveWorkerId(id1)
saveWorkerId(id2)
saveWorkerId(id3)
deleteWorkerId(id2)
val actual = getFirstWorkerId()
assertThat(actual, `is`(id1))
}
}
}

@Test
fun test_appDataStoreImpl_deleteAllWorkerIds() {
testCoroutineScope.runBlockingTest {
with(appDataStoreImpl) {
val id = getRandomUUID()
saveWorkerId(id)
deleteAllWorkerIds()
val actual = getWorkerIds()
assertThat(actual, `is`(emptyList()))
}
}
}

@Test
fun test_appDataStoreImpl_getWorkerIds() {
testCoroutineScope.runBlockingTest {
with(appDataStoreImpl) {
val id = getRandomUUID()
saveWorkerId(id)
val actual = getWorkerIds().size
assertThat(actual, `is`(1))
}
}
}

@Test
fun test_appDataStoreImpl_getFirstWorkerId() {
testCoroutineScope.runBlockingTest {
with(appDataStoreImpl) {
val id = getRandomUUID()
saveWorkerId(id)
val actual = getFirstWorkerId()
assertThat(actual, `is`(id))
}
}
}


@After
fun cleanUp() {
Dispatchers.resetMain()
testCoroutineDispatcher.cleanupTestCoroutines()
testCoroutineScope.runBlockingTest {
testDataStore.edit { it.clear() }
}
testCoroutineScope.cancel()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.notificationman.library.extensions

import java.util.*

fun getRandomUUID(): UUID = UUID.randomUUID()
Loading

0 comments on commit 147fa50

Please sign in to comment.