Skip to content

Commit 1fa09d2

Browse files
Merge pull request #36 from agrawalsuneet/loader
Fixed Context reference issue
2 parents db5993e + 0e2f5d2 commit 1fa09d2

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Check all other loaders [here](https://agrawalsuneet.github.io/agrawalsuneet/ope
3232
## How To use
3333
include below dependency in build.gradle of application and compile it
3434
```
35-
implementation 'com.agrawalsuneet.androidlibs:loaderspack:1.2.2'
35+
implementation 'com.agrawalsuneet.androidlibs:loaderspack:1.2.3'
3636
```
3737

3838

app/src/main/java/com/agrawalsuneet/loaders/MainActivity.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ import android.os.Bundle
44
import android.os.Handler
55
import android.support.v4.content.ContextCompat
66
import android.support.v7.app.AppCompatActivity
7-
import android.view.View
87
import android.view.animation.Animation
98
import android.view.animation.BounceInterpolator
109
import android.view.animation.LinearInterpolator
1110
import android.view.animation.RotateAnimation
1211
import android.widget.LinearLayout
1312
import com.agrawalsuneet.loaderspack.loaders.*
14-
import kotlinx.android.synthetic.main.main_curves.*
1513

1614
class MainActivity : AppCompatActivity() {
1715

1816
lateinit var containerLayout: LinearLayout
1917

2018
override fun onCreate(savedInstanceState: Bundle?) {
2119
super.onCreate(savedInstanceState)
22-
setContentView(R.layout.main_curves)
20+
setContentView(R.layout.activity_main_circular)
2321

2422
supportActionBar?.setTitle("CurvesLoader")
2523

loaderspack/build.gradle

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ repositories {
88
ext {
99
PUBLISH_GROUP_ID = 'com.agrawalsuneet.androidlibs'
1010
PUBLISH_ARTIFACT_ID = 'loaderspack'
11-
PUBLISH_VERSION = '1.2.2'
11+
PUBLISH_VERSION = '1.2.3'
1212
}
1313

1414
android {
1515
compileSdkVersion 28
16-
buildToolsVersion '28.0.2'
17-
1816

1917
defaultConfig {
2018
minSdkVersion 16
2119
targetSdkVersion 28
22-
versionCode 14
23-
versionName "1.2.2"
20+
versionCode 15
21+
versionName "1.2.3"
2422
}
2523

2624
buildTypes {

loaderspack/src/main/java/com/agrawalsuneet/loaderspack/loaders/CircularSticksLoader.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.view.View
99
import com.agrawalsuneet.dotsloader.utils.Helper
1010
import com.agrawalsuneet.loaderspack.R
1111
import com.agrawalsuneet.loaderspack.contracts.CircularSticksAbstractView
12+
import com.agrawalsuneet.loaderspack.utils.Utils
1213
import java.util.*
1314

1415
/**
@@ -73,6 +74,9 @@ class CircularSticksLoader : CircularSticksAbstractView {
7374

7475
private var timer: Timer? = null
7576

77+
var isAnimating: Boolean = false
78+
private set
79+
7680
constructor(context: Context) : super(context) {
7781
initPaints()
7882
}
@@ -136,7 +140,8 @@ class CircularSticksLoader : CircularSticksAbstractView {
136140

137141
if (visibility != VISIBLE) {
138142
timer?.cancel()
139-
} else if(shouldAnimate) {
143+
isAnimating = false
144+
} else if (shouldAnimate) {
140145
scheduleTimer()
141146
}
142147
}
@@ -210,6 +215,7 @@ class CircularSticksLoader : CircularSticksAbstractView {
210215
fun stopAnimation() {
211216
shouldAnimate = false
212217
timer?.cancel()
218+
isAnimating = false
213219
}
214220

215221
private fun scheduleTimer() {
@@ -222,8 +228,10 @@ class CircularSticksLoader : CircularSticksAbstractView {
222228
selectedStickPos = 1
223229
}
224230

225-
(context as Activity).runOnUiThread { invalidate() }
231+
(Utils.scanForActivity(context))?.runOnUiThread { invalidate() }
226232
}
227233
}, 0, animDuration.toLong())
234+
235+
isAnimating = true
228236
}
229237
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.agrawalsuneet.loaderspack.utils
2+
3+
import android.content.ContextWrapper
4+
import android.app.Activity
5+
import android.content.Context
6+
7+
8+
object Utils {
9+
10+
fun scanForActivity(context: Context?): Activity? {
11+
return when (context) {
12+
null -> null
13+
is Activity -> context
14+
is ContextWrapper -> scanForActivity(context.baseContext)
15+
else -> null
16+
}
17+
18+
}
19+
20+
}

0 commit comments

Comments
 (0)