DEV Community

Kien Nguyen Chi
Kien Nguyen Chi

Posted on

OSD600 Final Project Blog 3

Introduction

🔥This is a mini-series including 3 blogs that shows the process how I work on my final project in my Open-Source Development class (OSD600) at Seneca College.

📝In the first blog, I introduced how I found an open-source repo to work for Final Project. In the second blog, I introduced my process of installation and setup the app. In this last blog, I will introduce my process to implement the feature and how the result looks like.

Process

First Impression

It was a wow when I first looked at the code of the repo. It contains so many folders and functionalities. Some folders had the views of the features and code of functionalities. Some folders contained classes and database connection.
Image description
I did not know where to start. So, I picked Category folder to read the code first because my feature implemented is about Pin Category.

Difficulties

I decided to add an attribute to Category class named pinned. I planned to mark it as a boolean. I would set it to be true whenever user pins, and set it to be false for default value. And I would display all the pinned categories on top.

public struct AWSCategory: Model {
  public let id: String
  public var name: String
  public var imageName: String?
  public var tweets: List<AWSTweet>?
  public var pinned: Bool?

  public init(id: String = UUID().uuidString,
      name: String,
      imageName: String? = nil,
      tweets: List<AWSTweet>? = []
      pinned: Bool) {
      self.id = id
      self.name = name
      self.imageName = imageName
      self.tweets = tweets
      self.pinned = pinned
  }
}
Enter fullscreen mode Exit fullscreen mode

That was be the best shortcut for me to implement pin feature. But it would change the structure of the whole database system. So I needed to figure out other way.

Implementation

I created a button Pin to top whenever users press-hold on any category.

Button {
     viewModel.pinCategory(category: category)
} label: {
     Text("Pin to top")
}
Enter fullscreen mode Exit fullscreen mode

The button would show the text Pin to top. And when users hold-press it. It would call pinCategory() accepting parameter of current row category to do the functionality.

In pinCategory() function, firstly, I made a loop to found the index of the category in the array.

func pinCategory(category: AWSCategory){
   var categoryIndex = -1;
   for (index, each) in categories.enumerated() {
        if(each.id == category.id){
           categoryIndex = index
        }
   }
   ...
}
Enter fullscreen mode Exit fullscreen mode

If the index found is valid, I would remove the current category from the array, insert it to the front. So, when the page is reloaded, it would appear on the top of the list.

if(categoryIndex != -1){
    categories.remove(at: categoryIndex)
    categories.insert(category, at: 0)
    ...
}
Enter fullscreen mode Exit fullscreen mode

categories is the array of the imported categories from the database. So, I needed to update the categories to the database when I had done my feature. I had a chance to understand how to implement AWI Amplify Datastore. I read the articles and followed the implementation in /Managers/DataStoreManager.swift to see how actually this database system works.

for category in categories {
   DataStoreManger.shared.deleteCategory(category: category)
}
for category in categories {
   DataStoreManger.shared.createCategory(category: category)
}
Enter fullscreen mode Exit fullscreen mode

Result

This is 3 folders that I created:
Image description
When I hold-press on Folder 2, it will show:
Image description
And here is the final result:
Image description
When I close the app and reopen it, the categories are saved as before. I ensured the persistence of database. Folder 2 is still on top.

Conclusion

💻This has been the first open-source project that I spent so much time in it. From the process to find interesting project to work on, to the installation process and finally implementation process.

💪I'm happy to see my overall process and I'm looking forward to push myself in the open-source world.

Sources

📚Read my mini-series at:

💻Take a look at my work:

Top comments (0)