Skip to content

Commit 7dc0929

Browse files
author
ErikJhordan
committedMay 10, 2018
Improve null safety
1 parent 16615ba commit 7dc0929

File tree

13 files changed

+23
-42
lines changed

13 files changed

+23
-42
lines changed
 

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/data/remote/RemoteContract.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RemoteContract {
3131
const val SUCCESS = "success"
3232
const val QUOTES = "quotes"
3333

34-
// I should't expose the access key but it is to didactic use
34+
// I shouldn't expose the access key but it is to didactic use
3535
const val ACCESS_KEY_API_LAYER = "be4554e86f3a5670b287ccc40f5bead8"
3636
const val FORMAT_TYPE = "1"
3737
}

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/data/repository/CurrencyRepository.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CurrencyRepository @Inject constructor(
5353
.observeOn(AndroidSchedulers.mainThread())
5454
.subscribe({ currencyList ->
5555
mutableLiveData.value = transform(currencyList)
56-
}, { t: Throwable? -> t!!.printStackTrace() })
56+
}, { t: Throwable? -> t?.printStackTrace() })
5757
allCompositeDisposable.add(disposable)
5858
return mutableLiveData
5959
}
@@ -77,7 +77,7 @@ class CurrencyRepository @Inject constructor(
7777
} else {
7878
throw Throwable("CurrencyRepository -> on Error occurred")
7979
}
80-
}, { t: Throwable? -> t!!.printStackTrace() })
80+
}, { t: Throwable? -> t?.printStackTrace() })
8181
allCompositeDisposable.add(disposable)
8282
return mutableLiveData
8383
}

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/data/repository/Repository.kt

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import erikjhordanrey.android_kotlin_devises.domain.AvailableExchange
2121
import erikjhordanrey.android_kotlin_devises.domain.Currency
2222
import io.reactivex.Flowable
2323

24-
2524
interface Repository {
2625

2726
fun getTotalCurrencies(): Flowable<Int>

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/di/AppComponent.kt

-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,4 @@ import javax.inject.Singleton
2424
@Singleton interface AppComponent {
2525

2626
fun inject(currencyViewModel: CurrencyViewModel)
27-
2827
}
29-
30-

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/di/AppModule.kt

-2
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,3 @@ class AppModule(private val currencyApplication: CurrencyApplication) {
2727
@Provides @Singleton fun provideContext(): Context = currencyApplication
2828

2929
}
30-
31-

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/di/CurrencyApplication.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CurrencyApplication : Application() {
2929
initializeDagger()
3030
}
3131

32-
fun initializeDagger() {
32+
private fun initializeDagger() {
3333
appComponent = DaggerAppComponent.builder()
3434
.appModule(AppModule(this))
3535
.roomModule(RoomModule())

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/di/RemoteModule.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
2929
import retrofit2.converter.gson.GsonConverterFactory
3030
import javax.inject.Singleton
3131

32-
3332
@Module
3433
class RemoteModule {
3534

@@ -50,12 +49,10 @@ class RemoteModule {
5049
.addConverterFactory(GsonConverterFactory.create(gson))
5150
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
5251
.client(okHttpClient)
53-
.build();
52+
.build()
5453

5554

5655
@Provides @Singleton fun provideRemoteCurrencyService(retrofit: Retrofit): RemoteCurrencyService =
5756
retrofit.create(RemoteCurrencyService::class.java)
5857

5958
}
60-
61-

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/di/RoomModule.kt

-2
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,3 @@ class RoomModule {
2828
@Provides @Singleton fun provideRoomCurrencyDataSource(context: Context) =
2929
RoomCurrencyDataSource.buildPersistentCurrency(context)
3030
}
31-
32-

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/domain/AvailableExchange.kt

-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ package erikjhordanrey.android_kotlin_devises.domain
1919
import java.io.Serializable
2020

2121
data class AvailableExchange(var availableExchangesMap: Map<String, Double>): Serializable
22-

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/domain/Currency.kt

-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@
1717
package erikjhordanrey.android_kotlin_devises.domain
1818

1919
data class Currency(var code: String, var country: String)
20-

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/view/AboutFragment.kt

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import android.view.ViewGroup
2626
import erikjhordanrey.android_kotlin_devises.R
2727
import kotlinx.android.synthetic.main.about_fragment.*
2828

29-
3029
class AboutFragment : Fragment() {
3130

3231
companion object {
@@ -51,7 +50,6 @@ class AboutFragment : Fragment() {
5150
show_me_code.setOnClickListener { startActivityActionView(PROJECT_SOURCE_CODE) }
5251
}
5352

54-
5553
private fun startActivityActionView(url: String) {
5654
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
5755
}

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/view/CurrencyFragment.kt

+18-17
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class CurrencyFragment : Fragment() {
4040
}
4141

4242
private val currencies = ArrayList<String>()
43-
private var currenciesAdapter: ArrayAdapter<String>? = null
44-
private var currencyFrom: String? = null
45-
private var currencyTo: String? = null
43+
private lateinit var currenciesAdapter: ArrayAdapter<String>
44+
private lateinit var currencyFrom: String
45+
private lateinit var currencyTo: String
4646

47-
private var currencyViewModel: CurrencyViewModel? = null
47+
private lateinit var currencyViewModel: CurrencyViewModel
4848

4949
override fun onCreate(savedInstanceState: Bundle?) {
5050
super.onCreate(savedInstanceState)
@@ -64,8 +64,8 @@ class CurrencyFragment : Fragment() {
6464

6565
private fun initViewModel() {
6666
currencyViewModel = ViewModelProviders.of(this).get(CurrencyViewModel::class.java)
67-
currencyViewModel?.let { lifecycle.addObserver(it) }
68-
currencyViewModel?.initLocalCurrencies()
67+
currencyViewModel.let { lifecycle.addObserver(it) }
68+
currencyViewModel.initLocalCurrencies()
6969
}
7070

7171
private fun initUI() {
@@ -74,12 +74,12 @@ class CurrencyFragment : Fragment() {
7474
}
7575

7676
private fun populateSpinnerAdapter() {
77-
currencyViewModel?.loadCurrencyList()?.observe(this, Observer { currencyList ->
78-
currencyList!!.forEach {
77+
currencyViewModel.loadCurrencyList()?.observe(this, Observer { currencyList ->
78+
currencyList?.forEach {
7979
currencies.add(it.code + " " + it.country)
8080
}
81-
currenciesAdapter!!.setDropDownViewResource(R.layout.item_spinner);
82-
currenciesAdapter!!.notifyDataSetChanged()
81+
currenciesAdapter.setDropDownViewResource(R.layout.item_spinner)
82+
currenciesAdapter.notifyDataSetChanged()
8383
})
8484

8585
}
@@ -102,11 +102,13 @@ class CurrencyFragment : Fragment() {
102102
val quantity = currency_edit.text.toString()
103103
currencyFrom = getCurrencyCode(from_currency_spinner.selectedItem.toString())
104104
currencyTo = getCurrencyCode(to_currency_spinner.selectedItem.toString())
105-
val currencies = currencyFrom + "," + currencyTo
105+
val currencies = "$currencyFrom,$currencyTo"
106106

107107
if (quantity.isNotEmpty() && currencyFrom != currencyTo) {
108-
currencyViewModel?.getAvailableExchange(currencies)?.observe(this, Observer { availableExchange ->
109-
exchange(quantity.toDouble(), availableExchange!!.availableExchangesMap)
108+
currencyViewModel.getAvailableExchange(currencies)?.observe(this, Observer { availableExchange ->
109+
availableExchange?.run {
110+
exchange(quantity.toDouble(), availableExchangesMap)
111+
}
110112
})
111113

112114
} else {
@@ -133,11 +135,10 @@ class CurrencyFragment : Fragment() {
133135
}
134136

135137
private fun showResult(result: String) {
136-
val builder: AlertDialog.Builder
137-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
138-
builder = AlertDialog.Builder(context!!, R.style.AppCompatAlertDialogStyle)
138+
val builder: AlertDialog.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
139+
AlertDialog.Builder(context!!, R.style.AppCompatAlertDialogStyle)
139140
} else {
140-
builder = AlertDialog.Builder(context!!)
141+
AlertDialog.Builder(context!!)
141142
}
142143

143144
val setMessage = TextView(activity)

‎app/src/main/kotlin/erikjhordanrey/android_kotlin_devises/view/CurrencyViewModel.kt

-5
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,13 @@ class CurrencyViewModel : ViewModel(), LifecycleObserver {
4848
initializeDagger()
4949
}
5050

51-
5251
fun getAvailableExchange(currencies: String): LiveData<AvailableExchange>? {
5352
liveAvailableExchange = null
5453
liveAvailableExchange = MutableLiveData<AvailableExchange>()
5554
liveAvailableExchange = currencyRepository.getAvailableExchange(currencies)
5655
return liveAvailableExchange
5756
}
5857

59-
6058
fun loadCurrencyList(): LiveData<List<Currency>>? {
6159
if (liveCurrencyData == null) {
6260
liveCurrencyData = MutableLiveData<List<Currency>>()
@@ -115,9 +113,6 @@ class CurrencyViewModel : ViewModel(), LifecycleObserver {
115113
super.onCleared()
116114
}
117115

118-
119116
private fun initializeDagger() = CurrencyApplication.appComponent.inject(this)
120117

121118
}
122-
123-

0 commit comments

Comments
 (0)