Skip to content

Commit

Permalink
-Languages and Theme Enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
KhubaibKhan4 committed Feb 3, 2025
1 parent f667610 commit b5b42de
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 144 deletions.
Binary file not shown.
17 changes: 15 additions & 2 deletions Notes/NotesApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ import SwiftUI

@main
struct NotesApp: App {
@AppStorage("selectedCountry") private var storedCountry: String = "en"

@StateObject var appManager: AppManager = AppManager()



var body: some Scene {
WindowGroup {
ContentView()
.modelContainer(for: [NotesItem.self,TodoItem.self])
.environment(\.locale, Locale(identifier: storedCountry))
.environment(\.locale, Locale(identifier: appManager.appLanguage))
.environmentObject(appManager)
.preferredColorScheme(appManager.isDark ? .dark : .light)
.environment(\.locale, .init(identifier: appManager.appLanguage))
.environment(\.layoutDirection, isRTL(langauge: appManager.appLanguage) ? .rightToLeft : .leftToRight)
}
}

func isRTL(langauge: String) -> Bool {
let rtlLanguage = ["ar", "he", "fa", "ur"]
return rtlLanguage.contains(langauge)
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct HomeScreen: View{
NavigationView {
VStack{
if searchResults.isEmpty {
ContentUnavailableView.init("No Notes Found", systemImage: "text.page.badge.magnifyingglass", description: Text("No Notes Found in the Database. Please try to research other items."))
ContentUnavailableView.init(LocalizedStringKey("No Notes Found"), systemImage: "text.page.badge.magnifyingglass", description: Text(LocalizedStringKey("No Notes Found in the Database. Please try to research other items.")))

}else{
List{
Expand All @@ -61,7 +61,7 @@ struct HomeScreen: View{
Button {
togglePin(for: item)
} label: {
Label(item.isPinned ? "Unpin" : "Pin", systemImage: item.isPinned ? "pin.slash.fill" : "pin.fill")
Label(item.isPinned ? LocalizedStringKey("Unpin") : LocalizedStringKey("Pin"), systemImage: item.isPinned ? "pin.slash.fill" : "pin.fill")
}
.tint(.yellow)
}
Expand All @@ -78,7 +78,7 @@ struct HomeScreen: View{
Button {
togglePin(for: item)
} label: {
Label(item.isPinned ? "Unpin" : "Pin", systemImage: item.isPinned ? "pin.slash.fill" : "pin.fill")
Label(item.isPinned ? LocalizedStringKey("Unpin") : LocalizedStringKey("Pin"), systemImage: item.isPinned ? "pin.slash.fill" : "pin.fill")
}
.tint(.yellow)
}
Expand All @@ -95,7 +95,7 @@ struct HomeScreen: View{
}
}
.preferredColorScheme(isDark ? .dark : .light)
.navigationTitle("Notes")
.navigationTitle(LocalizedStringKey("Notes"))
.searchable(text: $searchText, isPresented: $isSearchPresented)
.toolbar {
ToolbarItem(placement:.topBarTrailing) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ struct SettingScreen: View {
@Environment(\.requestReview) private var requestReview
@Environment(\.colorScheme) private var colorScheme

@AppStorage("isDarkMode") private var isDark : Bool = false
@State private var themeText: String = "Light Mode"

@EnvironmentObject private var appManager: AppManager

var body: some View {
NavigationView {
Form {
Expand All @@ -30,17 +31,17 @@ struct SettingScreen: View {
}

Section("Color Scheme") {
Toggle(isOn: $isDark) {
Toggle(isOn: $appManager.isDark) {
Text(themeText)
}.toggleStyle(.switch)
.onChange(of: isDark) { oldValue, newValue in
.onChange(of: appManager.isDark) { oldValue, newValue in
print("Color Scheme Changed \(newValue)")
if newValue {
themeText = "Dark Mode"
}else {
themeText = "Light Mode"
}
isDark = newValue
appManager.isDark = newValue
}
}

Expand All @@ -57,14 +58,13 @@ struct SettingScreen: View {
Label("Privacy Policy", systemImage: "shield.fill")
}
}

}.onAppear {
themeText = isDark ? "Dark Mode" : "Light Mode"
themeText = appManager.isDark ? "Dark Mode" : "Light Mode"
}
}
.preferredColorScheme(isDark ? .dark : .light)
.preferredColorScheme(appManager.isDark ? .dark : .light)
.navigationTitle("Setting")

}
}
}
145 changes: 145 additions & 0 deletions Notes/presentation/screens/setting/SettingView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//
// SettingView.swift
// Notes
//
// Created by Muhammad Khubaib Imtiaz on 03/11/2024.
//

import SwiftUI

struct CountrySelectorView: View {
@State private var searchText: String = ""
@EnvironmentObject var appManager: AppManager
@State private var selectedCountry: Country? = nil
@State private var countries: [Country] = [
Country(name: "United States", flag: "🇺🇸", language: "English", languageCode: "en"),
Country(name: "Canada", flag: "🇨🇦", language: "French", languageCode: "en, fr"),
Country(name: "United Kingdom", flag: "🇬🇧", language: "English", languageCode: "en"),
Country(name: "India", flag: "🇮🇳", language: "Hindi", languageCode: "hi, en"),
Country(name: "Germany", flag: "🇩🇪", language: "German", languageCode: "de"),
Country(name: "Australia", flag: "🇦🇺", language: "English", languageCode: "en"),
Country(name: "Japan", flag: "🇯🇵", language: "Japanese", languageCode: "ja"),
Country(name: "France", flag: "🇫🇷", language: "French", languageCode: "fr"),
Country(name: "Brazil", flag: "🇧🇷", language: "Portuguese", languageCode: "pt"),
Country(name: "Russia", flag: "🇷🇺", language: "Russian", languageCode: "ru"),
Country(name: "China", flag: "🇨🇳", language: "Mandarin", languageCode: "zh"),
Country(name: "South Korea", flag: "🇰🇷", language: "Korean", languageCode: "ko"),
Country(name: "Italy", flag: "🇮🇹", language: "Italian", languageCode: "it"),
Country(name: "Spain", flag: "🇪🇸", language: "Spanish", languageCode: "es"),
Country(name: "Mexico", flag: "🇲🇽", language: "Spanish", languageCode: "es"),
Country(name: "Saudi Arabia", flag: "🇸🇦", language: "Arabic", languageCode: "ar"),
Country(name: "Sweden", flag: "🇸🇪", language: "Swedish", languageCode: "sv"),
Country(name: "Norway", flag: "🇳🇴", language: "Norwegian", languageCode: "no"),
Country(name: "Netherlands", flag: "🇳🇱", language: "Dutch", languageCode: "nl"),
Country(name: "Turkey", flag: "🇹🇷", language: "Turkish", languageCode: "tr"),
Country(name: "South Africa", flag: "🇿🇦", language: "Afrikaans", languageCode: "af, en"),
Country(name: "Egypt", flag: "🇪🇬", language: "Arabic", languageCode: "ar"),
Country(name: "Thailand", flag: "🇹🇭", language: "Thai", languageCode: "th"),
Country(name: "Vietnam", flag: "🇻🇳", language: "Vietnamese", languageCode: "vi"),
Country(name: "Argentina", flag: "🇦🇷", language: "Spanish", languageCode: "es"),
Country(name: "Nigeria", flag: "🇳🇬", language: "English", languageCode: "en"),
Country(name: "Indonesia", flag: "🇮🇩", language: "Indonesian", languageCode: "id"),
Country(name: "Pakistan", flag: "🇵🇰", language: "Urdu", languageCode: "ur, en"),
Country(name: "Bangladesh", flag: "🇧🇩", language: "Bengali", languageCode: "bn"),
Country(name: "Philippines", flag: "🇵🇭", language: "Filipino", languageCode: "fil, en"),
Country(name: "Malaysia", flag: "🇲🇾", language: "Malay", languageCode: "ms"),
Country(name: "Iran", flag: "🇮🇷", language: "Persian", languageCode: "fa"),
Country(name: "Iraq", flag: "🇮🇶", language: "Kurdish", languageCode: "ar, ku"),
Country(name: "Israel", flag: "🇮🇱", language: "Hebrew", languageCode: "he"),
Country(name: "Portugal", flag: "🇵🇹", language: "Portuguese", languageCode: "pt"),
Country(name: "Poland", flag: "🇵🇱", language: "Polish", languageCode: "pl"),
Country(name: "Czech Republic", flag: "🇨🇿", language: "Czech", languageCode: "cs"),
Country(name: "Hungary", flag: "🇭🇺", language: "Hungarian", languageCode: "hu"),
Country(name: "Romania", flag: "🇷🇴", language: "Romanian", languageCode: "ro"),
Country(name: "Ukraine", flag: "🇺🇦", language: "Ukrainian", languageCode: "uk"),
Country(name: "Greece", flag: "🇬🇷", language: "Greek", languageCode: "el"),
Country(name: "Serbia", flag: "🇷🇸", language: "Serbian", languageCode: "sr"),
Country(name: "Switzerland", flag: "🇨🇭", language: "French", languageCode: "de, fr, it"),
Country(name: "Denmark", flag: "🇩🇰", language: "Danish", languageCode: "da"),
Country(name: "Finland", flag: "🇫🇮", language: "Finnish", languageCode: "fi, sv"),
Country(name: "Iceland", flag: "🇮🇸", language: "Icelandic", languageCode: "is"),
Country(name: "Estonia", flag: "🇪🇪", language: "Estonian", languageCode: "et"),
Country(name: "Latvia", flag: "🇱🇻", language: "Latvian", languageCode: "lv"),
Country(name: "Lithuania", flag: "🇱🇹", language: "Lithuanian", languageCode: "lt"),
Country(name: "Belarus", flag: "🇧🇾", language: "Russian", languageCode: "be, ru"),
Country(name: "Kazakhstan", flag: "🇰🇿", language: "Russian", languageCode: "kk, ru"),
]



var filteredCountries: [Country] {
var list = searchText.isEmpty
? countries
: countries.filter { $0.name.lowercased().contains(searchText.lowercased()) }

if let selected = countries.first(where: { $0.languageCode == appManager.appLanguage }) {
if let index = list.firstIndex(where: { $0.id == selected.id }) {
list.remove(at: index)
}
list.insert(selected, at: 0)
}

return list
}

var body: some View {
VStack {
if let selectedCountry = selectedCountry {
HStack {
Text(selectedCountry.flag)
.font(.largeTitle)
Text(selectedCountry.language)
.font(.title)
.bold()
}
.padding()
} else {
Text("Select a language")
.font(.title2)
.foregroundColor(.gray)
.padding()
}

TextField("Search countries", text: $searchText)
.padding(10)
.background(Color(.systemGray6))
.cornerRadius(8)
.padding(.horizontal)

List(filteredCountries) { country in
HStack {
Text(country.flag)
.font(.largeTitle)
VStack(alignment: .leading) {
Text(country.name)
.font(.headline)
Text("\(country.language) (\(country.languageCode))")
.font(.subheadline)
.foregroundColor(.gray)
}
Spacer()
if selectedCountry?.id == country.id {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.blue)
}
}
.contentShape(Rectangle())
.onTapGesture {
selectedCountry = country
appManager.appLanguage = country.languageCode
appManager.languageName = country.language
}
}
.listStyle(PlainListStyle())
.padding()
}
.navigationTitle("Select Country")
}
}


struct CountrySelectorView_Previews: PreviewProvider {
static var previews: some View {
CountrySelectorView()
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import SwiftUI

struct SplashScreen: View {
@EnvironmentObject var appManager: AppManager

var body: some View {
VStack {
Image("logo")
Expand All @@ -16,6 +18,7 @@ struct SplashScreen: View {
.padding()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
.background(appManager.isDark ? .black : .white)
.preferredColorScheme(appManager.isDark ? .dark : .light)
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct TodoScreen: View {

@Environment(\.modelContext) private var context
@Environment(\.dismiss) private var dismiss
@EnvironmentObject var appManager: AppManager


@Query(filter: .true, sort: \TodoItem.id, order: .forward, animation: .smooth) private var todoList: [TodoItem]
@Query(filter: #Predicate<TodoItem> { $0.isCompleted == false }) private var inCompleteList: [TodoItem]
Expand Down Expand Up @@ -143,7 +145,7 @@ struct TodoScreen: View {
VStack(alignment: .leading) {
Text(item.title)
.font(.headline)
.foregroundColor(item.isCompleted ? .gray : .primary)
.foregroundColor(item.isCompleted ? .gray : appManager.isDark ? .white : .black)

Text(item.isCompleted ? "Completed" : "Pending")
.font(.subheadline)
Expand All @@ -156,7 +158,7 @@ struct TodoScreen: View {
.font(.title2)
}
.padding()
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white))
.background(RoundedRectangle(cornerRadius: 12).fill(appManager.isDark ? .gray.opacity(0.1) : .white))
.shadow(color: Color.black.opacity(0.1), radius: 4, x: 0, y: 2)
}

Expand Down
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions Notes/presentation/viewmodel/AppManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// AppManager.swift
// Notes
//
// Created by Muhammad Khubaib Imtiaz on 03/02/2025.
//

import SwiftUI

class AppManager: ObservableObject {

@AppStorage("isDark") var isDark: Bool = false {
didSet {
objectWillChange.send()
}
}

@AppStorage("language") var appLanguage: String = "en" {
didSet {
objectWillChange.send()
}
}

@AppStorage("languageName") var languageName: String = "English" {
didSet {
objectWillChange.send()
}
}
}
Loading

0 comments on commit b5b42de

Please sign in to comment.