Friday, August 24, 2018

Collection view cell not appear after resize the CollectionView height

I am facing the UICollectionView cell size issue. I am using two collection_view and one table_view 1:- Main CollectionView ,which is use for swipe and only display one cell at one time 2:- Inside Main Collection_View, i use table_view and now further inside table_view I am using Collection view which display 6 cell at one time.

Now the whole process is I am using a layout which is swipable and display 6 item at one page and so on.

The problem is when I set main cell height 200 then inside collectionview cell not appearing as expected.

Outer CollectionView Class

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var collectionMain: UICollectionView!
    @IBOutlet weak var tableView: UITableView!
    let itemsPerRow: CGFloat = 1
    let sectionInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

extension ViewController:UICollectionViewDataSource,UICollectionViewDelegate {
    //1
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    //2
    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    //3
    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:cellCollectionCollectionViewCell! = collectionView.dequeueReusableCell(withReuseIdentifier: "cellSwipe",
                                                                                        for: indexPath) as! cellCollectionCollectionViewCell

        switch indexPath.item {
        case 0:
            cell.backgroundColor = UIColor.black
            cell.setupCollectionTableCell(indexPath)
            break
        case 1:  cell.backgroundColor = UIColor.red
        cell.setupCollectionTableCell(indexPath)
            break

        default:
            cell.backgroundColor = UIColor.yellow
            cell.setupCollectionTableCell(indexPath)
            break
        }
        // Configure the cell
        return cell
    }


}
extension ViewController : UICollectionViewDelegateFlowLayout {
    //1
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        //2
        let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
        let availableWidth = self.collectionMain.frame.width - paddingSpace
        let widthPerItem = availableWidth / itemsPerRow

        return CGSize(width: widthPerItem, height: widthPerItem)
    }

    //3
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets {
        return sectionInsets
    }

    // 4
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return sectionInsets.left
    }
}

Model Constant class

import UIKit

class Constant {
    static let totalItem: CGFloat = 9
    static let column: CGFloat = 3

    static let minLineSpacing: CGFloat = 1.0
    static let minItemSpacing: CGFloat = 1.0

    static let offset: CGFloat = 1.0 // TODO: for each side, define its offset

    static func getItemWidth(boundWidth: CGFloat) -> CGFloat {

        let totalWidth = boundWidth - (offset + offset) - ((column - 1) * minItemSpacing)

        return totalWidth / column        
    }
}

Table Cell Class

import UIKit

class CollectionCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()

        if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.sectionInset = UIEdgeInsetsMake(
                Constant.offset,    // top
                Constant.offset,    // left
                Constant.offset,    // bottom
                Constant.offset     // right
            )

            layout.minimumInteritemSpacing = Constant.minItemSpacing
            layout.minimumLineSpacing = Constant.minLineSpacing
        }

        collectionView.isScrollEnabled = false
        collectionView.dataSource = self
        collectionView.delegate = self
    }

}

extension CollectionCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Int(Constant.totalItem)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BoxCell", for: indexPath)
        return cell
    }
}

extension CollectionCell: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let itemWidth = Constant.getItemWidth(boundWidth: collectionView.bounds.size.width)

        return CGSize(width: itemWidth, height: 50)
    }
}

Main CollectionCell class

import UIKit

class cellCollectionCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var tblView: UITableView!
    var indexPath:IndexPath?

    func setupCollectionTableCell(_ row:IndexPath){

            self.indexPath = row
            self.tblView.delegate = self
            self.tblView.dataSource = self
            //   self.tblView.estimatedRowHeight = 300
            self.tblView.reloadData()

    }
}
extension cellCollectionCollectionViewCell:UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.row {
        case 0:
            return 50
        case 1:
            let itemHeight = Constant.getItemWidth(boundWidth: tableView.bounds.size.width)

            let totalRow = ceil(Constant.totalItem / Constant.column)

            let totalTopBottomOffset = Constant.offset + Constant.offset

            let totalSpacing = CGFloat(totalRow - 1) * Constant.minLineSpacing

            let totalHeight  = ((itemHeight * CGFloat(totalRow)) + totalTopBottomOffset + totalSpacing)
            print("total height  \(totalHeight)")

            return totalHeight
        default:
            return UITableViewAutomaticDimension
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        switch indexPath.row {
//        case 0:
//            let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell", for: indexPath)
//            return cell
//        default:
//            let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
//            return cell
//        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
        return cell

    }

When I set height of Main Collection view 200

Bellow are the link of images

When I set height 300, working fine

Monday, August 20, 2018

BayesFactor Package R: Two different Output

I'm using the BayesFactor package but I get two different output for the same data using two very similar codes. I'm wondering which one is correct?

if(!require(BayesFactor)){install.packages('BayesFactor')}

require(BayesFactor)
##################################################

exp(ttest.tstat(t= 2 , n1=40, n2=40, nullInterval =c(0, Inf), rscale = sqrt(2)/2,
        complement = FALSE, simple = FALSE)$bf)   ### !CHECK THIS OUTPUT! ###


exp(ttest.tstat(t= 2 , n1=40, n2=40, nullInterval =c(0, Inf), rscale = sqrt(2)/2, 
       complement = FALSE, simple = TRUE))    ### !CHECK THIS OUTPUT! ###

Solved

It is mentioned in the documentation of ?ttest.tstat

If simple is TRUE, returns the Bayes factor (against the null). If FALSE, the function returns a vector of length 3 containing the computed log(e) Bayes factor, along with a proportional error estimate on the Bayes factor and the method used to compute it.

In essence, we don't need to take the exp when simple=TRUE

ttest.tstat(t= 2 , n1=40, n2=40, nullInterval =c(0, Inf),
   rscale = sqrt(2)/2, complement = FALSE, simple = TRUE)
#     B10 
# 2.502954 

whereas with simple=FALSE

exp(ttest.tstat(t= 2 , n1=40, n2=40, nullInterval =c(0, Inf),
        rscale = sqrt(2)/2, complement = FALSE, simple = FALSE)$bf)
#[1] 2.502954

Sunday, August 19, 2018

Deploy Identity Provider with CloudFormation or SAM

I have a CodeStar project, using console I've created an identity provider and an identity pool for OpenId Connect. This works and I can authenticate all my resources. I would turn this manual process into something more automated. The first two things needed are

  • create an Identity Provider
  • create an Identity Pool for that provider.

The ideal solution would be, add few instructions to template.yml in the code star project, so each time template is modified the identity provider will be modified accordingly. I tried to look at SAM documentation and doesn't seem to have something usefull for Identity Provider creation. I've look at CloudFormation Designer and seems there's nothing for this need. How is it possible?

How can I automate and put this information under source control?

Solved

CloudFormation doesn't have support for OpenID or SAML IdPs. However, all of the AWS SKDs do. I suggest looking into the lambda-backed custom resources for CloudFormation.

The custom resource lambda function could then have your own implementation of OpenID provider creation. See the AWS documentation for Python or Java SKD calls.


Saturday, August 18, 2018

How to return the letters of all partitions on a drive in CMD/Batch?

My goal is to retrieve the letters of every partition and loop through those to complete another task on each partition using that letter as a variable. Currently right now I have...

wmic logicaldisk get caption

which returns...

Caption  
C:       
D:       

Now how do I take that output, and take only C, then D, as a variable for a batch script?

Thank you very much, appreciate the help.

Solved

Probable code example:

@Echo Off
For /F "Skip=1 Delims=:" %%A In ('WMIC LogicalDisk Get Caption') Do Echo=%%A:
Timeout -1

Change Echo= to an appropriate command as necessary.