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

reload specific card inside CardPartsViewController by calling loadSpecificCards #280

Open
zhenyiy opened this issue Jan 7, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@zhenyiy
Copy link

zhenyiy commented Jan 7, 2022

I'd like to reload one specific card after I get the data update from an async restful process. However, loadSpecificCards function is not available under the CardPartsViewController. I am wondering what's the correct method to achieve that. Also, I am wondering what the "indexPath" in the function is. Is it possible for someone to give an example? Thank you very much!

import Foundation
import CardParts
import RxSwift

class CardRecommendationController: CardPartsViewController {
    
    private var diseaseClient: DiseaseClient?
    private var progressManager: ProgressManager?
    private var currentPage = 0
    private var timer: Timer?
    
    let cardPartTextView = CardPartTextView(type: .normal)
    var recommendations = BehaviorSubject(value: ["1", "2"])
    var recommendationImages: [UIImage?] = [UIImage(systemName: "heart"), UIImage(systemName: "lungs")]
    let imageRecommendation = CardPartImageView()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.progressManager = ProgressManager()
        self.diseaseClient = DiseaseClient()
        
        cardPartTextView.text = "Top Health Threats in the Country"
        cardPartTextView.font = UIFont.systemFont(ofSize: 18)
        cardPartTextView.textColor = .darkGray
        
        self.diseaseClient?.getDiseaseData(country: "US"){[unowned self] (diseaseData, error) in
                  if error != nil {
                    self.progressManager?.stopLoading()
                    return
                  }
                  if let diseaseData = diseaseData {
                    let viewModel3 = DiseaseViewModel(model: diseaseData)
                      viewModel3.cause.asObservable().bind(to: recommendations).disposed(by: bag)
                      var stackViews: [CardPartStackView] = []
                      do {
                          for (index, recommedationString) in try recommendations.value().enumerated() {
                              
                              let sv = CardPartStackView()
                              sv.axis = .vertical
                              sv.spacing = 8
                              stackViews.append(sv)
                              
                              let strs = recommedationString.components(separatedBy: "|")
                              
                              let image = CardPartImageView()
                              image.image = recommendationImages[index]
                              image.contentMode = .scaleAspectFit
                              image.tintColor = .lightGray
                              sv.addArrangedSubview(image)
                              
                              let recom1 = CardPartTextView(type: .normal)
                              recom1.text = String(index * 5 + 1) + ". " + strs[0]
                              recom1.textAlignment = .center
                              sv.addArrangedSubview(recom1)
                              
                              let recom2 = CardPartTextView(type: .normal)
                              recom2.text = String(index * 5 + 2) + ". " + strs[1]
                              recom2.textAlignment = .center
                              sv.addArrangedSubview(recom2)

                              let recom3 = CardPartTextView(type: .normal)
                              recom3.text = String(index * 5 + 3) + ". " + strs[2]
                              recom3.textAlignment = .center
                              sv.addArrangedSubview(recom3)
                              
                              let recom4 = CardPartTextView(type: .normal)
                              recom4.text = String(index * 5 + 4) + ". " + strs[3]
                              recom4.textAlignment = .center
                              sv.addArrangedSubview(recom4)

                              let recom5 = CardPartTextView(type: .normal)
                              recom5.text = String(index * 5 + 5) + ". " + strs[4]
                              recom5.textAlignment = .center
                              sv.addArrangedSubview(recom5)
                          }
                      } catch {
                          
                      }
                      
                      let cardPartPagedView = CardPartPagedView(withPages: stackViews, andHeight: 200)
                      
                      // To animate through the pages
                      timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: {[weak self] (_) in
                          
                          guard let this = self else { return }
                          do {
                              let recomSize = try this.recommendations.value().count
                              this.currentPage = this.currentPage == recomSize - 1 ? 0 : this.currentPage + 1
                          } catch{
                              
                          }
                          cardPartPagedView.moveToPage(this.currentPage)
                      })
                      
                      setupCardParts([cardPartTextView, cardPartPagedView])

                     // I want to call loadSpecificCards(cards: <#T##[CardController]#>, indexPaths: <#T##[IndexPath]#>) method here 
                    //  but it is not available for CardPartsViewController. What should be the best practice?


                      self.progressManager?.stopLoading()
                  }
        }
        
    }
}
@zhenyiy zhenyiy added the enhancement New feature or request label Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant