Giter Club home page Giter Club logo

contactslbta's Introduction

ContactsLBTA

TVC. fecth contacts and display... learn animation,

(1) this code is for creating cell UicollectionView Cell

    right accessory view is button view fot set star for like and dislike code
    import UIKit
    class ContactCell: UITableViewCell {
        
        // create refrence of View Controller and pass this whole cell to View Controller
        var link: ViewController?

            override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)

    //        backgroundColor = .red

            // kind of cheat and use a hack
            let starButton = UIButton(type: .system)
            starButton.setImage(#imageLiteral(resourceName: "fav_star"), for: .normal)
            starButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50)

            starButton.tintColor = .red
            starButton.addTarget(self, action: #selector(handleMarkAsFavorite), for: .touchUpInside)

            accessoryView = starButton
        }

        @objc private func handleMarkAsFavorite() {
    //        print("Marking as favorite")
            link?.someMethodIWantToCall(cell: self)
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

    }

(2) when cell is passed to this methods. get particular cell index

    func someMethodIWantToCall(cell: UITableViewCell) {
    // print("Inside of ViewController now...")
    
    // we're going to figure out which name we're clicking on
    
    // get particular cell index 
    
    guard let indexPathTapped = tableView.indexPath(for: cell) else { return }
    
    let contact = twoDimensionalArray[indexPathTapped.section].names[indexPathTapped.row]
    print(contact)
    
    let hasFavorited = contact.hasFavorited
    // toggle logic fot like dislike button..
    twoDimensionalArray[indexPathTapped.section].names[indexPathTapped.row].hasFavorited = !hasFavorited
    
    // tableView.reloadRows(at: [indexPathTapped], with: .fade)
    // then change accessory view controller tint color
    cell.accessoryView?.tintColor = hasFavorited ? UIColor.lightGray : .red
    }

(3) fetch contacts from contact diary using this function

// first import this framework
import Contacts

//  private func fetchContacts()
   {
    print("Attempting to fetch contacts today..")
    
    // create object of this class
    let store = CNContactStore()
    
    // then send request 
    store.requestAccess(for: .contacts) { (granted, err) in
        if let err = err {
            print("Failed to request access:", err)
            return
        }
        
        // if permission is granted  then below code execute. 
        if granted {
            print("Access granted")
            
            // provide array keys for fetching contacts details.. name, family, phone number
            
            let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
            
            // make contact request by providing keys
            let request = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
            
            do {
                
                var favoritableContacts = [FavoritableContact]()
                
                // get all contacts using this method 
                try store.enumerateContacts(with: request, usingBlock: { (contact, stopPointerIfYouWantToStopEnumerating) in
                    
                    print(contact.givenName)
                    print(contact.familyName)
                    print(contact.phoneNumbers.first?.value.stringValue ?? "")
                    
                    favoritableContacts.append(FavoritableContact(contact: contact, hasFavorited: false))
                    
                //  favoritableContacts.append(FavoritableContact(name: contact.givenName + " " + contact.familyName, hasFavorited: false))
                })
                
                let names = ExpandableNames(isExpanded: true, names: favoritableContacts)
                self.twoDimensionalArray = [names]
                
            } catch let err {
                print("Failed to enumerate contacts:", err)
            }
            
        } else {
            print("Access denied..")
        }
    } 
  }

contactslbta's People

Contributors

bhlvoong avatar iamhspatel2160419 avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.