Skip to content

Commit

Permalink
Fix codestyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPushkin committed Aug 20, 2022
1 parent f5ac39c commit e13160c
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class AppDatabaseDaosTest {
insertMarkerTexts(listOf(MarkerText(id, languageId, null, null)))
}
}

}
id++
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/ru/spbu/depnav/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ru.spbu.depnav.ui.search.SearchScreen
import ru.spbu.depnav.ui.theme.DepNavTheme
import ru.spbu.depnav.utils.preferences.PreferencesManager

@Suppress("UndocumentedPublicClass") // Class name is self-explanatory
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val mapScreenVm: MapScreenViewModel by viewModels()
Expand Down Expand Up @@ -57,4 +58,4 @@ class MainActivity : ComponentActivity() {
}
}
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/ru/spbu/depnav/data/db/DatabaseModule.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UndocumentedPublicFunction") // Method names are self-explanatory

package ru.spbu.depnav.data.db

import android.content.Context
Expand All @@ -11,6 +13,7 @@ import javax.inject.Singleton

private const val DB_ASSET = "markers.db"

/** Database-related providers for Hilt. */
@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {
Expand All @@ -25,6 +28,7 @@ object DatabaseModule {
.fallbackToDestructiveMigration()
.build()

/** Provides */
@Provides
fun provideMapInfoDao(appDatabase: AppDatabase) = appDatabase.mapInfoDao()

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/ru/spbu/depnav/data/model/MarkerText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ data class MarkerText(
EN, RU;

companion object {
/** Returns the current locale's language, if it is supported, or [EN] otherwise. */
@Suppress("UseIfInsteadOfWhen") // Planning to add more languages later.
fun getCurrent() = when (Locale.current.language) {
"ru" -> RU
else -> EN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import javax.inject.Inject

/** Repository for loading and saving [MapInfo] objects. */
class MapInfoRepo @Inject constructor(private val dao: MapInfoDao) {
/** Saves the provided [MapInfo] objects. */
/** Saves the provided objects. */
suspend fun insertAll(vararg mapInfos: MapInfo) = dao.insertAll(*mapInfos)

/** Loads a [MapInfo] by its name. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ private const val TAG = "MarkerWithTextRepo"

/** Repository for loading and saving [Marker] objects with associated [MarkerText] objects. */
class MarkerWithTextRepo @Inject constructor(private val dao: MarkerWithTextDao) {
/** Saves the provided objects. */
suspend fun insertAll(markersWithText: Map<Marker, MarkerText>) {
dao.insertMarkers(markersWithText.keys)
dao.insertMarkerTexts(markersWithText.values)
}

/** Loads a [Marker] by its ID and its corresponding [MarkerText] on the current language. */
suspend fun loadById(id: Int): Pair<Marker, MarkerText> {
val language = MarkerText.LanguageId.getCurrent()
val (marker, markerTexts) = dao.loadById(id, language).entries.firstOrNull()
Expand All @@ -23,7 +25,12 @@ class MarkerWithTextRepo @Inject constructor(private val dao: MarkerWithTextDao)
return marker to markerText
}

suspend fun loadByFloor(floor: Int, language: MarkerText.LanguageId): Map<Marker, MarkerText> {
/**
* Loads all [Markers][Marker] on the specified floor with their corresponding [MarkerText] on
* the current language.
*/
suspend fun loadByFloor(floor: Int): Map<Marker, MarkerText> {
val language = MarkerText.LanguageId.getCurrent()
val markersWithTexts = dao.loadByFloor(floor, language)
return markersWithTexts.entries.associate { (marker, markerTexts) ->
val markerText = markerTexts.squeezedFor(marker, language)
Expand All @@ -37,10 +44,12 @@ class MarkerWithTextRepo @Inject constructor(private val dao: MarkerWithTextDao)
MarkerText(marker.id, language, null, null)
}

suspend fun loadByTokens(
tokens: String,
language: MarkerText.LanguageId
): Map<Marker, MarkerText> {
/**
* Loads a [Marker] and its corresponding [MarkerText] on the current language so that the text
* has the specified tokens in it.
*/
suspend fun loadByTokens(tokens: String): Map<Marker, MarkerText> {
val language = MarkerText.LanguageId.getCurrent()
val textsWithMarkers = dao.loadByTokens(tokens, language)
return textsWithMarkers.entries.associate { (markerText, markers) ->
val marker = markers.firstOrNull()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/ru/spbu/depnav/ui/NavDestination.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package ru.spbu.depnav.ui

/** Application's GUI screens as navigation destinations. */
enum class NavDestination { MAP, SEARCH }
6 changes: 2 additions & 4 deletions app/src/main/java/ru/spbu/depnav/ui/map/MapScreenViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ class MapScreenViewModel @Inject constructor(
}

private suspend fun initMap(mapName: String, tilesSubdir: String) {
val language = MarkerText.LanguageId.getCurrent()

Log.i(TAG, "Initializing map $mapName on $language language")
Log.i(TAG, "Initializing map $mapName")

minScaleCollectionJob?.cancel("State changed")
mapState.shutdown()
Expand All @@ -155,7 +153,7 @@ class MapScreenViewModel @Inject constructor(
val floorNum = it + 1
val layers = listOf(makeTileProviderForFloor(floorNum))
val markers = withContext(Dispatchers.IO) {
async { markerWithTextRepo.loadByFloor(floorNum, language) }
async { markerWithTextRepo.loadByFloor(floorNum) }
}
floorNum to Floor(layers, markers)
}.toMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SearchScreenViewModel @Inject constructor(private val markerWithTextRepo:
val query = text.split(' ').joinToString(" ") { "$it*" }

viewModelScope.launch(Dispatchers.IO) {
val matches = markerWithTextRepo.loadByTokens(query, language)
val matches = markerWithTextRepo.loadByTokens(query)
Log.v(TAG, "Found ${matches.size} matches")
launch(Dispatchers.Main) { matchedMarkers = matches.values }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.setValue
import androidx.core.content.edit
import dagger.hilt.android.qualifiers.ApplicationContext
import ru.spbu.depnav.R
import ru.spbu.depnav.data.model.MapInfo
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -60,16 +61,22 @@ class PreferencesManager @Inject constructor(@ApplicationContext context: Contex
prefs.getString(MAP_STORED_NAME_KEY, MAP_STORED_NAME_DEFAULT) ?: MAP_STORED_NAME_DEFAULT
)

/** Defines what map the app shows. */
var mapStoredName: MapStoredName
get() = MapStoredName.valueOf(_mapStoredName)
set(value) {
prefs.edit { putString(MAP_STORED_NAME_KEY, value.name) }
_mapStoredName = value.name
}

enum class MapStoredName(val storedName: String) {
/** Maps available in the app. */
enum class MapStoredName(
/** Map's name as it is stored in the corresponding [MapInfo] in the database. */
val storedName: String
) {
SPBU_MM("spbu-mm");

/** Subdirectory with map's tiles in assets. */
val tilesSubdir = "$storedName/tiles"
}
}

0 comments on commit e13160c

Please sign in to comment.