Removed redundant interfaces, simplified the code

pull/49/head
Arty Bishop 2021-02-25 21:07:31 +00:00
rodzic 02dd0a5a78
commit 0438b8a51f
11 zmienionych plików z 123 dodań i 259 usunięć

Wyświetl plik

@ -20,25 +20,27 @@
package com.rtbishop.look4sat package com.rtbishop.look4sat
import android.net.Uri import android.net.Uri
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.rtbishop.look4sat.data.* import com.rtbishop.look4sat.data.*
import com.rtbishop.look4sat.repo.EntriesRepo import com.rtbishop.look4sat.repo.EntriesRepo
import com.rtbishop.look4sat.repo.SourcesRepo import com.rtbishop.look4sat.repo.DefaultSourcesRepo
import com.rtbishop.look4sat.repo.TransmittersRepo import com.rtbishop.look4sat.repo.TransmittersRepo
import com.rtbishop.look4sat.utility.PassPredictor import com.rtbishop.look4sat.utility.PassPredictor
import com.rtbishop.look4sat.utility.PrefsManager import com.rtbishop.look4sat.utility.PrefsManager
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.* import java.util.*
import javax.inject.Inject
class SharedViewModel @ViewModelInject constructor( @HiltViewModel
class SharedViewModel @Inject constructor(
private val prefsManager: PrefsManager, private val prefsManager: PrefsManager,
private val sourcesRepo: SourcesRepo, private val sourcesRepo: DefaultSourcesRepo,
private val entriesRepo: EntriesRepo, private val entriesRepo: EntriesRepo,
private val transmittersRepo: TransmittersRepo private val transmittersRepo: TransmittersRepo
) : ViewModel() { ) : ViewModel() {

Wyświetl plik

@ -26,20 +26,23 @@ import androidx.preference.PreferenceManager
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.qualifiers.ActivityContext import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module @Module
@InstallIn(ActivityComponent::class) @InstallIn(SingletonComponent::class)
object UtilityModule { object ApplicationModule {
@Provides @Provides
fun getSharedPreferences(@ActivityContext context: Context): SharedPreferences { @Singleton
return PreferenceManager.getDefaultSharedPreferences(context) fun getLocationManager(@ApplicationContext context: Context): LocationManager {
}
@Provides
fun getLocationManager(@ActivityContext context: Context): LocationManager {
return context.getSystemService(Context.LOCATION_SERVICE) as LocationManager return context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
} }
@Provides
@Singleton
fun getSharedPreferences(@ApplicationContext context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
} }

Wyświetl plik

@ -23,21 +23,24 @@ import com.rtbishop.look4sat.repo.remote.TransmittersApi
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory import retrofit2.converter.moshi.MoshiConverterFactory
import javax.inject.Singleton
@Module @Module
@InstallIn(ActivityComponent::class) @InstallIn(SingletonComponent::class)
object NetworkModule { object NetworkModule {
@Provides @Provides
@Singleton
fun getOkHttpClient(): OkHttpClient { fun getOkHttpClient(): OkHttpClient {
return OkHttpClient() return OkHttpClient()
} }
@Provides @Provides
@Singleton
fun getTransmittersApi(): TransmittersApi { fun getTransmittersApi(): TransmittersApi {
return Retrofit.Builder() return Retrofit.Builder()
.baseUrl("https://db.satnogs.org/api/") .baseUrl("https://db.satnogs.org/api/")

Wyświetl plik

@ -1,61 +0,0 @@
/*******************************************************************************
Look4Sat. Amateur radio satellite tracker and pass predictor.
Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
******************************************************************************/
package com.rtbishop.look4sat.di
import android.content.ContentResolver
import android.content.Context
import com.rtbishop.look4sat.repo.*
import com.rtbishop.look4sat.repo.local.EntriesDao
import com.rtbishop.look4sat.repo.local.SourcesDao
import com.rtbishop.look4sat.repo.local.TransmittersDao
import com.rtbishop.look4sat.repo.remote.TransmittersApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.qualifiers.ActivityContext
import okhttp3.OkHttpClient
@Module
@InstallIn(ActivityComponent::class)
object RepoModule {
@Provides
fun getEntriesRepo(resolver: ContentResolver, client: OkHttpClient, entriesDao: EntriesDao)
: EntriesRepo {
return DefaultEntriesRepo(resolver, client, entriesDao)
}
@Provides
fun getSourcesRepo(sourcesDao: SourcesDao): SourcesRepo {
return DefaultSourcesRepo(sourcesDao)
}
@Provides
fun getTransmittersRepo(transDao: TransmittersDao, transApi: TransmittersApi)
: TransmittersRepo {
return DefaultTransmittersRepo(transDao, transApi)
}
@Provides
fun getContentResolver(@ActivityContext context: Context): ContentResolver {
return context.applicationContext.contentResolver
}
}

Wyświetl plik

@ -19,6 +19,7 @@
package com.rtbishop.look4sat.di package com.rtbishop.look4sat.di
import android.content.ContentResolver
import android.content.Context import android.content.Context
import androidx.room.Room import androidx.room.Room
import com.rtbishop.look4sat.repo.local.EntriesDao import com.rtbishop.look4sat.repo.local.EntriesDao
@ -28,30 +29,41 @@ import com.rtbishop.look4sat.repo.local.TransmittersDao
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.qualifiers.ActivityContext import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module @Module
@InstallIn(ActivityComponent::class) @InstallIn(SingletonComponent::class)
object PersistenceModule { object RepositoryModule {
@Provides @Provides
@Singleton
fun getContentResolver(@ApplicationContext context: Context): ContentResolver {
return context.contentResolver
}
@Provides
@Singleton
fun getEntriesDao(db: SatelliteDb): EntriesDao { fun getEntriesDao(db: SatelliteDb): EntriesDao {
return db.entriesDao() return db.entriesDao()
} }
@Provides @Provides
@Singleton
fun getSourcesDao(db: SatelliteDb): SourcesDao { fun getSourcesDao(db: SatelliteDb): SourcesDao {
return db.sourcesDao() return db.sourcesDao()
} }
@Provides @Provides
@Singleton
fun getTransmittersDao(db: SatelliteDb): TransmittersDao { fun getTransmittersDao(db: SatelliteDb): TransmittersDao {
return db.transmittersDao() return db.transmittersDao()
} }
@Provides @Provides
fun getSatelliteDb(@ActivityContext context: Context): SatelliteDb { @Singleton
fun getSatelliteDb(@ApplicationContext context: Context): SatelliteDb {
return Room.databaseBuilder(context, SatelliteDb::class.java, "satDb").build() return Room.databaseBuilder(context, SatelliteDb::class.java, "satDb").build()
} }
} }

Wyświetl plik

@ -1,79 +0,0 @@
/*******************************************************************************
Look4Sat. Amateur radio satellite tracker and pass predictor.
Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
******************************************************************************/
package com.rtbishop.look4sat.repo
import android.content.ContentResolver
import android.net.Uri
import androidx.lifecycle.LiveData
import com.github.amsacode.predict4java.TLE
import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.repo.local.EntriesDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.InputStream
import javax.inject.Inject
class DefaultEntriesRepo @Inject constructor(
private val resolver: ContentResolver,
private val client: OkHttpClient,
private val entriesDao: EntriesDao
) : EntriesRepo {
override fun getEntries(): LiveData<List<SatEntry>> {
return entriesDao.getEntries()
}
override suspend fun updateEntriesFromFile(fileUri: Uri) {
withContext(Dispatchers.IO) {
resolver.openInputStream(fileUri)?.use { stream ->
val importedEntries = TLE.importSat(stream).map { SatEntry(it) }
val selection = entriesDao.getEntriesSelection()
entriesDao.updateEntries(importedEntries)
entriesDao.updateEntriesSelection(selection)
}
}
}
override suspend fun updateEntriesFromSources(sources: List<TleSource>) {
withContext(Dispatchers.IO) {
val streams = mutableListOf<InputStream>()
sources.forEach { source ->
val request = Request.Builder().url(source.url).build()
val stream = client.newCall(request).execute().body()?.byteStream()
stream?.let { inputStream -> streams.add(inputStream) }
}
val importedEntries = mutableListOf<SatEntry>()
streams.forEach { stream ->
val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) }
importedEntries.addAll(entries)
}
val selection = entriesDao.getEntriesSelection()
entriesDao.updateEntries(importedEntries)
entriesDao.updateEntriesSelection(selection)
}
}
override suspend fun updateEntriesSelection(satIds: List<Int>) {
entriesDao.updateEntriesSelection(satIds)
}
}

Wyświetl plik

@ -24,14 +24,13 @@ import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.repo.local.SourcesDao import com.rtbishop.look4sat.repo.local.SourcesDao
import javax.inject.Inject import javax.inject.Inject
class DefaultSourcesRepo @Inject constructor(private val sourcesDao: SourcesDao) : class DefaultSourcesRepo @Inject constructor(private val sourcesDao: SourcesDao) {
SourcesRepo {
override fun getSources(): LiveData<List<TleSource>> { fun getSources(): LiveData<List<TleSource>> {
return sourcesDao.getSources() return sourcesDao.getSources()
} }
override suspend fun updateSources(sources: List<TleSource>) { suspend fun updateSources(sources: List<TleSource>) {
sourcesDao.updateSources(sources) sourcesDao.updateSources(sources)
} }
} }

Wyświetl plik

@ -1,40 +0,0 @@
/*******************************************************************************
Look4Sat. Amateur radio satellite tracker and pass predictor.
Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
******************************************************************************/
package com.rtbishop.look4sat.repo
import androidx.lifecycle.LiveData
import com.rtbishop.look4sat.data.SatTrans
import com.rtbishop.look4sat.repo.local.TransmittersDao
import com.rtbishop.look4sat.repo.remote.TransmittersApi
import javax.inject.Inject
class DefaultTransmittersRepo @Inject constructor(
private val transmittersDao: TransmittersDao,
private val transmittersApi: TransmittersApi
) : TransmittersRepo {
override fun getTransmittersForSat(satId: Int): LiveData<List<SatTrans>> {
return transmittersDao.getTransmittersForSat(satId)
}
override suspend fun updateTransmitters() {
transmittersDao.updateTransmitters(transmittersApi.getTransmitters())
}
}

Wyświetl plik

@ -19,18 +19,63 @@
package com.rtbishop.look4sat.repo package com.rtbishop.look4sat.repo
import android.content.ContentResolver
import android.net.Uri import android.net.Uri
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import com.github.amsacode.predict4java.TLE
import com.rtbishop.look4sat.data.SatEntry import com.rtbishop.look4sat.data.SatEntry
import com.rtbishop.look4sat.data.TleSource import com.rtbishop.look4sat.data.TleSource
import com.rtbishop.look4sat.repo.local.EntriesDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.InputStream
import javax.inject.Inject
interface EntriesRepo { class EntriesRepo @Inject constructor(
private val resolver: ContentResolver,
private val client: OkHttpClient,
private val entriesDao: EntriesDao
) {
fun getEntries(): LiveData<List<SatEntry>> fun getEntries(): LiveData<List<SatEntry>> {
return entriesDao.getEntries()
suspend fun updateEntriesFromFile(fileUri: Uri) }
suspend fun updateEntriesFromSources(sources: List<TleSource>) suspend fun updateEntriesFromFile(fileUri: Uri) {
withContext(Dispatchers.IO) {
suspend fun updateEntriesSelection(satIds: List<Int>) kotlin.runCatching {
resolver.openInputStream(fileUri)?.use { stream ->
val importedEntries = TLE.importSat(stream).map { SatEntry(it) }
val selection = entriesDao.getEntriesSelection()
entriesDao.updateEntries(importedEntries)
entriesDao.updateEntriesSelection(selection)
}
}
}
}
suspend fun updateEntriesFromSources(sources: List<TleSource>) {
withContext(Dispatchers.IO) {
val streams = mutableListOf<InputStream>()
sources.forEach { source ->
val request = Request.Builder().url(source.url).build()
val stream = client.newCall(request).execute().body()?.byteStream()
stream?.let { inputStream -> streams.add(inputStream) }
}
val importedEntries = mutableListOf<SatEntry>()
streams.forEach { stream ->
val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) }
importedEntries.addAll(entries)
}
val selection = entriesDao.getEntriesSelection()
entriesDao.updateEntries(importedEntries)
entriesDao.updateEntriesSelection(selection)
}
}
suspend fun updateEntriesSelection(satIds: List<Int>) {
entriesDao.updateEntriesSelection(satIds)
}
} }

Wyświetl plik

@ -1,30 +0,0 @@
/*******************************************************************************
Look4Sat. Amateur radio satellite tracker and pass predictor.
Copyright (C) 2019, 2020 Arty Bishop (bishop.arty@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
******************************************************************************/
package com.rtbishop.look4sat.repo
import androidx.lifecycle.LiveData
import com.rtbishop.look4sat.data.TleSource
interface SourcesRepo {
fun getSources(): LiveData<List<TleSource>>
suspend fun updateSources(sources: List<TleSource>)
}

Wyświetl plik

@ -21,10 +21,20 @@ package com.rtbishop.look4sat.repo
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import com.rtbishop.look4sat.data.SatTrans import com.rtbishop.look4sat.data.SatTrans
import com.rtbishop.look4sat.repo.local.TransmittersDao
import com.rtbishop.look4sat.repo.remote.TransmittersApi
import javax.inject.Inject
interface TransmittersRepo { class TransmittersRepo @Inject constructor(
private val transmittersDao: TransmittersDao,
private val transmittersApi: TransmittersApi
) {
suspend fun updateTransmitters() fun getTransmittersForSat(satId: Int): LiveData<List<SatTrans>> {
return transmittersDao.getTransmittersForSat(satId)
fun getTransmittersForSat(satId: Int): LiveData<List<SatTrans>> }
suspend fun updateTransmitters() {
transmittersDao.updateTransmitters(transmittersApi.getTransmitters())
}
} }