Sunday, 6 May 2018

Key-Value Observing in Swift 4 Explained

Key-Value Observing (KVO) in Swift 4

Key-Value Observing (KVO), is an important concept in Cocoa API. It allows objects to be notified when the state of another object changes. Here I will explain how you can use this KVO in your swift programming skills where classes are interconnected with each others reference. If there is any change in property defined in custom type such as class then the reference of this custom type in another class should be notified quickly.


Lets do some coding {...}

Consider one small class that has definition as

@objcMembers class FirstClass: NSObject {

    // MARK: - Properties
    var createdAt = Date()
    dynamic var updatedAt = Date()
}


Read with attention that the above class is made "@objMembers" and one property is made "dynamic" that could be notified of value changes. Why because the KVO uses Objective-C runtime. And if you forgot to make the required class as @objMembers and required property as dynamic, the KVO will not work.
So for making your KVO successfull make your required class as @objMembers and required property as dynamic.

The Second class whose instance will be used in our UIViewController is as follows

class SecondClass: NSObject {
   // MARK: - Properties
   var firstClass: FirstClass

   // MARK: -
   lazy private var dateFormatter: DateFormatter = {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
        return dateFormatter
    }()

   // MARK: -
   var createdAt: String {
        return dateFormatter.string(from: firstClass.createdAt)
    }
   var updatedAt: String {
        return dateFormatter.string(from: firstClass.updatedAt)
    }

   // MARK: - Initialization
    init(withFirstClass firstClass: FirstClass) {
        self.firstClass = firstClass
        super.init()
        firstClass.updatedAt = Date()
    }

    // MARK: - Public Interface
    func updateFirstClass() {
        firstClass.updatedAt = Date()
    }
}

Here the actual implementation of our KVO will happen on ViewController in real scenario.

class ViewController: UIViewController {

let secondClassInst = SecondClass(withFirstClass: FirstClass())

//MARK: this array will hold multiple observations realated to multiple property changes of any models present in this ViewController.
var observers = [NSKeyValueObservation]()

//Mark: This lable will show us the values changed via KVO
@IBOutlet weak var label: UILabel!

override func viewDidLoad() {
        super.viewDidLoad()

         //Mark: KVO Observer applied here
        observeModels()
    }
}
func observeModels(){

//Mark: since observers variable is an array that's why it can hold multiple NSKeyValueObservation. 

self.observers = [self.secondClassInst.firstClass.observe(\FirstClass.updatedAt, changeHandler: { (FirstClassIns, change) in
            print("Changes observed on ViewController \(FirstClassIns.updatedAt)")
            DispatchQueue.main.async {
                self.label.text = "\(self.secondClassInst.updatedAt)"
            }
 })]
}

Here in above observeModels() function, I had applied only one observation in this array but it will be your TODO to implement and see the multiple property changes and handled at same time by only creating new array element as per your requirement in the observers array.

//Mark: The below button action will update the property with latest DateTime.
@IBAction func updateAction(_ sender: UIButton) {
        secondClassInst.updateFirstClass()
}



Here You go with the KVO implemented and the O/P will be as follows.


Saturday, 5 May 2018

Handsome Image Gallery with Swipe to Dismiss feature using Swift iOS.

Image Gallery (iOS Swift)

We all love image gallery in our mobile app. There could be requirement / use case also in our app that we want to show the latest feeds or social feeds in our app. There could be multiple images also to be shown on separate screen where a user can download/view the images, surf the multiple images and can download it (if required).

 Image Caching

I had used static images to show in the demo app but you can use remote url downloading and caching technique to show images. The caching is done with md5 generated string for each cached image. you can easily pass remote image url to the extension method written on UIImageView such as loadImage. This function gives you completion block after image is applied to UIImageView and cached also.

imageView.loadImage(urlString: imageURL, completion: { [weak self] (image) in
 //Your code here
})

Supported orientations - Landscape + Portrait

 This demo app supports both orientation such as Landscape, Portrait in the first collection of images screen and gallery also.

Protocol delegate oriented 

The app is has the lovely implementation on protocol, delegate pattern. You only has to pass image names array or url array.  I will soon update it with as framework, pod for easy use.

Extension written on UIViewController

 The Gallery screen is opened as UIViewController and the extension is written on UIViewController for presenting and dismissing it.


This demo app can be used by those who want to integrate this functionality in their app. The source code is open.

The github repo for the source code can be seen here

Below are the screenrecorded gif can be seen. There is fps issue in gif due to large video size. That's why the gif could be seen some jerky but if you clone the repo and run in on simulator/device then it will be smooth experience.


Portrait mode -


Landscape mode -





Thanks :)

Tuesday, 17 April 2018


AWSS3 Transfer Utility Complete Functioning (using Swift iOS)

This demo app requires you to register on AWS and get the Bucket-region, Bucket-name from there to use in your app for further steps.

Features :- 
  1. Single Video/Image upload (With functionality of Pause, Resume, Cancel)
  2. Multiple Videos/Images upload (With each has functionality of Upload, Pause, Resume, Cancel)
  3. See the list of running uploads in-progress.
  4. Mutlipart Video/Image upload.
  5. Get the list of uploaded Videos/Images.
  6. Easily Delete the already uploaded Videos/Images from your bucket

Let's Start With actual codes


1. We require pod file changes such as

pod 'AWSS3'
pod 'AWSMobileClient'


2. Some mandatory changes in info.plist file






3. awsconfiguration.json file that is downloaded from AWS while you created your bucket. Put this file in the same tree directory where your info.plist is present. The below screenshot shows the demo content within it.




4. Some important changes in Appdelegate.swift file.

Here the first method "handleEventsForBackgroundURLSession" will manage  background upload tasks.
Second method will help us to get default AWSServiceManager set up with our Region and PoolId configuration keys.


import AWSS3
import AWSMobileClient

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {

  AWSS3TransferUtility.interceptApplication(application,         handleEventsForBackgroundURLSession: identifier, completionHandler: completionHandler)
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   let credentialsProvider = AWSCognitoCredentialsProvider(regionType:    AWSRegionType.APNorthEast2, identityPoolId: "Your-PoolId-In-String-Format")

   let configuration = AWSServiceConfiguration(region: AWSRegionType.APNorthEast2, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

   return AWSMobileClient.sharedInstance().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)
}



5. Create singleton class with any name (I given UploadManager.swift), It will manage all your upload tasks, handling pause/resume/cancel of each task simultaneously, multiple in-Progress uploads, provides list of current running tasks, handling delete of the uploads from your AWS bucket. We will have enum for type of assest to be uploaded and many more.

enum MediaContentType: String {
   case Video = "movie/mov"
   case Image = "image/png"
}



enum UploadStatus: String {
   case Pause = "Pause"
   case Resume = "Resume"
   case Cancelled = "Cancelled"
   case InProgress = "InProgress"
   case Failed = "Failed"
   case Success = "Success"
}

6. The above class will handle all the status of each running task via single instance variable of Transfer Utility

lazyvar transferUtility: AWSS3TransferUtility = {
   return AWSS3TransferUtility.default()
}()

The above transferUtility give update to the UIViewController/collectionViewController/TableViewController via NotificationCenter. 
You just have to create anonymous callbacks for 3 important callbacks which we got from above transferUtility. These are as follows
  • continuationTaskBlock - It will be fired only once when your upload starts and the uploading task gets TransferId from AWSS3 sdk. This TransferId will be used by your controller to distinguish the uploads which are Initiated by that controller. If you want to save instance of that task for further manipluation then you can put in variable like (var uploadTask: AWSS3TransferUtilityUploadTask). In this closure you will fire a ContinuationNotification.
  • progressBlock - It will be fired multiple times while you video/image is getting uploaded. This block will give  you the progess of upload between 0.0 - 1.0 range. 1.0 will denote that your upload completed successfully. In this closure you will fire a ProgressNotification.
  • completionBlock - It will be fired when your upload completes successfully or with error. In this closure you will fire a CompletionNotification.

7. Custom Model will be useful for developer perspective when he want to show/manage multiple uploads in CollectionView/TableView at a time. 



class UploadModel{
  var tranferId: String = ""
  var progress: Double = 0.0
  var status: String = ""
  var failed: Bool = false
  var completed: Bool = false
  var inProgess: Bool = false
  var cancelled: Bool = false
  var paused: Bool = false
  init(){
   }
}

You can keep shared dictionary with upload file URL as key and above model as Value for distinguishing each upload in your whole app.
e.g. var dict: [String: UploadModel] = ["video1.mov": UploadModel(), "video2.mov": UploadModel(), "image1.png": UploadModel()]
As per the dictionary you can update your views or collectionviewcells/ tableviewcells.

On each notification from progressBlock, continuationBlock, completionBlock you can easily update above dict and as per that dict values your view gets updated.

By using this approach you can easily manage many number of uploads at a time.


8. Key Name (Asset name is important) 

For uploading any asset you have to pass key name for that upload and that key name will be assigned to that asset on your AWS cloud bucket. You can keep that key names in array for future purpose such as delete or download from cloud.


9. Delete Asset 

 For deleting the uploaded asset from AWS bucket you have to only give the key name     which you had set when you started a upload for that asset.

For complete source code you can see the gitHub repo



Sunday, 15 April 2018

Star Rating Control With Effortless Dragging Feature Using Swift (iOS)





An Smooth Draggable Star Rating Control

I have seen many apps those who are not having any rating view inside that. Some are having the feature but not so smooth as per the user's perspective. That's why I have created an easy to use pod for draggable rating view with good user experience.

Some fantastic features:

Easy smooth color change dragging can be seen.

- Any color support with the multiple stars. You can give low opacity to high opacity color to each star for looking awesome color change.

- Using this control in Storyboard or within XIB is very easy and developer can   easily see the changes applied to the control such as star selected color and Rate points in the Interface builder.

- Easy to use within any UIViewController or UIView.

- Developer can change the fill color of Star as per his requirement very easily.

- Easy and strong support of IBDesignable and IBInspectable (You can see the changes very easily in Interface builder).

- Easy and strong support of IBDesignable and IBInspectable (You can see the changes very easily in Interface builder).

- The Rate Points can be easily set in Interface builder and as per that the star will be filled.

- The fill color can be easily changed via Inteface builder or at run time.

- Can easily change colors.

- Gives exact Rate points in float(e.g. 1.2, 2.3, 4.9 etc) as per the your drag position.

- Default two buttons are there such as "Rate Now", "Cancel".

- Supports Multiple buttons (requires an array of string to be passed for button names).

- Easy button click delegates are provided via protocols.

Usage Guide:

You want to add pod 'StarRatingDraggable', '~> 1.0' similar to the following to your podfile:

target 'MyApp' do
    pod 'StarRatingDraggable', '~> 1.0'
end

Then run a pod install inside your terminal or from CocoaPods.app.



Without any Images used :) 

Cocoapod url -  https://cocoapods.org/pods/StarRatingDraggable

For source code please visit my GitHub Url

Below are the screenshots and gif is attached for demonstration purpose.
  1. 
    
    
    
    
    
     
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      
    
    
    
      
    
    
    
    
    
    Thank you so much.
    
    

Saturday, 24 March 2018

Extract .zip directly to external hard drive in MacOS

Easily extract .zip file directly to External HDD / USB / External Storage instead of default SSD or HDD.


If you are having two storage in your Mac machine in which the default is having small storage space and other one is with large space then you obviously want to store files, large projects on big volume and your working softwares and compilers on default volume.
Below is the example demonstrated (storage for example purpose only).

  1. SSD (120 GB) - 
     2. HDD (External 500 GB) -  Archive.zip    ( <--- it's the zip to be extracted, and we want it to be extracted on this location only where the zip is present)

But when you click it to extract then it extracts to the default SSD storage at the location ~/Downloads/ with extracted folder named as "Archive".  (This is the actual problem we are facing when extracting the .zip file whose actual size is 100GB and we are having small storage of default SSD)

At this point we want to extract the .zip file to the external storage only.

Open the Terminal app from Spotlight search or from Applications -> Utilities -> Terminal and navigate to your folder where the .zip file is stored with "cd ~/locationOf/Acrchive.zip" and press Enter.

And extract the ZIP file with the OSX command line tool 

unzip /ExtrenalHDD/Folder/Archive.zip -d /Volumes/ExtrenalHDD/FolderName

In the above command after typing /Volumes press double Tab and you can see the list of Volumes present such as SSD, HDD, USB etc. The the second path  /Volumes/ExtrenalHDD/FolderName is the location where you want to extract the zip.

Thanks You





Sunday, 11 March 2018

List Of Trending Software Development Blogs

In the today's passion for technology every school or college student or any software professional who wants to learn software coding, searches online for extremely good blogs for practice.
Here are the list of top blogs which could be better for all those who want to learn Software development briefly.


The top 20 Software development blogs are listed below


1. Dzone
One of the best software development blogs is Dzone. Web, where you will find all necessary information about agile software development. Tutorials, guidelines, tools and software insights for beginners and experts. Hundreds of publications, over 1M members. Top topics: Agile, Big data, Cloud, Data base, Devops, Integraton, IOT, Java, Mobile, Performance, Security and many more.


A computer science portal for geeks. It contains well written, well thought and well explained computer science and programming articles.


Place for in-depth development tutorials and new technology trends. Here you will find latest information about Back-end, Web front-end, Mobile, agile project management, etc.


This blog is about software development & software architecture. Here you will find useful information about: Agile, Backend, IOS, Android, DDD, TDD, CI, SOLID, Unit testing, microservices, docker, natural language processing, Reactive, Javascript, PHP, Scala, Nodejs, Angularjs and many many more.

This blog has tutorials for iOS and Android developers. Tips and best practices for swift and mac lovers. Unit testing, Unity, C#, Json and many more.

Awesome blog for Agile lovers. It has some practical tips and approaches for agile software development; testing, integration, etc. Here you may find announcements, journal entries, status reports, news, trends, tricks and tips and many more.

Best place to find examples of clean code. Also, you will see DevOps tools and best software development practices.

Scotch is a web development blog discussing all programming topics. Most popular ones are: Javascript, Angular 2, Node.JS, Docker, PHP, etc.

This blog you will love if you have a good sense of humor as it’s posts often include funny asides on the human aspects of coding. Also you will find articles discussing recent advances in the tech world.

As you could guess from the name, here you will find all useful information about Docker. Case studies, tutorials, examples, updates, news and many more. Everything you need to be convinced using docker, but using it right.

Here you may actually learn Java online. You will find Android development tutorials, Java tutorials for beginners, Java books, Scala, Kotlin, Groovy and JRuby news, code examples and many more.

Useful blog with articles about Java, .NET, PHP, Javascript, C++ and many more.

Here it is a bit easy to guess again. This blog is about JavaScript, about React. All necessary tips and advices you may get there to be sure your Javascript project is going in the right direction.

In this blog engineers of Twitter share their case studies, their tips, their findings and pitfalls. This blog is based on real examples, real projects. Here you may really learn from one of the best engineer teams in the world and know which tools they use and which methodology they follow.

This blog is about Scrum; best practices and tools. Here you will find success stories, tutorials, examples, statistics and other useful articles to improve team’s productivity and efficiency of the project.

Again quite easy to guess that this blog is about Scala. Scala updates, Scala best practices, Scala tips and examples.


17. Devhumor
Yes, this one is related to software humor. Memes, pictures, jokes, code and other stuff from real life that make you laugh.

Offers daily posts of user-submitted examples of bad code and software design. Good place to find examples and explanation of curious perversions in Information Technology. Basically, it is “how not to” guide for developing software. They recount tales of disastrous development, from project management gone spectacularly bad to inexplicable coding choices.

Blog with interesting articles about Java, Performance Solutions, agile, software architecture, continuous delivery, cloud and many more.

Engineering blog for Node.js and JavaScript lovers. TDD, callback hell, clean coding and other buzz and important topics are discussed here.

  • Also make profile on Stackoverflow.com and search daily for any type of stuff related to software development, Networking, Hardware related questions.



All the Best for the future.