Skip to content

Commit

Permalink
Some refactoring, added bundle delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
armcha committed Oct 17, 2017
1 parent e4fbb2a commit c3a0525
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 110 deletions.
4 changes: 2 additions & 2 deletions app/src/main/kotlin/io/armcha/ribble/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import com.squareup.leakcanary.LeakCanary
import io.armcha.ribble.di.component.ApplicationComponent
import io.armcha.ribble.di.component.DaggerApplicationComponent
import io.armcha.ribble.di.module.ApplicationModule
import io.armcha.ribble.presentation.utils.extensions.nonSafeLazy
import io.armcha.ribble.presentation.utils.extensions.unSafeLazy

/**
* Created by Chatikyan on 29.07.2017.
*/
class App : Application() {

val applicationComponent: ApplicationComponent by nonSafeLazy {
val applicationComponent: ApplicationComponent by unSafeLazy {
DaggerApplicationComponent.builder()
.applicationModule(ApplicationModule(this))
.build()
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/io/armcha/ribble/data/pref/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.content.Context
import android.content.SharedPreferences
import io.armcha.ribble.data.network.ApiConstants
import io.armcha.ribble.presentation.utils.extensions.emptyString
import io.armcha.ribble.presentation.utils.extensions.nonSafeLazy
import io.armcha.ribble.presentation.utils.extensions.unSafeLazy
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -21,7 +21,7 @@ class Preferences @Inject constructor(app: Application) {
private val USER_LOGGED_IN = "user_logged_in"
private val USER_TOKEN = "user_token"

private val sharedPreferences by nonSafeLazy {
private val sharedPreferences by unSafeLazy {
app.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE)
}
val isUserLoggedIn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.armcha.ribble.di.module.ActivityModule
import io.armcha.ribble.presentation.navigation.Navigator
import io.armcha.ribble.presentation.utils.S
import io.armcha.ribble.presentation.utils.extensions.emptyString
import io.armcha.ribble.presentation.utils.extensions.nonSafeLazy
import io.armcha.ribble.presentation.utils.extensions.unSafeLazy
import io.armcha.ribble.presentation.widget.MaterialDialog
import javax.inject.Inject

Expand All @@ -26,7 +26,7 @@ abstract class BaseActivity<V : BaseContract.View, P : BaseContract.Presenter<V>

private var dialog: MaterialDialog? = null

val activityComponent: ActivityComponent by nonSafeLazy {
val activityComponent: ActivityComponent by unSafeLazy {
getAppComponent().plus(ActivityModule(this))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.armcha.ribble.presentation.screen.shot_detail

import android.animation.StateListAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.v7.widget.LinearLayoutManager
Expand All @@ -17,6 +16,7 @@ import io.armcha.ribble.presentation.base_mvp.base.BaseFragment
import io.armcha.ribble.presentation.utils.C
import io.armcha.ribble.presentation.utils.L
import io.armcha.ribble.presentation.utils.S
import io.armcha.ribble.presentation.utils.delegates.args
import io.armcha.ribble.presentation.utils.extensions.*
import io.armcha.ribble.presentation.utils.glide.TransformationType
import io.armcha.ribble.presentation.utils.glide.load
Expand All @@ -29,9 +29,7 @@ import javax.inject.Inject
class ShotDetailFragment : BaseFragment<ShotDetailContract.View, ShotDetailContract.Presenter>(), ShotDetailContract.View {

companion object {
const val SHOT_EXTRA_KEY = "shot_extra_key"

fun getBundle(shot: Shot?) = Bundle().apply { putParcelable(SHOT_EXTRA_KEY, shot) }
fun getBundle(shot: Shot?) = Bundle().apply { putParcelable("shot", shot) }
}

private val items = intArrayOf(R.drawable.heart_full, R.drawable.eye, R.drawable.bucket)
Expand All @@ -40,7 +38,8 @@ class ShotDetailFragment : BaseFragment<ShotDetailContract.View, ShotDetailContr
protected lateinit var shotDetailPresenter: ShotDetailPresenter

private var recyclerAdapter: RibbleAdapter<Comment>? = null
private lateinit var shot: Shot

val shot: Shot by args()

override fun injectDependencies() {
activityComponent.inject(this)
Expand All @@ -50,11 +49,6 @@ class ShotDetailFragment : BaseFragment<ShotDetailContract.View, ShotDetailContr

override fun initPresenter() = shotDetailPresenter

override fun onAttach(context: Context?) {
super.onAttach(context)
shot = this extraWithKey SHOT_EXTRA_KEY
}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpViews()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.armcha.ribble.presentation.utils.delegates

import android.support.v4.app.Fragment
import io.armcha.ribble.presentation.utils.extensions.isNull
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

/**
* Created by Chatikyan on 30.09.2017.
*/


inline fun <reified VALUE> Fragment.args() = object : ReadOnlyProperty<Fragment, VALUE> {

private var value: VALUE? = null

override fun getValue(thisRef: Fragment, property: KProperty<*>): VALUE {
if (value.isNull) {
value = arguments[property.name] as VALUE
}
return value!!
}
}


This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@ import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Parcelable
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.text.Html
import android.text.Spanned
import android.widget.Toast
import io.armcha.ribble.App
import io.armcha.ribble.presentation.base_mvp.base.BaseFragment
import io.armcha.ribble.presentation.base_mvp.base.BasePresenter
import io.armcha.ribble.presentation.utils.Experimental
import java.io.Serializable
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


/**
Expand Down Expand Up @@ -83,10 +76,17 @@ fun Int.toPx(context: Context): Int {
return (this * density).toInt()
}

fun <T> nonSafeLazy(initializer: () -> T): Lazy<T> {
fun <T> unSafeLazy(initializer: () -> T): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) {
initializer()
}
}

fun Int.isZero(): Boolean = this == 0

inline fun <F, S> doubleWith(first: F, second: S, runWith: F.(S) -> Unit) {
first.runWith(second)
}

val Any?.isNull: Boolean
get() = this == null
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import android.util.AttributeSet
* Created by Chatikyan on 16.02.2017.
*/

class AnimatedImageView : AppCompatImageView, AnimatedView {

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
class AnimatedImageView(context: Context, attrs: AttributeSet? = null)
: AppCompatImageView(context, attrs), AnimatedView {

fun setAnimatedImage(newImage: Int, startDelay: Long = 0L) {
changeImage(newImage, startDelay)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package io.armcha.ribble.presentation.widget

import android.content.Context
import android.support.v7.widget.AppCompatImageView
import android.support.v7.widget.AppCompatTextView
import android.util.AttributeSet

/**
* Created by Chatikyan on 16.02.2017.
*/

class AnimatedTextView : AppCompatTextView, AnimatedView {

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
class AnimatedTextView (context: Context, attrs: AttributeSet? = null)
: AppCompatTextView(context, attrs), AnimatedView {

fun setAnimatedText(text: CharSequence, startDelay: Long = 0L) {
changeText(text, startDelay)
Expand Down
24 changes: 12 additions & 12 deletions app/src/main/kotlin/io/armcha/ribble/presentation/widget/ArcView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.view.View
import io.armcha.ribble.R
import io.armcha.ribble.presentation.utils.extensions.takeColor

class ArcView constructor(context: Context, attrs: AttributeSet) : View(context, attrs) {
class ArcView constructor(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {

private val SHADOW_OFFSET = 15F
private val START_ANGLE = 270F
Expand All @@ -31,16 +31,16 @@ class ArcView constructor(context: Context, attrs: AttributeSet) : View(context,
}

override fun onDraw(canvas: Canvas) {
val width = width.toFloat() - SHADOW_OFFSET
val height = height.toFloat() - SHADOW_OFFSET
rect.set(width / arcStartPoint, SHADOW_OFFSET, width, height)
with(path) {
val halfWidth = width / 2
lineTo(halfWidth + halfWidth / arcStartPoint, SHADOW_OFFSET)
addArc(rect, START_ANGLE, SWEEP_ANGLE)
lineTo(0F, height)
lineTo(0F, SHADOW_OFFSET)
}
canvas.drawPath(path, paint)
val width = width.toFloat() - SHADOW_OFFSET
val height = height.toFloat() - SHADOW_OFFSET
rect.set(width / arcStartPoint, SHADOW_OFFSET, width, height)
with(path) {
val halfWidth = width / 2
lineTo(halfWidth + halfWidth / arcStartPoint, SHADOW_OFFSET)
addArc(rect, START_ANGLE, SWEEP_ANGLE)
lineTo(0F, height)
lineTo(0F, SHADOW_OFFSET)
}
canvas.drawPath(path, paint)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@ import android.graphics.Color
import android.graphics.Paint
import android.support.v7.widget.AppCompatImageView
import android.util.AttributeSet
import io.armcha.ribble.R
import io.armcha.ribble.presentation.utils.extensions.toPx

/**
* Created by Chatikyan on 27.08.2017.
*/
class CircleLinedImageView : AppCompatImageView {
class CircleLinedImageView(context: Context, attrs: AttributeSet? = null)
: AppCompatImageView(context, attrs) {

private val LINE_WIDTH = 3F

private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private var padding = 17

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

init {
with(paint) {
color = Color.LTGRAY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import io.armcha.ribble.presentation.utils.extensions.show
/**
* Created by Chatikyan on 04.09.2017.
*/
class CircleProgressView : View, Animatable {
class CircleProgressView(context: Context, attrs: AttributeSet) : View(context, attrs), Animatable {

private val MAX_VALUE = 360F
private val END_VALUE = 280F
Expand Down Expand Up @@ -53,10 +53,6 @@ class CircleProgressView : View, Animatable {
}
get() = resources.getDimension(field).toInt()

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

init {
if (!isInEditMode)
hide()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import android.view.animation.GridLayoutAnimationController
/**
* Created by Chatikyan on 17.09.2017.
*/
class GridRecyclerView : RecyclerView {

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
class GridRecyclerView (context: Context, attrs: AttributeSet) : RecyclerView(context, attrs) {

override fun attachLayoutAnimationParameters(child: View, params: ViewGroup.LayoutParams,
index: Int, count: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import android.widget.Button
import android.widget.TextView
import io.armcha.ribble.R
import io.armcha.ribble.presentation.utils.AnimationUtils
import io.armcha.ribble.presentation.utils.extensions.nonSafeLazy
import io.armcha.ribble.presentation.utils.extensions.unSafeLazy
import io.armcha.ribble.presentation.utils.extensions.onClick
import io.armcha.ribble.presentation.utils.extensions.scale
import kotlinx.android.synthetic.main.dialog_item.*
Expand All @@ -20,13 +20,13 @@ import kotlinx.android.synthetic.main.dialog_item.*
*/
class MaterialDialog(context: Context) : Dialog(context, R.style.MaterialDialogSheet) {

private val titleText by nonSafeLazy {
private val titleText by unSafeLazy {
findViewById<TextView>(R.id.title)
}
private val messageText by nonSafeLazy {
private val messageText by unSafeLazy {
findViewById<TextView>(R.id.message)
}
private val positiveButton by nonSafeLazy {
private val positiveButton by unSafeLazy {
findViewById<Button>(R.id.positiveButton)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.util.AttributeSet
* Created by Chatikyan on 08.08.2017.
*/

class SquareCardView constructor(context: Context, attrs: AttributeSet) : CardView(context, attrs) {
class SquareCardView constructor(context: Context, attrs: AttributeSet? = null) : CardView(context, attrs) {

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.armcha.ribble.presentation.utils.extensions.takeColor
/**
* Created by Chatikyan on 29.08.2017.
*/
class TextImageLayout : LinearLayout {
class TextImageLayout(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

private val circleImage: CircleLinedImageView
private val textView: TextView
Expand All @@ -32,10 +32,6 @@ class TextImageLayout : LinearLayout {
DrawableCompat.setTint(wrap, context.takeColor(value))
}

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

init {
orientation = VERTICAL
gravity = Gravity.CENTER_HORIZONTAL
Expand Down
Loading

0 comments on commit c3a0525

Please sign in to comment.