Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PackageDescription

let package = Package(
name: "MMRatingView",
platforms: [.iOS(.v13)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand All @@ -20,7 +21,7 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "MMRatingView",
dependencies: []),
dependencies: [], resources: [.process("MMRatingAssets")]),
.testTarget(
name: "MMRatingViewTests",
dependencies: ["MMRatingView"]),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# MMRatingView

A description of this package.
Customisable Rating View for swiftUI.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "sports-baseball-24px.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "sports-baseball-outlined-24px.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "sports-basketball-24px.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "sports-basketball-outlined-24px.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "sports-football-24px.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"images" : [
{
"filename" : "sports-football-outlined-24px.svg",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"preserves-vector-representation" : true,
"template-rendering-intent" : "template"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions Sources/MMRatingView/MMRatingView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// MMRatingView.swift
//
//
// Created by Manish Patidar on 28/08/22.
//

import SwiftUI

/// A view of inline images that represents a rating.
/// Tapping on an image will change it from an unfilled to a filled version of the image.
///
/// The following example shows a MMRatingView with a maximum rating of 10 red flags, each with a width of 20:
///
/// MMRatingView(maxRating: 10,
/// currentRating: $currentRating,
/// width: 20,
/// color: .red,
/// ratingImage: .flag)
///
///
public struct MMRatingView: View {

@Binding var currentRating: Int

var maxRating: Int
var width:Int
var color: UIColor
var ratingImage: RatingImageType

/// Only two required parameters are maxRating and the binding to currentRating. All other parameters have default values
/// - Parameters:
/// - maxRating: The maximum rating on the scale
/// - currentRating: A binding to the current rating variable
/// - width: The width of the image used for the rating (Default - 20)
/// - color: The color of the image ( (Default - systemYellow)
/// - ratingImage: An enum representing the image used for the rating (Default - .star)
///
public init(maxRating: Int,
currentRating: Binding<Int>,
width: Int = 20,
color: UIColor = .systemYellow,
ratingImage: RatingImageType = .star) {
self.maxRating = maxRating
self._currentRating = currentRating
self.width = width
self.color = color
self.ratingImage = ratingImage
}

public var body: some View {
HStack {
ForEach(0..<maxRating, id: \.self) { rating in
correctImage(for: rating)
.resizable()
.scaledToFit()
.foregroundColor(Color(color))
.onTapGesture {
self.currentRating = rating+1
}
}
}
.frame(width: CGFloat(maxRating * width))
}

private func correctImage(for rating: Int) -> Image {
if rating < currentRating {
return ratingImage.fillImage
} else {
return ratingImage.openImage
}
}
}

struct MMRatingView_Previews: PreviewProvider {
static var previews: some View {
MMRatingView(maxRating: 5, currentRating: .constant(3))
.environment(\.colorScheme, .light)
.previewLayout(.sizeThatFits)
.padding(10)
}
}

41 changes: 41 additions & 0 deletions Sources/MMRatingView/RatingImageType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// MMRatingView.swift
//
// Created by Manish Patidar on 28/08/22.
//

import SwiftUI

public enum RatingImageType: String {

case star = "SF_star"
case heart = "SF_heart"
case thumbsUp = "SF_hand.thumbsup"
case bookmark = "SF_bookmark"
case flag = "SF_flag"
case bell = "SF_bell"

case baseball
case basketball
case football
case custom

var fillImage: Image {
rawValue.prefix(3) == "SF_" ?
Image(systemName: String(rawValue.dropFirst(3)) + ".fill")
:
rawValue == "custom" ?
Image("\(rawValue).fill")
:
Image("\(rawValue).fill", bundle: .module)
}
var openImage: Image {
rawValue.prefix(3) == "SF_" ?
Image(systemName: String(rawValue.dropFirst(3)))
:
rawValue == "custom" ?
Image(rawValue)
:
Image(rawValue, bundle: .module)
}
}