Skip to content

Commit

Permalink
refactor(shared): use entity classes for UserRepository.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Dec 1, 2024
1 parent a8011cd commit a4add62
Show file tree
Hide file tree
Showing 42 changed files with 378 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.action.actionStartActivity
import androidx.glance.appwidget.provideContent
import com.paligot.confily.android.R
import com.paligot.confily.core.networking.NetworkingRepository
import com.paligot.confily.core.networking.UserRepository
import com.paligot.confily.navigation.Screen
import com.paligot.confily.widgets.presentation.NetworkingWidget
import com.paligot.confily.widgets.style.ConfilyGlanceTheme
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

class NetworkingAppWidget : GlanceAppWidget(), KoinComponent {
private val userRepository: NetworkingRepository by inject()
private val userRepository: UserRepository by inject()

override suspend fun provideGlance(context: Context, id: GlanceId) {
provideContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.paligot.confily.core.events.EventRepository
import com.paligot.confily.core.events.entities.mapToUi
import com.paligot.confily.core.networking.entities.mapToUi
import com.paligot.confily.models.ui.EventUi
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.paligot.confily.events.presentation.EventListVM
import com.paligot.confily.infos.panes.TicketQrCodeScanner
import com.paligot.confily.infos.presentation.InfoCompactVM
import com.paligot.confily.models.ui.ExportNetworkingUi
import com.paligot.confily.models.ui.convertToModelUi
import com.paligot.confily.navigation.Screen
import com.paligot.confily.networking.presentation.NetworkingCompactVM
import com.paligot.confily.networking.presentation.ProfileInputVM
Expand Down Expand Up @@ -242,7 +241,7 @@ fun MainNavigation(
VCardQrCodeScanner(
navigateToSettingsScreen = {},
onQrCodeDetected = { vcard ->
viewModel.saveNetworkingProfile(vcard.convertToModelUi())
viewModel.saveNetworkingProfile(vcard)
navController.popBackStack()
},
onBackClicked = { navController.popBackStack() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import com.paligot.confily.core.agenda.AgendaRepository
import com.paligot.confily.core.events.EventRepository
import com.paligot.confily.core.networking.NetworkingRepository
import com.paligot.confily.core.networking.UserRepository
import com.paligot.confily.core.networking.entities.UserItem
import com.paligot.confily.models.ui.ScaffoldConfigUi
import com.paligot.confily.models.ui.UserNetworkingUi
import com.paligot.confily.models.ui.VCardModel
import com.paligot.confily.navigation.BottomActions
import com.paligot.confily.style.theme.actions.NavigationAction
import com.paligot.confily.style.theme.actions.NavigationActionsUi
Expand All @@ -29,7 +30,7 @@ sealed class MainNavigationUiState {
class MainNavigationViewModel(
agendaRepository: AgendaRepository,
private val eventRepository: EventRepository,
private val userRepository: NetworkingRepository
private val userRepository: UserRepository
) : ViewModel() {
val uiState: StateFlow<MainNavigationUiState> = agendaRepository.scaffoldConfig()
.map { MainNavigationUiState.Success(navActions(it)) }
Expand All @@ -47,8 +48,8 @@ class MainNavigationViewModel(
eventRepository.insertOrUpdateTicket(barcode)
}

fun saveNetworkingProfile(user: UserNetworkingUi) = viewModelScope.launch {
userRepository.insertNetworkingProfile(user)
fun saveNetworkingProfile(model: VCardModel) = viewModelScope.launch {
userRepository.insertUserScanned(model.mapToEntity())
}

private fun navActions(config: ScaffoldConfigUi): NavigationActionsUi =
Expand All @@ -66,3 +67,10 @@ class MainNavigationViewModel(
}.toImmutableList()
)
}

private fun VCardModel.mapToEntity(): UserItem = UserItem(
email = email,
firstName = firstName,
lastName = lastName,
company = company
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.paligot.confily.networking.presentation

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.paligot.confily.core.networking.NetworkingRepository
import com.paligot.confily.core.networking.UserRepository
import com.paligot.confily.core.networking.entities.mapToUi
import com.paligot.confily.models.ui.UserNetworkingUi
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
Expand All @@ -19,10 +21,14 @@ sealed class ContactsUiState {
}

class ContactsViewModel(
private val repository: NetworkingRepository
private val repository: UserRepository
) : ViewModel() {
val uiState: StateFlow<ContactsUiState> = repository.fetchNetworking()
.map { ContactsUiState.Success(users = it) as ContactsUiState }
val uiState: StateFlow<ContactsUiState> = repository.fetchUsersScanned()
.map { users ->
ContactsUiState.Success(
users = users.map { it.mapToUi() }.toImmutableList()
) as ContactsUiState
}
.catch { emit(ContactsUiState.Failure(it)) }
.stateIn(
scope = viewModelScope,
Expand All @@ -31,6 +37,6 @@ class ContactsViewModel(
)

fun deleteNetworking(email: String) = viewModelScope.launch {
repository.deleteNetworkProfile(email)
repository.deleteUserScannedByEmail(email)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package com.paligot.confily.networking.presentation

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.paligot.confily.core.networking.NetworkingRepository
import com.paligot.confily.core.networking.UserRepository
import com.paligot.confily.core.networking.entities.mapToUi
import com.paligot.confily.models.ui.UserProfileUi
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -16,11 +17,11 @@ sealed class MyProfileUiState {
data class Failure(val throwable: Throwable) : MyProfileUiState()
}

class MyProfileViewModel(repository: NetworkingRepository) : ViewModel() {
val uiState: StateFlow<MyProfileUiState> = repository.fetchProfile()
class MyProfileViewModel(repository: UserRepository) : ViewModel() {
val uiState: StateFlow<MyProfileUiState> = repository.fetchUserProfile()
.map {
MyProfileUiState.Success(
profile = it ?: UserProfileUi(
profile = it?.mapToUi() ?: UserProfileUi(
email = "",
firstName = "",
lastName = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.paligot.confily.networking.presentation
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.paligot.confily.core.agenda.AgendaRepository
import com.paligot.confily.core.networking.NetworkingRepository
import com.paligot.confily.core.networking.UserRepository
import com.paligot.confily.core.networking.entities.mapToUi
import com.paligot.confily.models.ui.ExportNetworkingUi
import com.paligot.confily.navigation.FabActions
import com.paligot.confily.navigation.Screen
Expand Down Expand Up @@ -36,7 +37,7 @@ sealed class NetworkingUiState {

class NetworkingViewModel(
agendaRepository: AgendaRepository,
private val networkingRepository: NetworkingRepository
private val userRepository: UserRepository
) : ViewModel() {
private val _exportPath = MutableSharedFlow<ExportNetworkingUi>(replay = 1)
val exportPath: SharedFlow<ExportNetworkingUi> = _exportPath
Expand Down Expand Up @@ -85,6 +86,6 @@ class NetworkingViewModel(
}

fun exportNetworking() = viewModelScope.launch {
_exportPath.tryEmit(networkingRepository.exportNetworking())
_exportPath.tryEmit(userRepository.exportUserScanned().mapToUi())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.paligot.confily.networking.presentation

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.paligot.confily.core.networking.NetworkingRepository
import com.paligot.confily.core.networking.UserRepository
import com.paligot.confily.core.networking.entities.UserInfo
import com.paligot.confily.core.networking.entities.mapToUi
import com.paligot.confily.models.ui.Field
import com.paligot.confily.models.ui.UserProfileUi
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -16,17 +18,17 @@ sealed class ProfileInputUiState {
}

class ProfileInputViewModel(
private val repository: NetworkingRepository
private val repository: UserRepository
) : ViewModel() {
private val _uiState = MutableStateFlow<ProfileInputUiState>(ProfileInputUiState.Loading)
val uiState: StateFlow<ProfileInputUiState> = _uiState

init {
viewModelScope.launch {
try {
repository.fetchProfile().collect {
repository.fetchUserProfile().collect {
_uiState.value = ProfileInputUiState.Success(
profile = it ?: UserProfileUi(
profile = it?.mapToUi() ?: UserProfileUi(
email = "",
firstName = "",
lastName = "",
Expand Down Expand Up @@ -58,11 +60,14 @@ class ProfileInputViewModel(
val profile = (_uiState.value as ProfileInputUiState.Success).profile
val email = profile.email
if (email == "") return@launch
repository.saveProfile(
email,
profile.firstName,
profile.lastName,
profile.company
repository.insertUserInfo(
UserInfo(
firstName = email,
lastName = profile.firstName,
email = profile.lastName,
company = profile.company,
qrCode = null
)
)
}
}
1 change: 1 addition & 0 deletions features/speakers/speakers-di/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ kotlin {
implementation(projects.shared.coreDi)

implementation(libs.koin.compose.viewmodel)
implementation(libs.lyricist)
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
0060F46B2C8BC231000BCD06 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0060F46A2C8BC231000BCD06 /* GoogleService-Info.plist */; };
009DACF52C69384200A7F42E /* AgendaRepositoryExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 009DACF42C69384200A7F42E /* AgendaRepositoryExt.swift */; };
009DACF92C693B0900A7F42E /* NetworkingRepositoryExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 009DACF82C693B0900A7F42E /* NetworkingRepositoryExt.swift */; };
058557BB273AAA24004C7B11 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557BA273AAA24004C7B11 /* Assets.xcassets */; };
058557D9273AAEEB004C7B11 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */; };
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2152FB032600AC8F00CF470E /* iOSApp.swift */; };
Expand Down Expand Up @@ -119,7 +118,6 @@
/* Begin PBXFileReference section */
0060F46A2C8BC231000BCD06 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
009DACF42C69384200A7F42E /* AgendaRepositoryExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgendaRepositoryExt.swift; sourceTree = "<group>"; };
009DACF82C693B0900A7F42E /* NetworkingRepositoryExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkingRepositoryExt.swift; sourceTree = "<group>"; };
058557BA273AAA24004C7B11 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
058557D8273AAEEB004C7B11 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
2152FB032600AC8F00CF470E /* iOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -344,7 +342,6 @@
D52F3B992961CDCC00535233 /* ViewExt.swift */,
D5C1B3BA29F26D9E00D94391 /* BundleExt.swift */,
009DACF42C69384200A7F42E /* AgendaRepositoryExt.swift */,
009DACF82C693B0900A7F42E /* NetworkingRepositoryExt.swift */,
);
path = extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -725,7 +722,6 @@
buildActionMask = 2147483647;
files = (
D574DE1A292446AE00468074 /* EventSessionView.swift in Sources */,
009DACF92C693B0900A7F42E /* NetworkingRepositoryExt.swift in Sources */,
D59944BE2833CDC900F59C1B /* TicketDetailedView.swift in Sources */,
D5BC483229B0CC98002CC517 /* ViewModelFactory.swift in Sources */,
D5B5E8522B4C972D00129758 /* AgendaFiltersViewModel.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "iosApp/screens/networking/NetworkingViewModel.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "40"
endingLineNumber = "40"
startingLineNumber = "41"
endingLineNumber = "41"
landmarkName = "fetchNetworking()"
landmarkType = "7">
</BreakpointContent>
Expand Down
20 changes: 0 additions & 20 deletions iosApp/iosApp/extensions/NetworkingRepositoryExt.swift

This file was deleted.

19 changes: 11 additions & 8 deletions iosApp/iosApp/screens/networking/NetworkingViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ enum UserProfileUiState {

@MainActor
class NetworkingViewModel: ObservableObject {
private let repository: NetworkingRepository = RepositoryHelper().networkingRepository
private let repository: UserRepository = RepositoryHelper().userRepository
private let interactor: UserInteractor = InteractorHelper().userInteractor

@Published var uiState: UserProfileUiState = UserProfileUiState.loading

Expand All @@ -31,8 +32,8 @@ class NetworkingViewModel: ObservableObject {
func fetchNetworking() {
networkingTask = Task {
do {
let networkingStream = asyncSequence(for: repository.fetchNetworking())
let profileStream = asyncSequence(for: repository.fetchProfile())
let networkingStream = asyncSequence(for: interactor.fetchUsersScanned())
let profileStream = asyncSequence(for: interactor.fetchUserProfile())
for try await (networkings, profile) in combineLatest(networkingStream, profileStream) {
self.uiState = .success(NetworkingUi(userProfileUi: profile, showQrCode: false, users: networkings))
}
Expand All @@ -48,21 +49,23 @@ class NetworkingViewModel: ObservableObject {

func saveProfile(email: String, firstName: String, lastName: String, company: String) {
if (email == "") { return }
repository.saveProfile(email: email, firstName: firstName, lastName: lastName, company: company)
repository.insertUserInfo(
user: UserEntity(firstName: firstName, lastName: lastName, email: email, company: company, qrCode: nil)
)
}

func saveNetworkingProfile(text: String) {
if let data = text.data(using: .unicode) {
do {
let contacts = try CNContactVCardSerialization.contacts(with: data)
let contact = contacts.first
let user = UserNetworkingUi(
let user = UserItemEntity(
email: (contact?.emailAddresses.first?.value ?? "") as String,
firstName: contact?.givenName ?? "",
lastName: contact?.familyName ?? "",
company: contact?.organizationName ?? ""
)
repository.insertNetworkingProfile(user: user)
repository.insertUserScanned(user: user)
if case .success(let networkingUi) = uiState {
self.uiState = .success(networkingUi.doCopy(
userProfileUi: networkingUi.userProfileUi,
Expand All @@ -88,11 +91,11 @@ class NetworkingViewModel: ObservableObject {
}

func deleteNetworkProfile(email: String) {
repository.deleteNetworkProfile(email: email)
repository.deleteUserScannedByEmail(email: email)
}

func exportNetworking() -> ComposeMailData {
let networking = repository.exportNetworking()
let networking = repository.exportUserScanned()
let recipients = networking.mailto != nil ? [networking.mailto!] : []
do {
return ComposeMailData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ qrcode BLOB NOT NULL
);

selectTicket:
SELECT * FROM Ticket WHERE event_id == ?;
SELECT id, email, firstname, lastname, barcode, qrcode
FROM Ticket
WHERE event_id == ?;

insertUser:
insertTicket:
INSERT OR REPLACE INTO Ticket(
id, ext_id, event_id, email, firstname, lastname, barcode, qrcode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?);
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
CREATE TABLE User(
email TEXT NOT NULL PRIMARY KEY,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
company TEXT,
qrcode BLOB NOT NULL
);

CREATE TABLE Profile(
event_id TEXT NOT NULL PRIMARY KEY,
email TEXT NOT NULL,
Expand Down
Loading

0 comments on commit a4add62

Please sign in to comment.