Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implmenting new feature where user can add ready providers with icons… #14

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Billing-Tracker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
390E9675257460A6004B17DA /* SubscriptionListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 390E9674257460A6004B17DA /* SubscriptionListViewModel.swift */; };
390E967925747198004B17DA /* SubscriptionTimingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 390E967825747198004B17DA /* SubscriptionTimingsViewModel.swift */; };
390E967E257476F3004B17DA /* AddSubscriptionButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 390E967D257476F3004B17DA /* AddSubscriptionButton.swift */; };
391F0CD7257D9A6900FA3F41 /* ProvidedProvidersViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 391F0CD6257D9A6900FA3F41 /* ProvidedProvidersViewModel.swift */; };
39290E2D2577C6F9004D9768 /* SubscriptionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39290E2C2577C6F9004D9768 /* SubscriptionService.swift */; };
39290E332577CD64004D9768 /* ProviderServices.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39290E322577CD64004D9768 /* ProviderServices.swift */; };
3929C7BA2571BD0C0012DD51 /* Provider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3929C7B92571BD0C0012DD51 /* Provider.swift */; };
Expand Down Expand Up @@ -77,7 +78,7 @@
39D9134125716E5F00D9BDFE /* UpdateSubscriptionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39D9134025716E5F00D9BDFE /* UpdateSubscriptionView.swift */; };
39D9134B257181AC00D9BDFE /* DaysRingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39D9134A257181AC00D9BDFE /* DaysRingView.swift */; };
39E7754325770A9700BB3B31 /* TextFieldModifiers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39E7754225770A9700BB3B31 /* TextFieldModifiers.swift */; };
39E7754725770F5300BB3B31 /* saveButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39E7754625770F5300BB3B31 /* saveButtonView.swift */; };
39E7754725770F5300BB3B31 /* StandardButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39E7754625770F5300BB3B31 /* StandardButton.swift */; };
39EF3A612576723E00777442 /* SubscriptionRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39EF3A602576723E00777442 /* SubscriptionRepository.swift */; };
39F215A125771DA3009332E7 /* SubscriptionFormAlerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39F215A025771DA3009332E7 /* SubscriptionFormAlerts.swift */; };
39F215A4257728F6009332E7 /* TabIconsModifiers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39F215A3257728F6009332E7 /* TabIconsModifiers.swift */; };
Expand All @@ -98,6 +99,7 @@
390E9674257460A6004B17DA /* SubscriptionListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionListViewModel.swift; sourceTree = "<group>"; };
390E967825747198004B17DA /* SubscriptionTimingsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionTimingsViewModel.swift; sourceTree = "<group>"; };
390E967D257476F3004B17DA /* AddSubscriptionButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddSubscriptionButton.swift; sourceTree = "<group>"; };
391F0CD6257D9A6900FA3F41 /* ProvidedProvidersViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProvidedProvidersViewModel.swift; sourceTree = "<group>"; };
39290E2C2577C6F9004D9768 /* SubscriptionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionService.swift; sourceTree = "<group>"; };
39290E322577CD64004D9768 /* ProviderServices.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProviderServices.swift; sourceTree = "<group>"; };
3929C7B92571BD0C0012DD51 /* Provider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Provider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -160,7 +162,7 @@
39D9134025716E5F00D9BDFE /* UpdateSubscriptionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateSubscriptionView.swift; sourceTree = "<group>"; };
39D9134A257181AC00D9BDFE /* DaysRingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DaysRingView.swift; sourceTree = "<group>"; };
39E7754225770A9700BB3B31 /* TextFieldModifiers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldModifiers.swift; sourceTree = "<group>"; };
39E7754625770F5300BB3B31 /* saveButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = saveButtonView.swift; sourceTree = "<group>"; };
39E7754625770F5300BB3B31 /* StandardButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StandardButton.swift; sourceTree = "<group>"; };
39EF3A602576723E00777442 /* SubscriptionRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionRepository.swift; sourceTree = "<group>"; };
39F215A025771DA3009332E7 /* SubscriptionFormAlerts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionFormAlerts.swift; sourceTree = "<group>"; };
39F215A3257728F6009332E7 /* TabIconsModifiers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabIconsModifiers.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -603,7 +605,7 @@
children = (
3929C7E12571DBB10012DD51 /* DismissButtonView.swift */,
390E967D257476F3004B17DA /* AddSubscriptionButton.swift */,
39E7754625770F5300BB3B31 /* saveButtonView.swift */,
39E7754625770F5300BB3B31 /* StandardButton.swift */,
390BF4FB2577E6B9007B4BC3 /* BackTrackButton.swift */,
399956542578C1BD000D907D /* AddProviderButton.swift */,
392C38112579205500252032 /* CustomEditButton.swift */,
Expand Down Expand Up @@ -664,6 +666,7 @@
isa = PBXGroup;
children = (
39F7DE4F257B074000D2AB99 /* ProvidedProvidersView.swift */,
391F0CD6257D9A6900FA3F41 /* ProvidedProvidersViewModel.swift */,
);
path = ProvidedProvidersView;
sourceTree = "<group>";
Expand Down Expand Up @@ -807,7 +810,7 @@
3929C7F52571E2370012DD51 /* SubscriptionFormViewModel.swift in Sources */,
390E967925747198004B17DA /* SubscriptionTimingsViewModel.swift in Sources */,
39F215A4257728F6009332E7 /* TabIconsModifiers.swift in Sources */,
39E7754725770F5300BB3B31 /* saveButtonView.swift in Sources */,
39E7754725770F5300BB3B31 /* StandardButton.swift in Sources */,
398A474D257367D700C4212B /* RegisterViewModel.swift in Sources */,
399AD2F62570724B00AFA10A /* SubscriptionCellView.swift in Sources */,
390E9675257460A6004B17DA /* SubscriptionListViewModel.swift in Sources */,
Expand All @@ -825,6 +828,7 @@
397EAB0A2573E81C00E1F7C3 /* LoadingView.swift in Sources */,
39D9134125716E5F00D9BDFE /* UpdateSubscriptionView.swift in Sources */,
3929C7DD2571D8160012DD51 /* User.swift in Sources */,
391F0CD7257D9A6900FA3F41 /* ProvidedProvidersViewModel.swift in Sources */,
399956552578C1BD000D907D /* AddProviderButton.swift in Sources */,
39D9132E25711B9800D9BDFE /* Icons.swift in Sources */,
39F215A125771DA3009332E7 /* SubscriptionFormAlerts.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ final class UserAuthenticationManager : ObservableObject{

DispatchQueue.main.async { self.user = user }
///initing user with initial providers
let givenProviders:[Provider] = [ .init(name: "Spotify", image: Images.Spotify ),
.init(name: "Netflix", image: Images.Netflix ),
.init(name: "Youtube", image: Images.Youtube),
.init(name: "iCloud", image: Images.iCloud),
.init(name: "Amazon", image: Images.amazon ),
.init( name: "Apple Music", image: Images.appleMusic),
.init( name: "Apple TV", image: Images.appleTv) ,
.init(name: "Uber", image: Images.uber) ]
let givenProviders:[Provider] = [ .init(name: "Spotify", image: Images.Spotify ,original:true , deleted:false ),
.init(name: "Netflix", image: Images.Netflix ,original:true , deleted:false ),
.init(name: "Youtube", image: Images.Youtube ,original:true , deleted:false ),
.init(name: "iCloud", image: Images.iCloud ,original:true , deleted:false ),
.init(name: "Amazon", image: Images.amazon ,original:true , deleted:false ),
.init( name: "Apple Music", image: Images.appleMusic ,original:true , deleted:false),
.init( name: "Apple TV", image: Images.appleTv ,original:true , deleted:false) ,
.init(name: "Uber", image: Images.uber ,original:true , deleted:false) ]

for givenProvider in givenProviders{ self.providersRepository.addProvider(provider: givenProvider){_ in} }

Expand Down
16 changes: 9 additions & 7 deletions Billing-Tracker/Models/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ struct Provider : Codable , Identifiable{
var userId : String?
var name:String
var image:String
var original:Bool
var deleted:Bool
}


#if DEBUG
struct Providers{
static let providersList = [
Provider(name: "Spotify", image: Images.Spotify),
Provider(name: "Netflix", image: Images.Netflix),
Provider(name: "Youtube", image: Images.Youtube),
Provider(name: "iCloud", image: Images.iCloud ),
Provider(name: "Amazon", image: Images.amazon ),
Provider( name: "Apple Music", image: Images.appleMusic),
Provider( name: "Apple TV", image: Images.appleTv),
Provider(name: "Spotify", image: Images.Spotify ,original:true , deleted:false),
Provider(name: "Netflix", image: Images.Netflix ,original:true , deleted:false),
Provider(name: "Youtube", image: Images.Youtube ,original:true , deleted:false),
Provider(name: "iCloud", image: Images.iCloud ,original:true , deleted:false),
Provider(name: "Amazon", image: Images.amazon ,original:true , deleted:false),
Provider( name: "Apple Music", image: Images.appleMusic ,original:true , deleted:false),
Provider( name: "Apple TV", image: Images.appleTv ,original:true , deleted:false),
]
}
#endif
Expand Down
55 changes: 49 additions & 6 deletions Billing-Tracker/Repositories/ProviderRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ final class ProviderRepository:ObservableObject{
private let db = Firestore.firestore()
private let collectionName = FirestoreKeys.Collections.providers.rawValue
private let storage = Storage.storage()
@Published var originalProviders = [Provider]()


init(){ self.loadData() }
init(){
self.loadData()
self.getOriginalProviders(){_ in}

}

func loadData(){
if let userId = Auth.auth().currentUser?.uid{
self.db.collection(collectionName)
.order(by: "createdTime")
.whereField("userId", isEqualTo: userId)
.whereField("deleted", isEqualTo: false)
.addSnapshotListener { (querySnapshot, error) in
if let error = error {
fatalError("Error in your network I will customize them for sure \(error.localizedDescription)" )
Expand All @@ -40,7 +46,7 @@ final class ProviderRepository:ObservableObject{
/// add provider
func addProvider(provider:Provider , completion: @escaping (Result <Void, Error>) -> Void){
if let userId = Auth.auth().currentUser?.uid{
let providerHelper = Provider(id: provider.id, createdTime: provider.createdTime, userId: userId, name: provider.name, image: provider.image)
let providerHelper = Provider(id: provider.id, createdTime: provider.createdTime, userId: userId, name: provider.name, image: provider.image , original:provider.original , deleted: provider.deleted)
do{
let _ = try self.db.collection(collectionName).addDocument(from: providerHelper){error in
if let error = error {
Expand All @@ -51,6 +57,7 @@ final class ProviderRepository:ObservableObject{
return
}
}catch {

completion(.failure(error))

}
Expand All @@ -62,7 +69,8 @@ final class ProviderRepository:ObservableObject{
if let _ = Auth.auth().currentUser?.uid{
db.collection(collectionName)
.document(docId)
.delete() { error in
//changing state to deleted
.setData(["deleted": true] , merge: true) { error in
if let error = error {
completion(.failure(error))
return
Expand All @@ -71,11 +79,44 @@ final class ProviderRepository:ObservableObject{
return
}
}

}
// getting providers given by the developer
func getOriginalProviders(completion: @escaping(Result< [Provider] , Error > ) -> Void ){
if let userId = Auth.auth().currentUser?.uid{
db.collection(collectionName)
.whereField("userId", isEqualTo: userId)
.whereField("original", isEqualTo: true)
.addSnapshotListener { (querySnapshot, error) in
if let error = error {
completion(.failure(error))
return
}
if let documents = querySnapshot?.documents{
self.originalProviders = documents.compactMap{ provider in
try? provider.data(as: Provider.self)

}
}
}
}
}

/// update provider name
func updateProvider(){ }

func addOriginalProvider(docId:String , completion: @escaping (Result <Void , Error> )-> Void){
if let _ = Auth.auth().currentUser?.uid{
db.collection(collectionName)
.document(docId)
//changing state to deleted
.setData(["deleted": false] , merge: true) { error in
if let error = error {
completion(.failure(error))
return
}
completion(.success( () ))
return
}
}
}
// upload images
func uploadImage(image:UIImage, providerName: String , completion: @escaping (Result<URL?,Error>)->Void){

Expand All @@ -101,4 +142,6 @@ final class ProviderRepository:ObservableObject{

}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct AddProviderView: View {
Button(action:{
viewModel.addProvider()
}){
saveButtonView(title:"Add")
StandardButton(title:"Add")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct ManageProvidersView: View {
NavigationView {
List{
ForEach(viewModel.providers){ list in
ProviderCellView(provider: list.provider)
ProviderCellView(name: list.provider.name, image: list.provider.image)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ final class ManageProvidersViewModel : ObservableObject{
}
return
}
let helperProvider = Provider(name: self.providerName, image: Images.CustomProvider)
// provider by user
let helperProvider = Provider(name: self.providerName, image: Images.CustomProvider , original : false , deleted: false )

providersRepository.addProvider(provider: helperProvider){result in
switch result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,27 @@
import SwiftUI

struct ProvidedProvidersView: View {
@StateObject var viewModel = ProvidedProvidersViewModel()
var body: some View {
Text("Hello, World!")
NavigationView {
List(viewModel.providerServices){provider in
HStack{
ProviderCellView(name: provider.provider.name , image: provider.provider.image)
Spacer()
Button(action:{
self.viewModel.addProvider(provider: provider)
}){
Image(systemName: Icons.SFAddProvider)
.renderingMode(.original)
.resizable()
.frame(width:32 , height: 32)

}
}
}
.navigationTitle("Providers With Icons 💳")
.navigationBarTitleDisplayMode(.inline)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ProvidedProvidersViewModel.swift
// Billing-Tracker
//
// Created by Nawaf B Al sharqi on 07/12/2020.
//

import Foundation
import SwiftUI
import Combine

final class ProvidedProvidersViewModel : ObservableObject{
// i should bring providedProviders instead of user providers its self
@Published var providerServices = [ProviderServices]()
@Published var providersRepository = ProviderRepository()
@Published var selectedProvider: ProviderServices?
@Published var didSelectProvider = false
private var cancellables = Set<AnyCancellable>()

init(){
self.providersRepository
.$originalProviders
.map{ providers in
providers.map{provider in
ProviderServices(provider: provider)
}

}
.assign(to: \.providerServices, on: self)
.store(in: &cancellables)
}

func addProvider(provider:ProviderServices){
guard let docId = provider.provider.id else {return }

self.providersRepository.addOriginalProvider(docId: docId) { _ in }

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct ProvidersSelectionView:View{
self.viewModel.showProvidersList = false
}){
HStack{
ProviderCellView(provider: list.provider)
ProviderCellView(name: list.provider.name, image: list.provider.image)

if viewModel.selectedProvider?.id == list.provider.id {
Spacer()
Expand Down
7 changes: 4 additions & 3 deletions Billing-Tracker/Screens/SettingView/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct SettingView: View {
@StateObject var viewModel = SettingViewModel()
@State var showCustomProvidersView = false
@State var showReadyProviders = false
var body: some View {
ZStack{
NavigationView {
Expand Down Expand Up @@ -63,7 +64,7 @@ struct SettingView: View {


Button(action:{
showCustomProvidersView.toggle()
showReadyProviders.toggle()
}){
HStack{
Image(systemName: Icons.SFbookmark )
Expand All @@ -74,8 +75,8 @@ struct SettingView: View {
Text("Ready Providers With Icons")
.foregroundColor(.standardText)
}
}.sheet(isPresented: $showCustomProvidersView){
ManageProvidersView()
}.sheet(isPresented: $showReadyProviders){
ProvidedProvidersView()
}
}
Section(header:Text("Support")){
Expand Down
Loading