Computer >> कंप्यूटर >  >> प्रणाली >> Android

Kotlin and Jetpack Compose के साथ एक Android ऐप बनाएं

Jetpack Compose, नेटिव UI बनाने के लिए Android का आधुनिक टूलकिट है। यह Android पर UI विकास को सरल और तेज़ करता है।

हमने अभी-अभी freeCodeCamp.org YouTube चैनल पर एक कोर्स जारी किया है जो आपको कोटलिन और जेटपैक कंपोज़ के साथ एक सुडोकू एंड्रॉइड ऐप बनाना सिखाएगा।

साथ ही आप ग्राफ डेटा संरचनाओं और एल्गोरिदम के बारे में भी जानेंगे।

रयान एम. के ने इस पाठ्यक्रम को विकसित किया। रयान एक बेहद अनुभवी डेवलपर और शिक्षक हैं।

इस पाठ्यक्रम में शामिल अनुभाग यहां दिए गए हैं:

  • ऐप डिज़ाइन दृष्टिकोण:तृतीय पक्ष पुस्तकालय न्यूनतमवाद और एमवी-जो भी वास्तुकला
  • डोमेन पैकेज:रिपोजिटरी पैटर्न, एनम, डेटा क्लास, सील क्लास, हैश कोड, इंटरफेस
  • सामान्य पैकेज:विस्तार कार्य और चर, खुला-बंद सिद्धांत (ओसीपी), सार वर्ग, सिंगलटन
  • पर्सिस्टेंस (स्टोरेज) पैकेज:क्लीन आर्किटेक्चर बैक एंड w/ जावा फाइल सिस्टम स्टोरेज, जेटपैक प्रोटो डेटास्टोर
  • यूआई पैकेज:जेटपैक कंपोज यूआई बेसिक्स, स्टाइल्स, टाइपोग्राफी, लाइट एंड डार्क थीम्स
  • यूआई घटक पैकेज:संशोधक, पुन:प्रयोज्य टूलबार और लोडिंग स्क्रीन
  • यूआई एक्टिव गेम फ़ीचर पैकेज:प्रेजेंटेशन लॉजिक और व्यूमॉडल w/ Coroutines, Kotlin Function Types
  • यूआई सक्रिय गेम फ़ीचर पैकेज:जेटपैक कंपोज़ यूआई और गतिविधि कंटेनर के साथ सुडोकू गेम
  • कम्प्यूटेशन लॉजिक पैकेज:n-size *square* सुडोकस
  • के लिए ग्राफ़ DS और Algos का अवलोकन, डिज़ाइन और परीक्षण

पूरा कोर्स नीचे देखें या freeCodeCamp.org YouTube चैनल पर (3.5-घंटे देखें)।

प्रतिलेख

(स्वत:उत्पन्न)

इस कोर्स में, आप जेटपैक कंपोज़ यूआई लाइब्रेरी का उपयोग करके एक एंड्रॉइड ऐप बनाना सीखेंगे।

साथ ही, आप ग्राफ एल्गोरिदम और डेटा संरचनाओं के बारे में जानेंगे।

रयान एम. के इस पाठ्यक्रम को पढ़ाते हैं।

वह बहुत अनुभवी डेवलपर और प्रशिक्षक हैं।

सभी कैसे हैं? यह यहां रयान है, और मैं ग्राफ सुडोकू एप्लिकेशन पर मेरी ट्यूटोरियल श्रृंखला में आपका स्वागत करना चाहता हूं।

यह एक ऐसा एप्लिकेशन है जिसे मैंने मुख्य रूप से ग्राफ़ डेटा संरचनाओं और एल्गोरिदम के बारे में अपनी समझ को परिष्कृत करने के लिए लिखा था, और Android जेटपैक कंपोज़ पर नई UI लाइब्रेरी।

श्रृंखला के इस भाग में, मैं श्रृंखला के मुख्य लक्ष्यों और विषयों का अवलोकन करूँगा और कुछ डिज़ाइन निर्णयों पर चर्चा करूँगा जो आपको आश्चर्यचकित कर सकते हैं।

मैं प्रोजेक्ट सोर्स कोड के सार्वजनिक संस्करण को अप टू डेट रखने की कोशिश करूंगा।

और अगर कुछ बहिष्कृत हो जाता है या काम करना बंद कर देता है, तो उस स्रोत कोड को देखना आपकी ज़िम्मेदारी होगी।

इस श्रृंखला का प्रारूप शैली के साथ एक कोड है, जिसका अर्थ है कि सीखने का सबसे अच्छा तरीका मेरे साथ कोड लिखना है जैसा कि मैं इसे समझाता हूं।

उन्नत डेवलपर्स के लिए, पूर्ण स्रोत सीधे सीखने के लिए उपलब्ध है, लेकिन आप अपने ज्ञान में किसी भी कमी को दूर करने के लिए वीडियो देख सकते हैं।

मैं कोटलिन भाषा की कुछ काफी उन्नत सुविधाओं और सॉफ्टवेयर आर्किटेक्चर के कालातीत सिद्धांतों का उपयोग करता हूं, ताकि आप एक या दो चीजें सीख सकें।

शुरुआती लोगों के लिए, यह बहुत महत्वपूर्ण है कि आप मेरे साथ कोड में लेकिन अपनी गति से अनुसरण करें।

हो सकता है कि आपको पहली बार में ऐसा न लगे कि आप प्रगति कर रहे हैं, लेकिन समझें कि आप कोड लिखने के कौशल का अभ्यास कर रहे होंगे, भले ही आप अभी तक समझ नहीं पाए हैं कि आप क्या लिख ​​​​रहे हैं।

मैं आपको गुप्त रूप से कोड लिखना भी सिखाऊंगा जो लिखना, पढ़ना, ठीक करना, सुधारना और परीक्षण करना आसान है।

लेकिन आपको सॉफ़्टवेयर डिज़ाइन के कालातीत सिद्धांतों को याद रखने के लिए कहने के बजाय, आप सीखेंगे और व्यवहार में उनका उपयोग करेंगे क्योंकि हम इस एप्लिकेशन को बनाते हैं।

चार सामान्य विषय हैं जो यह ट्यूटोरियल ग्राफ डेटा संरचनाओं और एल्गोरिदम जेटपैक को स्वच्छ UI आर्किटेक्चर और कोटलिन भाषा सुविधाओं को प्रदर्शित करेगा।

हम हल और अनसुलझे n आकार की सुडोकू पहेलियाँ उत्पन्न करने के लिए निर्देशित रंगीन ग्राफ़ और लेखन एल्गोरिदम में मेरे प्रयोगों के विषय का पता लगाएंगे।

हम जेटपैक कंपोज़ का उपयोग करके पूरे यूजर इंटरफेस का निर्माण करेंगे, जो हमें पूरी तरह से कोटलिन में अपना यूआई बनाने की अनुमति देता है।

एक्सएमएल विचारों और शैलियों के विपरीत।

मैं आपको दिखाऊंगा कि कैसे ठोस और स्वच्छ आर्किटेक्चर में आमतौर पर बताए गए सिद्धांतों का उपयोग करके एक साधारण ऐप को वायर अप किया जाता है।

हालांकि, मैं इन विषयों को अपने तरीके से पढ़ाता हूं, इसलिए शब्दजाल या अधिक इंजीनियरिंग की अपेक्षा न करें।

मैं यह भी दिखाऊंगा कि इस खूबसूरत प्रोग्रामिंग भाषा की बुनियादी और उन्नत सुविधाओं का उपयोग कैसे और कब करना है।

वीडियो का यह हिस्सा मध्यवर्ती और उन्नत डेवलपर्स के लिए अधिक लक्षित है।

ट्यूटोरियल को पूरा करने के लिए इस डिज़ाइन निर्णय को समझना आवश्यक नहीं है।

इस पूरे ट्यूटोरियल में, आप देखेंगे कि कंपोज़ और प्रोटो डेटा स्टोर के अलावा, मैं एंड्रॉइड जेटपैक से लगभग किसी भी लाइब्रेरी का उपयोग नहीं करता हूं, वास्तव में, कोटलिन और जावा मानक पुस्तकालयों और एंड्रॉइड एसडीके, मेरे कोड पर भरोसा करके सामान्य रूप से बहुत कम तृतीय पक्ष पुस्तकालय हैं। पुस्तकालयों में बहिष्करण और परिवर्तनों के प्रति अधिक प्रतिरोधी हो जाता है।

ऐसा इसलिए है क्योंकि एंड्रॉइड एसडीके, और मानक पुस्तकालय तीसरे पक्ष के पुस्तकालयों की तुलना में कम बार बदलते हैं, जैसे कि आप एक एंड्रॉइड जेटपैक देखते हैं।

इसका यह भी अर्थ है कि कुछ चीजें जो पुस्तकालय जैसे जेटपैक व्यूमॉडल, जेटपैक, नेविगेशन या सहायता संभाल सकती हैं, हमारे द्वारा हाथ से लिखी जानी चाहिए।

मुझे वास्तव में यह पसंद है, लेकिन आपके पास एक अलग मूल्य प्रणाली हो सकती है।

और यहां मेरा लक्ष्य आपको इन उपकरणों को सीखने से हतोत्साहित नहीं करना है यदि आप उनमें रुचि रखते हैं।

कहा जा रहा है कि, आपको आश्चर्य हो सकता है कि इस तरह के एक छोटे से ऐप में अपने स्वयं के व्यूमॉडल नेविगेशन या निर्भरता इंजेक्शन कोड को उनके बिना लिखना कितना आसान है।

यह एप्लिकेशन मॉडल व्यू का उपयोग करता है, जो भी आर्किटेक्चर है, जो यह कहने का मेरा तरीका है कि मैं किसी और की शैली का पालन नहीं करता हूं।

कई वर्षों तक इस विषय का अध्ययन करने के बाद, मैंने अच्छे सॉफ़्टवेयर डिज़ाइन के सिद्धांतों की अपनी समझ में परियोजना की आवश्यकताओं को अपने आर्किटेक्चर का मार्गदर्शन करने दिया।

इस मामले में, मुझे लगता है कि रचना एमवीवीएम आधारित दृष्टिकोण के लिए बनाई गई है, लेकिन मैंने एक विशिष्ट कारण के लिए एक प्रस्तुति तर्क वर्ग जोड़ा है।

इस कारण को निष्क्रिय दृष्टि या विनम्र वस्तु कहा जाता है।

दृश्य या दृश्य मॉडल प्रत्येक स्क्रीन के नियंत्रण प्रवाह को प्रबंधित करने के बजाय, मैंने उस तर्क को एक अलग वर्ग में खींच लिया।

इस वर्ग को लिखना और परीक्षण करना बहुत आसान है क्योंकि इसमें कोई तृतीय पक्ष निर्भरता नहीं है।

और यह मेरे विचार मॉडल को एक बदसूरत भगवान वस्तु बनने से रोकता है।

आपको इसे एक बार आजमाना चाहिए।

मैंने इस आर्किटेक्चर को केवल सॉफ्टवेयर आर्किटेक्चर के सबसे महत्वपूर्ण सिद्धांत, सरोकारों को अलग करके लागू किया है।

श्रृंखला के इस भाग के लिए बस इतना ही।

अब हम कोडिंग शुरू करते हैं।

डोमेन पैकेज दो चीजों का प्रतिनिधित्व करता है, सबसे सामान्य जानकारी, जैसे डेटा वर्ग, स्थिरांक, और पर्याप्त जो प्रोग्राम को वस्तुतः प्रतिनिधित्व करना चाहिए।

साथ ही, सबसे सामान्य चीजें जो इस प्रोग्राम को करनी चाहिए, जो प्रोग्राम फ़ंक्शंस और इंटरफेस का उपयोग करके दर्शाता है।

संक्षेप में, यह मेरे द्वारा रेट किए गए किसी भी नए प्रोग्राम की नींव है और मैं अपने डोमेन पैकेज या मॉड्यूल को डिजाइन करने के लिए एक दोहराने योग्य प्रक्रिया का उपयोग करता हूं।

उस प्रक्रिया के स्पष्ट और सरल परिचय के लिए।

मेरे चैनल पर इस वीडियो को देखें, सूचना प्रणाली और एप्लिकेशन कैसे डिज़ाइन करें।

वह वीडियो उस बातचीत की रिकॉर्डिंग है जो मैंने उस विशेष विषय के बारे में मिस्र के कुछ सॉफ्टवेयर इंजीनियरों को दी थी।

वैसे भी, इस पैकेज में अधिकांश कोड सरल है, लेकिन इसमें एक डिज़ाइन पैटर्न शामिल है, जिसे मैं अभी पेश करूंगा।

इस पैकेज में कई इंटरफेस हैं, जिनका उपयोग रिपोजिटरी डिजाइन पैटर्न को नियोजित करने के लिए किया जाता है।

इस पैटर्न को अग्रभाग पैटर्न के रूप में भी जाना जाता है।

और पैटर्न का सामान्य लक्ष्य इसकी तकनीकी परिभाषा से सरल है।

मुखौटा या भंडार पैटर्न की तकनीकी परिभाषा एक उपप्रणाली के विवरण को छिपाने के लिए है, इस मामले में, एक अमूर्त के पीछे डेटा भंडारण तंत्र, इस मामले में, एक इंटरफ़ेस।

आइए एक व्यावहारिक उदाहरण देखें।

उस परिभाषा को समझने के लिए, हमारे प्रेजेंटेशन लॉजिक क्लासेस इन रिपोजिटरी इंटरफेस के संदर्भों को रखेंगे, न कि उन वर्गों के जो इंटरफेस को लागू करते हैं।

यह हमारी प्रस्तुति तर्क कक्षाओं को कई लाभ देता है।

उन्हें एक दूसरे से स्वतंत्र रूप से एक कंपाइलर त्रुटि के बिना बनाया जा सकता है, उन्हें इंटरफ़ेस के नकली कार्यान्वयन के साथ परीक्षण किया जा सकता है बिना हमें प्रस्तुति तर्क वर्ग के भीतर किसी भी कोड को बदलने की आवश्यकता होती है।

साथ ही, यदि हम इंटरफ़ेस के किसी भिन्न कार्यान्वयन का उपयोग करने का निर्णय लेते हैं, जैसे फ़ाइल संग्रहण से कमरे के डेटाबेस में स्विच करना, तो हम प्रस्तुति तर्क वर्ग में किसी भी परिवर्तन की आवश्यकता के बिना भी ऐसा कर सकते हैं।

ये लाभ सॉफ्टवेयर सिस्टम के निर्माण का परिणाम हैं, जो शिथिल रूप से युग्मित हैं।

और रिपोजिटरी या मुखौटा पैटर्न ढीले युग्मन को बढ़ावा देने का एक आसान तरीका है।

वास्तव में, इंटरफेस आमतौर पर ढीले युग्मन को बढ़ावा देते हैं।

अब हर जगह इस पैटर्न का उपयोग करने की आवश्यकता महसूस न करें।

एक अच्छा सामान्य नियम महत्वपूर्ण वास्तु सीमाओं में उनका उपयोग करना है।

इस मामले में, मैं उन्हें इस एप्लिकेशन के फ्रंट एंड और बैक एंड के बीच एक सीमा के रूप में उपयोग कर रहा हूं।

चीजों को शुरू करने के लिए, डोमेन पैकेज पर राइट क्लिक करें, नई कोटलिन फाइल पर जाएं और कठिनाई नामक फाइल बनाएं।

और वह एक एनम क्लास होने जा रहा है।

कोटलिन, और जावा और कई अन्य भाषाओं में एनम कक्षाएं मूल्यों का एक सीमित सेट बनाने के लिए उपयोगी हैं।

जैसा कि हम बाद में देखेंगे, आप प्रतिबंधित प्रकार के सेट बनाने के लिए कोटलिन में सीलबंद कक्षाओं का उपयोग कर सकते हैं।

किसी भी मामले में, केवल प्रतिबंधित मूल्यों के उस सेट को बनाने के बाहर मुख्य लाभ यह है कि एनम आपके कार्यक्रम की सुगमता में काफी सुधार कर सकते हैं।

जैसा कि हम एक पल में देखेंगे, आइए अपनी एनम प्रविष्टियों में जोड़ें।

स्पष्ट रूप से, यह एनम किसी दिए गए सुडोकू पहेली की कठिनाई का प्रतिनिधित्व करेगा।

हालांकि, आगे बढ़ने से पहले हमें एक और चीज जोड़ने की जरूरत है, हम वास्तव में एल्गोरिदम में इन प्रविष्टियों में से प्रत्येक के लिए कुछ मूल्यों का उपयोग करेंगे ताकि मूल रूप से सुडोकू पहेली की कठिनाई को निर्देशित किया जा सके।

तो कोटलिन में एक एनम के लिए एक मूल्य जोड़ने के लिए, हमें इसे एक संपत्ति या कुछ गुण देने की जरूरत है।

जैसा कि आप देख सकते हैं, हमारी प्रविष्टियां अब पढ़ी जाती हैं, तो जाहिर है, हमें उन्हें कुछ युगल देने की जरूरत है।

और यही वह सब कुछ है जो हमें यहां करने की आवश्यकता है।

मुख्य पैकेज पर राइट क्लिक करें, क्लास के लिए न्यू कोटलिन फाइल पर जाएं।

और हम सेटिंग नामक एक डेटा वर्ग बनाने जा रहे हैं।

सेटिंग्स हमारा पहला डेटा मॉडल है, जैसा कि मैं इसे या एक सादे पुराने कोटलिन ऑब्जेक्ट को कॉल करना पसंद करता हूं।

और जैसा कि हम एक पल में देखेंगे, इसमें कठिनाई और सीमा दोनों शामिल होंगे, जो कि सुडोकू पहेली के आकार का है।

तो चार बटा चार सुडोकू पहेली में चार की सीमा होगी, नौ बटा नौ की नौ की सीमा होगी।

डेटा कीवर्ड जब क्लास कीवर्ड के आगे रखा जाता है तो मूल रूप से कुछ सहायक तरीके जोड़ता है या उत्पन्न करता है, जैसे हैश कोड या कॉपी के बराबर।

हम निश्चित रूप से बाद में इस कक्षा में नहीं, बल्कि उनमें से कुछ और अन्य में कॉपी का उपयोग करेंगे।

कुछ बिंदु पर, हम उत्पन्न हैश कोड फ़ंक्शन का भी उपयोग करेंगे।

वैसे भी, यह वास्तव में एक साधारण वर्ग है, हम इसमें केवल दो गुण जोड़ने जा रहे हैं।

और बस इतना ही।

डोमेन पैकेज पर राइट क्लिक करें, न्यू कोटलिन क्लास या फाइल पर जाएं।

और इस बार, हम उपयोगकर्ता सांख्यिकी नामक एक वर्ग बनाने जा रहे हैं।

और वह डेटा क्लास होने जा रहा है।

अब, इस वर्ग का उद्देश्य सुडोकू पहेली की किसी विशेष कठिनाई या आकार को हल करने के लिए उपयोगकर्ता के सबसे छोटे स्लैश के सर्वोत्तम समय का प्रतिनिधित्व करना है।

तो हम मूल रूप से केवल गुणों का एक पूरा समूह जोड़ने जा रहे हैं जो काफी हद तक समान हैं।

अब, एक काम जो आप IntelliJ IDEA या Android Studio में कर सकते हैं, वह यह है कि आप बस वहां क्लिक कर सकते हैं और फिर Ctrl D दबा सकते हैं, चाहे जितनी बार आपको आवश्यकता हो और वह एक नई लाइन में कॉपी हो जाएगा।

मूल रूप से इस वर्ग के लिए यही है।

अब, आप सोच रहे होंगे कि हम यहां लंबे समय का उपयोग क्यों कर रहे हैं, हम वास्तव में उस समय को संग्रहीत कर रहे हैं जो उपयोगकर्ता को मिलीसेकंड में किसी विशेष गेम को पूरा करने में लगता है।

इसलिए हम दीर्घ पूर्णांक मान चाहते हैं।

दोबारा, डोमेन पैकेज पर राइट क्लिक करें, न्यू कोटलिन फाइलर क्लास पर जाएं, और यह सुडोकू नोट नामक डेटा क्लास होने जा रहा है।

अब, यह वह जगह है जहां हमारे डेटा मॉडल के साथ चीजें थोड़ी अधिक जटिल और दिलचस्प होने लगती हैं।

तो मैं यहाँ क्या कर रहा हूँ मैं एक ग्राफ डेटा संरचना में एक व्यक्तिगत नोड का प्रतिनिधित्व कर रहा हूँ, हम ग्राफ डेटा संरचनाओं के बारे में बाद में बहुत कुछ बात करने जा रहे हैं जब हम इसे समर्पित ट्यूटोरियल के भाग पर पहुँचते हैं।

लेकिन सिर्फ आपको एक विचार देने के लिए, इसलिए हम वास्तव में एक निर्देशित रंगीन ग्राफ़ बनाने जा रहे हैं।

और यहां ध्यान देने योग्य एक महत्वपूर्ण बात यह है कि इस विशेष मामले में, यह शब्द रंग वास्तव में केवल एक संख्या को संदर्भित करता है, यह केवल एक मान है जिसे हम किसी प्रकार के नोड में जोड़ते हैं।

और फिर, आपको आगे एक पूर्वावलोकन देने के लिए, जब मैं ग्राफ़ डेटा संरचना कहता हूं, तो इसके लिए एक बेहतर नाम नेटवर्क डेटा संरचना होगा, क्योंकि यह अनिवार्य रूप से यही है।

यह नोड्स और किनारों का एक संग्रह है, जो अनिवार्य रूप से नोड्स के बीच नोड्स संबंधों के बीच की रेखाओं की तरह हैं।

वैसे भी, हमारी डेटा संरचना में इस विशेष नोड संरचना में एक रंग या एक मान होने वाला है, जो एक से नौ या एक से चार तक का पूर्णांक है, साथ ही, 00 सहित एक खाली सुडोकू टाइल की तरह प्रतिनिधित्व करता है, लेकिन यह अधिक चिंता का विषय है सामने का छोर।

इन नोटों में एक X और Y निर्देशांक भी शामिल होंगे, इसलिए ऊपर बाईं ओर x शून्य y शून्य होगा, नीचे दाईं ओर x आठ, y आठ होगा, और हम शून्य आधारित अनुक्रमण का उपयोग करने जा रहे हैं।

इसलिए x एक से x नौ तक शुरू करने के बजाय, हम मूल रूप से उसे एक से घटा देते हैं।

तो उस प्रस्तावना के लिए बस इतना ही, आइए कोड लिखना शुरू करें।

तो हम x और y मानों से शुरू करेंगे।

इसके बाद, हम रंग जोड़ेंगे जो एक चर होगा क्योंकि यह पूरे निष्पादन के दौरान बदल सकता है।

फिर हम एक बूलियन जोड़ देंगे जिसे रीड ओनली कहा जाता है और हम इसे लिखने के बाद समझाते हैं कि वह क्या है।

अब, यहाँ केवल पढ़ने के लिए बूलियन का उद्देश्य काफी सरल है।

जब हम अनिवार्य रूप से एक सुडोकू पहेली उत्पन्न करते हैं और फिर उसे अनसुलझा करते हैं, जो यह कहने का एक और तरीका है कि हम एक नई सुडोकू पहेली बनाते हैं तो खेल को वास्तव में खेलने योग्य और मजेदार बनाने के लिए कुछ निश्चित सुराग हटा दें।

सुडोकू बोर्ड पर या सुडोकू ग्राफ़ डेटा संरचना में उन संख्याओं में से कुछ को केवल वही पढ़ा जाएगा जो दिए गए सुराग की तरह हैं जिन्हें उपयोगकर्ता स्वयं को बदलने में सक्षम नहीं होना चाहिए।

जैसा कि हम बाद में देखेंगे।

यह यूजर इंटरफेस को भी प्रभावित करेगा क्योंकि हम रीड ओनली सुडोकू नोड्स या टाइल्स को अलग-अलग तरीके से ड्रा करेंगे, जिन्हें यूजर एडिट कर सकता है।

अब, हम अभी तक पूरा नहीं कर चुके हैं, हमें हैश कोड फ़ंक्शन को ओवरराइड करने की आवश्यकता है जो डेटा क्लास कीवर्ड द्वारा प्रदान किया गया था जैसा कि मैंने पहले चर्चा की थी, और फिर हमें हैश कोड प्राप्त करने के लिए एक फ़ंक्शन जोड़ने की भी आवश्यकता है।

ठीक है, इसलिए हैश कोड का एक डिफ़ॉल्ट कार्यान्वयन होगा, जो विभिन्न गुणों में यहां दिए गए मानों पर आधारित है, हम वास्तव में कुछ अलग करने जा रहे हैं।

तो हम इसी तरह गेट हैश टाइप करने जा रहे हैं।

और यह वास्तव में एक फ़ंक्शन है जिसे हम भी बनाने जा रहे हैं, हम यहां x और y के लिए दो पैरामीटर जोड़ेंगे।

ठीक है, अब हम गेट हैश फ़ंक्शन जोड़ने जा रहे हैं, और यह शीर्ष स्तर पर होगा, जिसका अर्थ है कि यह हमारे सुडोकू नोड वर्ग के कोष्ठक के बाहर बैठता है।

ठीक है, चलो बस इस फ़ंक्शन को लागू करते हैं, और फिर मैं समझाता हूँ कि हम यहाँ क्या कर रहे हैं।

ठीक है, तो मैं यह बताकर शुरू करता हूं कि वास्तव में हैश कोड या हैश मान क्या है।

तो यह अनिवार्य रूप से किसी प्रकार की उत्पन्न कुंजी या किसी प्रकार के एल्गोरिदम के आधार पर विशिष्ट पहचानकर्ता है।

इस मामले में, मेरे पास एक बहुत ही सरल एल्गोरिथम है, मैं केवल x मान को 100 से गुणा करता हूं।

और मैं y मान को अकेला छोड़ देता हूँ।

और मैं मूल रूप से उन दो मानों को एक साथ एक पूर्णांक में मिलाता हूं।

अब, मैं x को 100 से गुणा करने का कारण यह है कि अगर मैंने नौ से नौ सुडोकू पहेली में ऐसा नहीं किया, तो कुछ किनारे के मामले होंगे जहां एक्स और वाई मान तकनीकी रूप से भिन्न होते हैं, परिणामी हैश कोड कई अलग-अलग नोड्स के लिए अद्वितीय नहीं होगा।

सीधे शब्दों में कहें, मैं सुडोकू पहेली में प्रत्येक व्यक्तिगत टाइल के लिए हैश कोड को अद्वितीय बनाने के लिए 100 से गुणा कर रहा हूं।

जहां तक ​​कि हम हैश कोड का उपयोग क्यों कर रहे हैं, सामान्य तौर पर, मैं कोशिश करूंगा और इसे बिल्कुल सीधा रखूंगा।

मूल रूप से, हम ग्राफ़ में प्रत्येक नोड को लिंक किए गए हैश मैप में संग्रहीत करने जा रहे हैं।

तो हैश मान उस हैश मैप की कुंजी का प्रतिनिधित्व करेंगे।

हैश मैप में प्रमुख मान जोड़े होते हैं, अगर आपको इसकी जानकारी नहीं है, तो हम इसे एक पल में देख पाएंगे।

लेकिन यह वास्तव में उपयोगी साबित होता है क्योंकि हमारा यूजर इंटरफेस भी उस तरह की एक्स और वाई समन्वय शैली में चीजों का प्रतिनिधित्व करेगा।

तो बस इसके लिए मेरी बात मान लीजिए।

हैश कोड उन स्थितियों में बहुत उपयोगी होते हैं जहां आपके पास बड़ी संख्या में तत्व होते हैं, और आप जरूरी नहीं कि प्रत्येक व्यक्तिगत तत्व के लिए एक संदर्भ बनाए रखना चाहते हैं।

इसके बजाय, हम हैश कोड प्राप्त करके केवल एक संदर्भ प्राप्त कर सकते हैं।

ओह, और जाने से पहले, हमें यहाँ एक और चीज़ जोड़ने की ज़रूरत है, हम इस चीज़ को क्रमानुसार लागू करने जा रहे हैं।

अब अनिवार्य रूप से, यह क्या करता है कि यह हमें हमारे सुडोकू नोड्स को पढ़ने और लिखने की अनुमति देता है और पूरी पहेली को एक फ़ाइल में भी।

चूंकि हम एक समय में केवल एक पहेली को संग्रहित करने जा रहे हैं, मैं वास्तव में डेटाबेस की तरह कुछ उपयोग नहीं करना चाहता था, यह फ़ाइल सिस्टम का उपयोग करने के लिए और अधिक समझ में आता है, और धारावाहिक और इसे करना आसान बनाता है अनिवार्य रूप से।

ठीक है, हमारे पास इस विशेष पैकेज में फिर से बनाने के लिए एक अंतिम डेटा मॉडल है, हमारी कक्षा में नई कोटलिन फ़ाइल पर राइट क्लिक करें, इसे सुडोकू पहेली कहा जाएगा।

और फिर से, यह एक डेटा वर्ग होने जा रहा है।

डेटा मॉडल के बारे में सोचने का एक अच्छा तरीका है, जैसा कि मैं उन्हें कॉल करना पसंद करता हूं कि वे एक वास्तविक दुनिया की वस्तु के आभासी प्रतिनिधित्व हैं, इस मामले में, एक सुडोकू पहेली।

जिस तरह से मैंने शुरू में इस वर्ग को डिजाइन किया था, वह इस बारे में महत्वपूर्ण प्रश्न पूछ रहा है कि सुडोकू पहेली क्या है।

बाउंड्री जैसी चीजें, तो प्रति पंक्ति या स्तंभ में चार टाइलें हैं या उदाहरण के लिए नौ टाइलें हैं, हमें कठिनाई है।

और सबसे महत्वपूर्ण बात यह है कि हमारे पास ग्राफ़ डेटा संरचना ही है।

एक विशेष पहेली को हल करने के लिए उपयोगकर्ता द्वारा लिया गया बीता हुआ समय भी है।

तो चलिए आगे बढ़ते हैं और उन गुणों को जोड़ते हैं और फिर मैं उनमें से कुछ को समझाता हूँ जिन्हें बाद में समझाया जाना चाहिए।

इसके अलावा, इससे पहले कि मैं भूल जाऊं, आइए यहां क्रमिक कार्यान्वयन में जोड़ें।

ठीक है, तो आप शायद सोच रहे हैं कि नया सुडोकू क्या है।

तो हम क्या कर रहे हैं कि हम वास्तव में शीर्ष स्तर के कार्यों की एक बड़ी श्रृंखला को कॉल करने जा रहे हैं, जो हमारे सभी अलग-अलग एल्गोरिदम बनाएंगे, जो एक दिए गए आकार के आधार पर एक नई सुडोकू पहेली बनाने और उत्पन्न करने के लिए आवश्यक हैं। , जो कि सीमा का प्रतिनिधित्व करता है और एक दी गई कठिनाई है।

साथ ही, यदि आप इस बारे में सोच रहे हैं कि लिंक किए गए हैश मैप के साथ क्या हो रहा है, जो लिंक की गई सूचियों से भरा है, तो यह एक आसन्न सूची का प्रतिनिधित्व करने का एक तरीका है।

फिर से, मैं ग्राफ़ डेटा संरचनाओं पर उन विवरणों में जा रहा हूँ और विभिन्न तरीकों से आप उनका प्रतिनिधित्व कर सकते हैं, या कम से कम मैं इस विशेष एप्लिकेशन में उनका प्रतिनिधित्व कैसे करता हूं जब हम उस विशेष पैकेज और विषय पर पहुंचते हैं।

लेकिन कुछ समय के लिए यह समझने के लिए कि यह हमारी आभासी सुडोकू पहेली का प्रतिनिधित्व करता है, आखिरी चीज जो हम करने जा रहे हैं वह सिर्फ एक छोटी सी विधि में जोड़ना है, जो इसे थोड़ा और स्पष्ट और सुपाठ्य बनाता है कि ग्राफ को कैसे पकड़ें, कम से कम मेरी राय में।

और हम सिंगल एक्सप्रेशन सिंटैक्स का उपयोग करने जा रहे हैं।

तो मैं सिर्फ बराबर ग्राफ़ टाइप करने जा रहा हूँ।

आइए एक नया इंटरफ़ेस बनाएं जिसे I गेम रिपोजिटरी कहा जाता है।

मैं इंटरफेस के सामने पूंजी I लगाने के इस नामकरण परंपरा का उपयोग करना पसंद करता हूं।

और फिर जैसा कि हम बाद में देखेंगे, पर्सिस्टेंस पैकेज में, मैं I MPL का एक प्रत्यय जोड़ूंगा, जो स्वाभाविक रूप से उन वर्गों के लिए कार्यान्वयन के लिए है जो इस इंटरफ़ेस को लागू करते हैं।

छोटे से मध्यम स्तर के एप्लिकेशन को डिजाइन करते समय, एक चीज जो आप कर सकते हैं वह यह है कि आप वास्तव में अपने रिपॉजिटरी इंटरफेस में कार्यों के बारे में सोच सकते हैं।

उपयोग के मामलों के रूप में, जो चीजें हम यहां लिखने जा रहे हैं, उन्हें गेम अपडेट गेम अपडेट नोड सेव करने के लिए बुलाया जा रहा है, जो बहुत समान है यदि आप उपयोग के मामलों में उपयोगकर्ता कहानियों की तरह कर रहे हैं तो एप्लिकेशन डिज़ाइन टाइप करें।

इसलिए मैं इसे वहीं फेंकना चाहता था।

एक अधिक जटिल एप्लिकेशन में, जहां मुझे कई अलग-अलग डेटा स्रोतों और रिपॉजिटरी के अधिक समन्वय की आवश्यकता होती है, मैं शायद एक इंटरएक्टर, या एक उपयोग के मामले की तरह होता, जैसा कि आमतौर पर मार्टिन फाउलर या रॉबर्ट मार्टिन, अंकल बॉब के बीच बात की जाती है।

लेकिन इस विशेष मामले में, सरल अनुप्रयोगों में, आम तौर पर, एक वर्ग के रूप में उपयोग के मामले में, आमतौर पर अमूर्तता की एक अनावश्यक अतिरिक्त परत होती है।

तो यहाँ के साथ, हम बस अपने प्रस्तुतकर्ता के साथ जा रहे हैं, या मॉडल या जो कुछ भी देख रहे हैं, सीधे एक रिपॉजिटरी से बात कर रहे हैं।

और यह इस आकार के अनुप्रयोग के लिए पर्याप्त मात्रा में अमूर्तता है।

अब, एक इंटरफ़ेस कैसे काम करता है कि यह एक वर्ग के समान ही है, जैसा कि हम देखेंगे, एक पल में, हमें वास्तव में उन कार्यों को लागू करने या शरीर देने की अनुमति नहीं है जो हम लिख रहे हैं, हम बस लिखते हैं जिसे आमतौर पर या तो एब्स्ट्रैक्ट फंक्शन या फंक्शन स्टब्स कहा जाता है।

अब यहाँ दो महत्वपूर्ण बिंदु हैं।

सबसे पहले, सस्पेंड कीवर्ड को वहां लागू किया जाता है, क्योंकि इन कार्यों को वास्तव में सह रूटीन स्कोप से बुलाया जाएगा, जो कि लॉजिक क्लास या प्रस्तुतकर्ता में मौजूद है जो इस विशेष इंटरफ़ेस को संदर्भित करेगा।

इसलिए अभी के लिए कंसीडर स्थापित करने के लिए हमें बस इतना ही करना है।

अब, यदि आप परिचित नहीं हैं, तो मैं यहाँ जो उपयोग कर रहा हूँ उसे फ़ंक्शन प्रकार के रूप में जाना जाता है।

और इसलिए हम क्या करेंगे कि हम वास्तव में दो कार्यों के संदर्भ में गुजरेंगे जो तर्क वर्ग में मौजूद हैं प्रस्तुति तर्क उन कार्यों को पास करेगा।

और फिर भंडार कार्यान्वयन में चीजें जो इस विशेष इंटरफ़ेस को लागू करती हैं, इस तरह वे किसी प्रकार के परिणाम के साथ वापस कॉल करेंगे, या तो एक सफल परिणाम या विफलता।

अब आप सोच रहे होंगे कि हमारे पास इकाई क्यों है और फिर यह इकाई लौटाती है जो कि उस तीर का अर्थ है, इस विशेष मामले में? यह कुछ ऐसा है जो हमें कोटलिन कंपाइलर को ठीक से समझने के लिए करना है कि हम उसे क्या करने के लिए कह रहे हैं।

तो यह शून्य में गुजरने के बराबर होगा या मूल रूप से इस विशेष फ़ंक्शन में कुछ भी नहीं होगा, और फिर उस विशेष फ़ंक्शन से कुछ भी नहीं लौटाएगा।

लेकिन यह क्या करेगा यदि आपके पास आरएक्स जावा के साथ कोई अनुभव है, तो यह एक अपूर्ण की तरह कुछ संकेत देगा, जहां मूल रूप से हम इस फ़ंक्शन को कॉल करने पर एप्लिकेशन को सफलतापूर्वक फिर से शुरू करना चाहते हैं।

लेकिन इस विशेष फ़ंक्शन को वास्तव में कुछ भी वापस करने की आवश्यकता नहीं है।

बाद में, हम कुछ उदाहरण देखेंगे जब हमें वास्तव में हमारे सफलता फ़ंक्शन प्रकार के माध्यम से एक मान वापस करने की आवश्यकता होती है।

फिर से, आप सोच रहे होंगे कि गेम को सेव करने और गेम को अपडेट करने में क्या अंतर है? ठीक है, अनिवार्य रूप से, अपडेट गेम फ़ंक्शन में, हम एक संपूर्ण सुडोकू पहेली को थोक में लिखने जा रहे हैं, जिसमें एक बीता हुआ समय शामिल है, हालांकि कुछ स्थितियां हैं जहां हमें वास्तव में केवल गेम के बीता हुआ समय को अपडेट करने की आवश्यकता है। , जैसे कि जब उपयोगकर्ता एप्लिकेशन से दूर जाता है।

तो मैं यहां जो कर रहा हूं वह यह है कि हम उपयोगकर्ता के दृष्टिकोण से जो हासिल करना चाहते हैं उसके आधार पर मैं विशेष प्रकार के विशेष कार्य बना रहा हूं।

अब मैं प्रदर्शित करने जा रहा हूं कि हम वास्तव में इन विशेष प्रकार के कार्यों से एक मूल्य वापस करना चाहते हैं।

ठीक है, इसलिए सफलता के लिए, हम यहां प्रभावी रूप से यह कह रहे हैं कि इस विशेष फ़ंक्शन को किसी प्रकार के बूलियन मान के साथ कॉल किया जाना चाहिए, जाहिर है, या तो सही या गलत जब इसे इस विशेष निलंबन फ़ंक्शन के कार्यान्वयन में कहा जाता है।

अब, मूल रूप से, हम यहां जो कर रहे हैं वह यह है कि जब उपयोगकर्ता सुडोकू पहेली में एकल नोड या टाइल को अपडेट करता है, तो संभावना है कि यह पहेली में अंतिम टाइल हो सकती है।

और उस विशेष मामले में, यह मानते हुए कि पहेली सही है, और उपयोगकर्ता ने अंतिम टाइल डाली है, जो संकेत देगी कि लाभ पूरा हो गया है।

तो यह यहाँ थोड़ा भ्रमित करने वाला हो सकता है।

लेकिन सफलता का मतलब यह नहीं है कि खेल ही पूरा हो गया है।

इसलिए मैं वहां अंतर कर रहा हूं।

ठीक है, अब, इस विशेष मामले में, हम जो कर रहे हैं वह यह है कि जब उपयोगकर्ता किसी सक्रिय गेम में वापस आता है, तो हम स्पष्ट रूप से एक वर्तमान गेम प्राप्त करना चाहते हैं।

और वास्तव में एक किनारे का मामला है जहां उपयोगकर्ता गेम को पूरा करता है, एप्लिकेशन से दूर नेविगेट करता है, और फिर एप्लिकेशन को पुनरारंभ करता है।

और इसलिए हम अभी भी उसमें से गुजर रहे हैं वह पूरा झंडा है।

यहाँ हम स्पष्ट रूप से एक सेटिंग ऑब्जेक्ट पर लौटने जा रहे हैं।

चलिए एक और इंटरफ़ेस बनाते हैं।

और इसे आई गेन, डेटा स्टोरेज कहा जाएगा।

और यह स्पष्ट रूप से एक इंटरफ़ेस है।

अब, इससे पहले कि हम स्वयं इंटरफ़ेस लिखें, हम कुछ अलग करने जा रहे हैं।

हम एक परिणाम रैपर बनाने जा रहे हैं या ठीक है, मान लीजिए कि यह कार्यात्मक प्रोग्रामर के किसी भी सोम विज्ञापन से प्रेरित है, लेकिन किसी को भी वास्तव में परवाह नहीं है कि कार्यात्मक प्रोग्रामर क्या सोचते हैं या वैसे भी बात करते हैं।

सील कक्षाएं वास्तव में कोटलिन प्रोग्रामिंग भाषा की मेरी पसंदीदा सरल विशेषताओं में से एक हैं।

यह हमें प्रतिबंधित प्रकार के सेट बनाने की अनुमति देता है, और उन प्रकारों में विशेष मान हो सकते हैं।

और मूल रूप से यह क्या करेगा और जैसा कि हम एक पल में देखेंगे कि यह हमें I गेम डेटा स्टोरेज के किसी विशेष फ़ंक्शन से किसी ऑब्जेक्ट को वापस करने की अनुमति देता है।

और यह विशेष वस्तु कई अलग-अलग राज्यों का प्रतिनिधित्व करने में सक्षम है।

तो जैसा कि मैंने आपको AI गेम रिपॉजिटरी में पहले दिखाया था, यहाँ, हम दो अलग-अलग फ़ंक्शन संदर्भों के रूप में एक त्रुटि स्थिति और एक सफलता स्थिति का प्रतिनिधित्व कर रहे हैं।

इस उदाहरण में, हम एक ही वस्तु के माध्यम से उन दोनों राज्यों का प्रतिनिधित्व करने जा रहे हैं।

अब, एक अंतिम बिंदु, आगे बढ़ने से पहले, यदि आपके पास, उदाहरण के लिए, किसी प्रकार की स्थिति है, जहां आप केवल एक सफल मामले का प्रतिनिधित्व करना चाहते हैं, तो सफलता में केवल वापसी इकाई के बराबर, आप क्या कर सकते हैं आप वास्तव में बस कर सकते हैं किसी ऑब्जेक्ट का उपयोग करें ताकि आप कह सकें कि ऑब्जेक्ट पूरा हो गया है, आदि।

लेकिन हम वास्तव में इस एप्लिकेशन में ऐसा नहीं करते हैं।

इसलिए आपको इसे इसमें जोड़ने की आवश्यकता नहीं है।

अब, इंटरफ़ेस को समाप्त करते हैं।

I सेटिंग स्टोरेज नामक एक अन्य इंटरफ़ेस बनाएं।

अब, हम यहां परिणाम रैपर का भी उपयोग करने जा रहे हैं।

तो मैं वास्तव में इसे कॉपी और पेस्ट करने जा रहा हूँ।

और हम कुछ चीजों का नाम बदलने जा रहे हैं।

ठीक है, और अब हम इंटरफ़ेस लिख सकते हैं।

इस पैकेज के लिए जाने के लिए एक और इंटरफ़ेस।

इसे पीआई सांख्यिकी कहा जा रहा है अगर मैं इसे भंडार की वर्तनी कर सकता हूं, और जाहिर है, यह एक इंटरफ़ेस होने जा रहा है।

अब एक अंतिम बिंदु, फिर से, रिकॉर्ड वास्तव में उपयोगकर्ता इंटरफ़ेस में किसी चीज़ को प्रभावित करने वाला है या नहीं, इस पर आधारित एक आँकड़ा जो अद्यतन हो जाता है वह एक रिकॉर्ड है यानी किसी विशेष सीमा या सुडोकू पहेली के आकार के लिए पूरा होने का कम से कम संभव समय है, और एक विशेष कठिनाई।

सामान्य पैकेज में कोड होता है जिसे विभिन्न वर्गों और कार्यों में पुन:उपयोग किया जाता है।

ट्यूटोरियल के इस भाग में, हम कोटलिन भाषा की कई अलग-अलग विशेषताओं के बारे में जानेंगे, जिन्हें एक बुद्धिमान और कुशल तरीके से कोड साझा करने के लिए डिज़ाइन किया गया है।

कवर किए गए विषयों में विस्तार कार्य और समस्या शामिल हैं ये अमूर्त वर्ग, खुले बंद सिद्धांत, ऑब्जेक्ट, सिंगलटन, और सह नियमित प्रेषक।

कोड लिखने से पहले, आइए खुले बंद सिद्धांत के बारे में बात करते हैं।

OCP एक अपेक्षाकृत भ्रमित करने वाली अवधारणा है, लेकिन मैं आपको अपनी मौखिक परिभाषा देने के लिए इसे सबसे स्पष्ट तरीके से समझाने की कोशिश करूंगा, जिसे हम बाद में तोड़ेंगे।

किसी भी सामान्य रूप से पुन:उपयोग की जाने वाली सॉफ़्टवेयर इकाई, जिसे बदलने की उम्मीद है, उसका एक निश्चित सार्वजनिक इंटरफ़ेस और इसके कार्यान्वयन को बदलने का एक तरीका होना चाहिए।

उस विशेष परिभाषा को समझने के लिए, कुछ चीजें हैं जिन्हें मुझे अनपैक करने की आवश्यकता है।

सबसे पहले, जब मैं सॉफ़्टवेयर इकाई कहता हूं, तो मैं आम तौर पर किसी वर्ग या फ़ंक्शन के बारे में बोल रहा होता हूं, लेकिन यह कुछ अन्य चीजें भी हो सकती हैं।

तो आइए देखें कि सार्वजनिक इंटरफ़ेस से मेरा क्या मतलब है और मैं यह तर्क क्यों देता हूं कि यदि इसका सामान्य रूप से पुन:उपयोग किया जाता है तो इसे ठीक किया जाना चाहिए।

सार्वजनिक इंटरफ़ेस से, मैं विशेष रूप से जावा या कोटलिन इंटरफ़ेस की बात नहीं कर रहा हूं, इसके बजाय, मेरा मतलब किसी वर्ग या फ़ंक्शन के सार्वजनिक रूप से दृश्यमान पहलू से है।

चूंकि यह एक Android ट्यूटोरियल है, आइए हम गतिविधि वर्ग का उदाहरण लेते हैं।

एक गतिविधि सामान्य रूप से पुन:उपयोग किए जाने और समय के साथ बदलने की उम्मीद की मेरी आवश्यकता के अनुरूप है।

तो ओसीपी के बारे में सोचने के लिए यह एक आदर्श मामला है।

गतिविधि के प्रत्येक उपवर्ग में ऑनक्रिएट फ़ंक्शन शामिल होना चाहिए, जो सार्वजनिक इंटरफ़ेस का एक हिस्सा है, जिसे वह उन कक्षाओं को उपलब्ध कराता है जो इसका संदर्भ देते हैं।

हम चाहते हैं कि यह सार्वजनिक इंटरफ़ेस क्यों न बदले, इसका कारण बहुत आसान है।

मान लीजिए कि एंड्रॉइड प्लेटफॉर्म डेवलपर्स ने अचानक सभी जीवनचक्र कार्यों से सहेजे गए इंस्टेंस, स्टेट बंडल को हटाने और हटाने का फैसला किया।

क्योंकि इस सार्वजनिक इंटरफ़ेस का उपयोग लगभग हर Android प्रोग्राम द्वारा इस प्लेटफ़ॉर्म अपडेट के आसपास किया जाता है, जो सभी के कोड को तोड़ देगा।

मेरा विशेष रूप से मतलब यह है कि सभी कोड बेस में सभी गतिविधि उपवर्ग, जिन्होंने इस पैरामीटर को नहीं हटाया है, वे संकलित करने में सक्षम नहीं होंगे।

इसलिए मैं विशेष रूप से गतिविधि जैसी सामान्य रूप से पुन:उपयोग की जाने वाली सॉफ़्टवेयर संस्थाओं के बारे में बात कर रहा हूं, और यह क्यों महत्वपूर्ण है कि उनके सार्वजनिक इंटरफेस जितना संभव हो उतना कम हो।

चूंकि हमने स्थापित किया है कि क्यों निश्चित सार्वजनिक इंटरफेस वास्तव में महत्वपूर्ण हैं।

अगला प्रश्न सरल है।

फिर हम सार्वजनिक इंटरफ़ेस को बदलने के लिए कार्यान्वयन के लिए एक तंत्र या तरीका कैसे प्रदान करते हैं? खैर, यह पता चला है कि इस समस्या को हल करने के लिए कोटलिन आपको कई विकल्प देता है।

उन सभी को मौखिक रूप से समझाने के बजाय, मैं आपको सिखाऊंगा कि उन्हें कोड में कैसे उपयोग करना है।

जैसे ही हम इस एप्लिकेशन को बनाते हैं, कॉमन पैकेज पर राइट क्लिक करें, और न्यू कोटलिन फाइल या क्लास पर जाएं।

और यह वास्तव में एक अमूर्त वर्ग होने जा रहा है, जिसका वे यहाँ कोई विकल्प नहीं देते हैं।

तो हम जो करने जा रहे हैं वह आधार तर्क में टाइप करना है, हम सार कीवर्ड में जोड़ देंगे।

दुर्भाग्य से, मेरे पास एक अमूर्त वर्ग और सादे पुराने विरासत में इंटरफ़ेस के बीच अंतर को समझाने का समय नहीं है।

इस विशेष पाठ्यक्रम में, यह कुछ ऐसा है जिस पर मैंने ध्यान दिया और अपने अन्य वीडियो पाठ्यक्रमों में बहुत स्पष्ट रूप से समझाया।

लेकिन मैं यह बताऊंगा कि हम यहां एक इंटरफ़ेस के बजाय एक अमूर्त वर्ग का उपयोग क्यों कर रहे हैं।

जिस कारण से हम एक अमूर्त वर्ग का उपयोग करना चाहते हैं, वह उन स्थितियों के लिए है जब हम व्यवहार साझा करना चाहते हैं।

इसलिए उदाहरण के लिए, हम एक फंक्शन स्टब या एब्सट्रैक्ट फंक्शन लिखेंगे, जिसे मैं किसी भी क्लास में शेयर करना चाहता हूँ, जो बेस लॉजिक से इनहेरिट होता है।

और मैं एक वेरिएबल भी साझा करना चाहता हूं, लेकिन इस विशेष चर को जनता के विपरीत संरक्षित करना होगा।

और अगर हम एक इंटरफ़ेस का उपयोग करके ऐसा करने की कोशिश करते हैं, तो आवश्यक रूप से वह विशेष मूल्य सार्वजनिक होगा, हम एक सामान्य प्रकार का भी उपयोग करने जा रहे हैं।

तो मैं आपको दिखाऊंगा कि यह कैसे करना है।

तो एक सामान्य प्रकार के लिए सिंटैक्स केवल कोण कोष्ठक का उपयोग करना है।

And then you could take quite literally whatever you wanted between those angle brackets.

But my suggestion to you is to not use something which is already used, hence why I'm using this all capitals event.

Now, if it doesn't make sense what we're doing here, it will make more sense when we write the classes which inherit from base logic.

Let's go ahead and finish this off.

To briefly explain the intent of this abstract class.

Basically, I'm saying that I want a set of classes, the ones which will inherit from base logic, all of which will have this function on event.

In other words, these classes will handle events from the user interface.

And then as we'll see, we're going to use this job object which comes from the coroutines API as a way to Cancel child co routines.

And also to make each of these logic classes as its own co routine scope.

I'll explain that when we get to that particular part of the tutorial, right click on the common package and create a new kotlin file, which is just going to be a plain old file, and it's going to be called extensions.

gotlands extension functions and extension properties are among my favorite features of the language as a whole.

Without getting too technical here, extensions allow you to employ the open closed principle, which states that software entities should be open for extension, but closed for modification.

If that doesn't make sense, don't worry about it is kind of a confusing definition.

But it allows us to add new functionality to existing source code without having to modify the original source code.

Now, this particular file extensions.kt is kind of like a replacement for static utilities that we might have used in Java or something like that.

It's really just a place where you stick utility code which is used across the application.

Let's write our first extension function to see how this works.

The purpose of this particular extension function, obviously it will be used within activities is really just syntactic sugar, its way to make it so that I don't have to type out toast dot make text and supply this message toast dot length long and dot show.

Instead, in the activity where we'll be using activities, I should say where we'll be using this particular extension function, we can just type make toast, give it whatever string we want to display, and it's handled like that.

By making it an extension function of the activity class, I can use it seamlessly in any activity.

Let's write another much uglier utility extension function.

The purpose of this ugly little function here is to take the elapsed time of the given puzzle which the user is currently working on, and to attempt to convert it into a value based on minutes and seconds or a string to display based on minutes and seconds.

Now, if it takes the user longer than an hour, then we end up just displaying like a generic more than 5959.

Now if you think this code is ugly, in kotlin, I challenge you to write it in Java.

Now for beginners, this might not make sense intuitively, but it's important to understand what this is referring to.

This is actually referring to the long object, which we will be calling dot two time on.

That might make a little bit more sense when we actually get to using this particular extension function.

There's only one more extension, we need to add, and it's actually going to be an extension property this time.

So what I'm doing here is I'm hitting alt, enter on this particular red thing, and then I'm going to hit Add remaining branches.

going to hit alt enter, again, to import our, these are obviously string resources.

That's one thing, we're not going to be writing by hand.

So hopefully, what you've done is you've gone and grabbed the source code for the starting point, which includes things like string resources, right click on the common package again, and we're going to create a new kotlin interface, which is going to be called dispatcher provider.

This interface is very small, what we'll do is we'll write the code and then I'll briefly explain what it does.

Now, unfortunately, I can't briefly explain what a co routine context is.

But I can't explain the purpose of this particular class and how we're going to be using these co routine contexts.

So in most situations, most of the work that we're going to be doing within co routines land is going to take place on the main thread or the UI thread.

Now, with that being said, there are a few operations like writing to a file, which we don't actually want to occur on the main thread.

And that would be a situation where we're going to provide the IO context.

Now, the actual purpose of this particular interface is really key here.

What we're going to be doing is that if we wanted to hypothetically test any class, which needs to use these co routine contexts, in a JVM environment, so not an actual running application, then what we could do is we could return a particular kind of CO routine context, which allows us to test in that particular environment.

I know that's a lot of technical detail, but I can't really make it a whole lot simpler than that.

However, by using this interface here, when we want to use our co routines in the production environment, we can provide the real UI main thread context for the front end, and then we can provide a real dispatcher for the IO thread.

To make that even simpler, we're really just making the code easier to test.

Right click on the common package, go to New kotlin file or class, this time, it's going to be an object.

And hopefully I can spell this right production dispatcher provider.

Again, what we'll do is we'll write the code here and then I'll explain how it works afterwards.

I'm going to hit alt Enter again.

And this is where we will return the actual dispatchers that we'll be using in production as per the name of this particular object.

Now there's a number of reasons why I'm using the object keyword here.

So basically objects in kotlin are in this particular case Singleton's.

So that basically means that we will only ever have one of these production dispatcher, a provider software thingies floating around in memory space at one particular time.

They're also thread safe, which is important because although co routine is not necessarily a thread, our dispatchers dot main and dispatchers.io has something to do with threading.

And the other thing that an object can do is it can actually inherit from an interface.

Now we're not actually going to be writing any unit tests in this particular application, which require the dispatchers but just to show you What you would do if you wanted to unit test some class which needs to use these co routine context, what you can do is you can just instead return dispatchers dot unconfined, and then you would return that for both the IO context and the UI context.

And then that is what you would use in like a JVM j unit test environment.

The persistence package contains classes and functions, which have the role of persisting or storing data beyond the lifecycle of an Android process.

If you don't know what a process is, it simply means a program which is running on a device.

Practically speaking, we will store the progress which the user has made in the current Sudoku game, as well as the settings for that game, and the user's personal records or statistics, as I call them.

Here's a quick look at the architecture of the persistence package.

The game repository in this situation functions as a back end decision maker for the two data sources, which coordinates the data sources themselves.

Just try to carry out CRUD operations, create, read, update, delete, and either report with a success or a failure if an exception is thrown.

The general principle here is to keep things together, which makes sense to be kept together to separate what doesn't need to be kept together, and to also use an abstraction or an interface.

In any place where the implementation might change, I might decide to stop using the local file storage or proto data store.

So hiding these details from the repository is not over engineering, but rather a calculated decision.

Speaking of data sources or storage mechanisms, we will use two different mechanisms for storing our data.

Firstly, we will store the user's preferred Game Settings and their personal statistics in protro data store data store provides a lightweight and efficient way to store this kind of data using protocol buffers.

Protocol Buffers is a serialization language similar to JSON.

However, I find it easier to read than JSON.

And fortunately, the library we will use also comes with its own protobuf compiler that will generate some of the boilerplate code which we would otherwise need to write ourselves.

We also use the device's file storage to store the progress of the user in the currently active game.

Every Android app is given some memory space to store files, which is what we will use.

This is done by making all of the domain models implement serializable.

And using Java as input and output streams to read and write objects from kotlin language.

So in case you aren't following along with the tutorial, and you haven't downloaded the starting point repository, what you're going to want to do is you're going to want to add a directory called pro tau in the main source set, the starting point repository should already have that directory.

So just go ahead and right click on it, and go to new file.

And this file is going to be called gain underscore settings, dot proto, and make sure it's all lowercase.

Go ahead and type this in the top of the file.

So protocol buffers are essentially like a serialization language.

It's very similar to JSON.

If you want to look more into it, you can about what the benefits and the pros and cons of using something like JSON.

But personally, this being the only project that I've used Protocol Buffers in so far, I'm quite happy with it.

Okay, so let's just add two more lines.

And I'll explain some more from there.

Okay, so we'll talk a little bit more about this in a moment.

But basically, what's going to happen here is, we're going to define this protocol buffer message, as it's called, which is kind of like a data type for lack of a better term.

And what we can do is, so this file will be consumed by something called the protocol buffer compiler.

And in this case, what we're basically telling it is that we're going to be generating Java files.

Now in the generated class files.

The protocol buffer compiler is going to basically add whatever we put in the Java package as the package for the generated Java class file.

It's just useful to not mix up your namespaces and stuff like that.

And as for the second option, here, Java, multiple files.

If you don't have that turned on, then what can happen is that basically, the generated Java files will all be in one single file.

We don't really want that, although I'm not sure if it's absolutely integral to getting this application to work.

Like I say, we're going to go through this pretty practically and I'm not an expert in protocol buffers.

Okay, now, we're going to do Line a message which is kind of like one of the main data types for lack of a better term in this particular language.

Okay, so let's talk about what we just did here.

So we've defined a message, which in Protocol Buffers is kind of like a data type or a collection of fields.

And we've done two things.

So within the game settings message, we have a 32 bit integers, like a kind of a smaller integer to represent the boundary of a Sudoku puzzle.

So when I say boundary, I mean like a four by four Sudoku puzzle will have a boundary of four, a nine by nine Sudoku puzzle will have a boundary of nine, obviously.

And the other thing we did here is we defined an enum in protocol buffers.

Now when you're creating these enums, you'll need like a default value unknown.

And then you've got the other values that the enum can potentially be.

Also notice how in boundary and difficulty the fields above the enum I'm giving it default values, naturally, those will be like the values that the protocol buffer gets pre loaded with, like the first time you access it.

Now, the important thing to understand here is that assuming you've added the support for Protocol Buffers into your build Gradle configuration, the proto buffer compiler is going to actually generate some Java files or classes out of this particular message.

Okay, so what I'm doing here is I've opened up the completed project, and I'm just having a look at the file which was generated by the protocol buffer compiler.

And all I really want you to notice here is that when you're using proto data store, what's going to happen is it's actually going to generate a Java class for you.

Obviously, you can see we have our gain settings in camel case, which is what we defined as our message.

And then we also have that enum defined below.

So what does this actually do for us, basically, this is going to allow us to serialize or basically translate from Java into the protocol buffer language and vice versa.

And it also means that we don't actually have to create our own plain old Java object in order to do that.

The library is going to generate that for us.

But we can still use it in our code, which we'll do in a moment, we're going to add one more proto file.

So go ahead and open up the protobuf directory, right click again, go to file.

And this one's going to be called user statistics dot Proto.

Alright, so I've just copied and pasted the first three lines from the other protocol because we'll be reusing them.

And we are going to create another message here.

Now when I say statistics, this is kind of like my way of talking about the user's personal records.

So what are the shortest times to completion that a user has made in solving a particular size and difficulty in a particular Sudoku puzzle? It's pretty straightforward.

So let's just write it out.

And there you have it.

Now, you might be wondering why I'm using 64 bit integers here.

So these actual values are going to be stored in milliseconds, which is why I do want the 64 bit integer storage there instead of the 32 bit integer.

I'm not actually 100% sure if that's necessary, but I did that just to be safe, and realistically, it's not really going to eat up that much extra memory.

Okay, so that's it for our protocol buffer files.

Now, we're going to have to create some protocol buffer data stores, which is how we're actually going to create and access our protocol buffers.

Go ahead and right click on the persistence package, go to New kotlin file or class and this is just going to be a file called data stores.

Okay, so before proceeding, you're going to want to go to build and make project.

Now the build will probably fail, but all we really wanted to do is to generate the appropriate Java class out of the protocol buffer.

But if for some reason that doesn't work for you, just follow along, and eventually it will work.

Okay, so for each protocol buffer based data source, we're going to need to provide a way to get ahold of it or create it from context, then the other thing we'll need is a serializer.

Go ahead and import everything.

And there's two things we need to add into the delegate here.

Okay, so don't worry that it's showing up red will actually write this serializer.

Next, so I just wanted to explain what's going on here.

So we're creating a data store object, and it takes the protocol buffer generated Java class, which is called Game Settings.

And essentially, what this does is it creates a reference which which we can use to either store or retrieve our protocol buffer.

Now, you might be wondering what game underscore setting.pb is, and why it has a different file extension than our proto files, to the best of my understanding game underscore settings.

PB is something that's generated after the fact by the compiler, whereas the profile is something we write for the compiler to consume.

But in case I'm wrong on that, then feel free to flame me on Twitter.

The other thing we'll need is a serializer, which takes care of serialization quite obviously.

After that, you can just click here, hit alt insert, override methods, and we only need the methods from the serializer interface.

So again, let's read the code and then I'll explain what I need to explain after the fact.

Okay, so I'm going to keep the details here pretty light.

So obviously, when we create our data store, it's given the game setting serializer here.

And what the serializer does is it helps us to read and write from input streams.

So in other words, we're going to be obviously reading from a protocol buffer file, and then that's going to be serialized, or rather D serialized into Java, and vice versa.

So basically, what the Android team has done for us here is they've made it a lot easier to handle things like error handling and dealing with input streams.

Because if you've ever worked with input streams in Java, then you can tell there's, you know, you're probably familiar with a lot of boilerplate code to do with that.

So basically, we do a little bit of boilerplate work here.

And it translates to a very simple API, when we actually want to read and write with this particular tool in the back end, which we'll be doing in a moment.

Okay, now, obviously, we need to write another data store and also serializer for the other data type.

So this is going to be one of those rare scenarios where I do actually just copy and paste because there's absolutely nothing new, we're just going to change a couple of the words.

So this would be one of the points where I encourage you to have the complete source code open on the side and then that way, you can do a little bit of copy paste action, like I'm going to do now.

And that is our data stores file complete.

Now obviously, if you had a whole bunch of these, you'd probably want to use separate files, but since I only have Have the two I just decided to stick them in the same file, right click on the persistence package and go to New kotlin class.

This one's going to be called local game storage ample.

So firstly, we're going to make a constant which will represent the name of the text file that we will be reading and writing the game data to.

Next, we'll create the constructor.

So you might be wondering where a file storage directory comes from.

When we create the build logic of this application, which is kind of like my inversion of control dependency injection type stuff, what's going to happen is we're going to call this one function to the Android system, which will return us the specific directory from the system where we can read and write things like files.

Let's go ahead and implement the interface.

Now, I'm going to try to get through this relatively quickly.

But one thing I want to explain is that you'll notice I'm making fairly extensive usage of helper functions.

The reason for that is just to avoid writing redundant code.

Also, as with the other implementations, we're going to be using the width context co routine builder to do this kind of IO work off of the main thread.

So what we'll do is we'll call a helper function called update game data, and we'll pass it in the game data.

And if that operation happens to be successful, then we'll actually just return the same game object that was passed in because it should be consistent.

Okay, now we can create the helper.

So here, we're going to throw the exception so that it'll actually get picked up by the catch block in the functions that we'll be calling this helper.

Now, we're going to be using input and output streams, which are part of the Java standard library in order to rate our data to and from the file.

If you're wondering kind of what this word stream means, ultimately, what we're actually doing kind of at the low level, is we're going to take our game or Sudoku puzzle object, and we're going to serialize it into basically a stream or a very long sequence of textual characters.

And that's what we'll actually be reading and writing from the file.

Okay, so two points, you always want to close your streams.

Also, you might be wondering, how is it that we can say dot write object and pass in our Sudoku puzzle, but let's just check the parameters here.

So I'm going to hit Ctrl p within the parameter brackets, and as you can see, it accepts any type.

Now the important thing is that if our different classes like Sudoku puzzle and Sudoku node did not extend serializable than we wouldn't be able to do this without errors.

So for update node, it's a little bit different, we're just updating one individual node.

So how this is going to work is we're going to get the old data and then we're just going to update that individual node.

And then we will rewrite the result back to storage.

So get game will be another helper, we write, and what I'm going to do is I'm actually going to write that one right away.

Otherwise, the autocomplete and error handling stuff will be all over the place.

Okay, that's what we need to do there.

Now, just a quick reminder here, when we say color, and really, whenever anyone talks about a color in a graph data structure, they're really just talking about a number.

So in this case, the number represents the actual value placed in a particular Sudoku square.

So it'll be like something from one through nine, or one through four, depending on the boundary of the Sudoku will also update the elapsed time.

After it's updated, we will write that result to storage hopefully.

And just to keep the front end synchronized with everything else, then we will return that same game object.

Now it has just come to my attention that I have forgotten to add a particular integer called color to this particular function when I wrote it, so let's just go ahead and fix that now.

There we go.

And I managed to save the easiest for last.

And that's it for this file.

Right click on the persistence package, go to New kotlin class, this one's going to be called game repository info.

So in case you jumped ahead, and you aren't actually familiar with the repository pattern, I actually already explained that in part two of this series where I built the domain package.

In any case, let me just reiterate, reiterate what the purpose of this particular classes, it's basically like a bridge and decision maker for the backend.

Now sometimes you'll have multiple different repositories or datasets.

In the back end, and it might be a good idea to keep them separate.

The reason why I didn't in this particular case is because the game storage and the settings storage are actually inextricably linked.

They are by nature closely related.

So based on that, and the fact that this isn't actually a very large application, I chose to put them together within this repository.

And then how it will work is that the repository will coordinate these two different data sources.

Let's start with the constructor and the repository interface.

Okay, so as you can see, we have our work cut out for us.

So what I'm going to do is I'm going to try to write the code relatively quickly.

And after it's written, I'll explain what it does.

So there shouldn't be anything new in this particular function, except for the fact that we're making an assignment statement within a control statement, Val current game result equals etc.

We're allowed to do that because kotlin is a beautiful and idiomatic language.

This one's actually pretty simple.

You know, for the life of me, I don't understand why it keeps putting on air on top.

I'll explain this function in a moment.

So puzzle is complete is actually a function which exists in the computation logic package, which we'll be writing later on, of course, and all it does is exactly what it says.

But it will return either a true or a false based on whether the puzzle is complete or not.

Hence is complete.

Okay, so what I've done here is I've copied and pasted in the plain language use case which describes this particular function.

Now, as you can see, it's pretty complicated to give a basic explanation of what's going on.

And why did this when we request the current game, ie when the application starts up, there's a number of different things that could happen.

So for starters, the user could have a currently active game and they just want to retrieve it.

It could be the first run of the application, so no game currently exists in storage.

And then there are different situations where errors could occur along the way.

This is something that happens when you're coordinating multiple different data sources.

Now I have my own system of tracking these different event streams, I use basically letters and numbers to denote steps and different potential event streams.

But whatever you do, my suggestion to you is to write this down in plain language first and then go ahead writing the code.

That's what I did this comment above you see here, I wrote that before I wrote the code.

Anyways, let's get started.

Okay, so for our first Event Stream, we attempt to retrieve the current game, and that returned successfully.

And then we also want to know whether the current game is complete or not.

We can just get rid of oncomplete.

And here we go again.

So this is obviously the case where the user has first loaded the application and we want to create a brand new game.

And Looks like I'll have to do this manually this time.

The autocomplete is not helping me here.

But in fairness, we haven't written that function yet.

Okay, I'm just gonna double check that I wrote that correctly.

Now, before I want to move on, I want to explain one thing about my perspective on software architecture.

While sometimes in a simpler application, we can do something like have the presenter coordinate different repositories or back end data sources.

In this particular case, there was enough complicated back end logic that I wanted to have also a decision maker class, which happened to be this game repository imple on the back end, and part of the purpose of this class is to take care of the logic of coordinating these different back end data sources, so that I can keep the presentation logic class doing what it's supposed to do, managing presentation logic, and then I have this class dealing with this messy kind of almost business logic type stuff here.

Anyways, we're not done yet.

Okay, so it just occurred to me that I have missed a function in the interface of a game repository.

So let's just go ahead and add that in.

So what I'm going to do is I'm just going to copy update game, paste it down below.

And what we're going to call this is create new game.

And it's going to take in a settings object and that's it.

So that's actually a helper function that I created mostly for legibility, let's just go ahead and add that in right now.

Just another quick note here, you'll notice that I like incredibly long and descriptive names of everything that's going on.

This is largely because I don't have a great memory for fine details.

So by making these things super long and descriptive, I don't actually have to remember them, I can just read my code and pretty much understand what it does.

Even in these complicated situations where we have all these different event streams and interactions occurring, okay, only two more short functions to go.

And that's it for our back end.

In the top level of the UI package, we have four small files, which we will use to create and apply styles, colors, fonts, and so on.

One of those files is the global theme for our application.

And I will show you how to create both a light and dark theme for the app in only a few extra lines of code.

Stay tuned for the end of this section, as I will do a live demo of the different themes.

Right click on the UI package and create a new kotlin file, which is going to be called color dot Katie.

This file will essentially be a replacement for colors dot XML, if you're used to working with the old resources system, which was based in XML, let's create a color object.

Make sure you import the Compose color class.

Okay, so before we proceed, the most important thing to understand here is how to read these particular values.

So the first two characters here 0x.

This basically tells the compiler, which is the program that will be reading this code that this is in fact a hexadecimal number.

The second two digits here indicates the alpha value as a percentage.

Alpha is another way of saying transparency or how opaque something is.

The remaining three pairs are the red, blue, and green or RGB values, again in a hexadecimal percentage, and that's pretty much all there is to know about these different color values.

I've copied and pasted over the rest of the values because there's absolutely no point in either was typing all this out.

But also keep in mind that they have some predefined values such as flack, for example, which you can also make use of right click on the UI package, and we're going to create another new kotlin file.

And this one's going to be called shape.

So in the old view system, when you wanted to do something like creating a background with rounded corners, or a button or widget or something like that, you had to create usually something inside of the drawable folder, which was XML based.

Again, since this is compose, we can just go ahead and do that in kotlin.

Instead, hey, I will just use some default parameters.

Now, this might be your first time seeing the.dp extension, let's just take a quick look at the source code.

So as you can see, you can basically just append it to an integer double and various kinds of numbers.

The important thing to understand here is that this basically tells the Compose framework that we want to use density independent pixels.

If you want a more profound explanation of what exactly those are, I strongly suggest you look into it because it's a little bit complicated.

Suffice it to say that the idea here is to allow the framework to create values for heights and widths and things like that would work across a variety of different screen sizes and form factors.

Right click on the UI package, and we're going to create another kotlin file, this one is going to be called type.

Now in case you're wondering, when we say type, we're not really talking about a type system, or anything to do with type theory, it has to do with type Pog, Rafi or text and how this text is styled or presented.

So again, this is very much the kind of thing that we used to do and styles dot XML, we're basically going to create a bunch of different text styles, which will use throughout the application.

And then we'll kind of see how to wrap those up in a typography object.

And then we'll see how to add that typography object to our sort of global compose theme.

First, let's create a text style.

Sometimes we have a situation where we want to keep a bunch of default values, but we might want one or two values, which are actually passed in as a parameter to create the text style object.

So I'll show you another way to create these text styles using function.

Just gonna do some quick copy paste here.

And then we can override the color.

So again, what I'm going to do for the rest of these textiles now that we've seen everything there is to see here is I'm going to copy and paste them over.

But there is one more thing that's new that we need to create in this particular file.

Okay, as you can see, we've got a couple different textiles here.

So the last thing we need to do is create a typography object.

So basically, what that's going to mean is that we're going to assign some of the text styles that we've created below, which are used in common things like the body text of a particular feature of the application, buttons, titles, that kind of thing.

If that doesn't make sense.

Let's just write the code.

Make sure you select compose dot material, not kotlin dot txt.

Okay, we're just gonna do To more.

All right, and the only other thing we need to do is set up our graph Sudoku theme.

Right click on the UI package, and we've got, you guessed it another kotlin file.

And it's going to be called graph Sudoku themed.

So one of the handy little features of jetpack compose is that it is incredibly easy to create a theme for light and dark modes.

As someone who uses generally speaking, dark mode almost always actually really appreciate this particular feature of compose.

The first step in that process is to create two different color palettes.

Let's start with the light color palette.

So some of these properties should probably be familiar to most Android developers like having a color primary.

That's how we used to do it also in the old XML system with colors, or at least that was a common naming convention.

Now, one thing I want to point out here is that there's a degree to which some of these more obscure ones like primary variant surface on primary and so forth, I'm really just using those because it's convenient, they don't necessarily have to mean anything in particular.

But the important thing to understand here is that if there's any different color between a light theme and a dark theme, we do want to define it somewhere in here, and then use it appropriate in the composable, which we'll be learning to do later on.

Okay, that was actually supposed to be uppercase there by convention.

And also notice that I've copy pasted over the dark color palette, because again, there's nothing new going on there.

The next step, however, is very important, we're going to create our theme, and it's actually going to be really, really easy.

Here's a little shortcut I learned from a friend, if you want to create a composable function really quickly start typing comp, and then hit enter, just saves you a little bit of time.

Now this theme is going to have two parameters here.

So before we write the body of this function, I just wanted to discuss these two parameters.

So as you can see, we're actually making a function call is system in dark theme, what's going to happen is this system call will return a Boolean, which will tell us whether the user has specified if the app is supposed to be in dark mode or light mode.

And then the content represents everything that will be wrapped inside of this theme.

What's important to understand here is that everything that we put inside of this composable, ie the content will have access to all these different colors, styles and typography information from within the theme itself.

The actual utility of this will make a lot more sense when we actually write the composable.

Just to finish things off, we're going to create a material theme composable.

And we won't need the lambda expression.

So there you have it, it only took a few minutes to create like the color resources and styles and typography information necessary to render both a dark color palette and a light color palette for different modes.

What I'm going to do is show you a quick demo of what this actually looks like in an application.

Here I'm going to be starting the application in the light theme.

Then I'm going to navigate to the operating system settings and set it to a preferred dark mode.

And upon returning we see immediately that the application now is using the dark theme.

We're now ready to start building our user interface.

The UI components package contains reusable elements of the user interface.

Since this is a very small app, the only two such elements are a toolbar, and a loading screen.

One of the great features of compose is that we can make our components reusable in different ways.

Firstly, if a component needs to be positioned according to where it fits in different parent composable, or parent screens, we can pass in a modifier instead of creating a modifier within the child composable.

This is worth experimenting with in case you haven't already.

Secondly, it is possible to pass in composable as arguments, which also allows reuse and extension of functionality.

In this app, we want different toolbar icons for the two different UI screens.

And we can achieve this by passing in the icon compostables.

From those parent UI screens, you'll see later on how we can specify and handle different icons and different click events.

Using the same toolbar will also create this reusable loading screen and later, I will show you how to animate it, right click on the UI package and go to new package.

And this one's going to be called components.

Just a brief explanation here, I've adopted this particular convention from the composed samples repository.

So what will go into this particular folder are composable, which will end up being reusable across a variety of different UI elements and different screens.

In this case, we're going to be creating a reusable toolbar, and also a reusable loading screen, right click on the components folder, and go to New kotlin file, and this one's going to be called app toolbar.

Let's create our functions stub, what I'm going to do is I'm going to type co MP and then the autocomplete will create a composable function.

This one's going to be called app toolbar.

First, let's write the parameter list and I'll explain it a little bit.

Make sure you select the compose.ui modifier.

Let's start by talking a little bit about modifiers.

So modifiers are basically how you can create most of these styles size and position kind of data for a particular composable.

Now there's kind of two different main ways to do this.

We could of course, create this modifier and use it within this widget.

But that would be for a situation when the widget itself is going to be deciding that kind of information.

Since we're using a reusable component here, an app toolbar, which we plan to use in multiple different places.

In this particular situation, we're going to pass the modifier into this function, which is a way of basically saying that the parent composable will actually decide where to position and how to size this particular UI element.

The title is pretty self explanatory, but what is a little more complicated is the icon.

And again, that will be dictated by something in the parent composable.

That's how I actually make this thing reusable and allow it to handle different icons or different actions when it's clicked.

After we finish off this particular composable, I'll show you a quick preview of the actual icon that we'll be using.

So hopefully that will make a little bit more sense.

The first thing we want to do is override the top app bar composable.

Let's just pause a moment and talk about different colors.

So one way to solve this problem would be to hard code some kind of color in here.

But in the previous section of this tutorial, we went through the trouble of setting up both a light and dark theme.

So what we're doing here is we're actually going to be using a color which is based on the theme.

Remember in the graph Sudoku theme composable, there was a call to a function which was his system and dark theme or something like that.

And that's actually going to dictate which color palette we select.

So by using material theme colors dot primary, it will automatically inherit the appropriate color based on whether we're in light and dark mode.

And that would be one reason to avoid hard coding something in here.

In this case, we have a color which will be the same regardless of whether it's light or dark mode.

So we're just going to add in a text composable which is effectively eight Extra view.

But if you wanted to add something like a logo for the application in front or after the title text, and what you could do is you could add in a row here and then just add in both the icon and then the text composable.

And then you'd be ready to go.

Go ahead and import that.

This is probably pretty self explanatory.

But when we want to inherit style data for particular fonts and stuff like that, then this is how we can do it.

Again, this is something super handy.

And you only see this in kotlin, certainly not Java.

So what we're doing here is we're explicitly asking is the application currently in light mode, and then we're picking a text color based on that.

This is really just an alternative way of handing this conditional UI logic without having to assign something to a theme specifically.

Next, we'll deal with alignment.

And that's it for the texts composable in our toolbar.

So action bar is probably something that will be more familiar to the older Android developers.

But basically think of this is like the icons within the toolbar.

Generally, they're used for very important actions in the user interface, like navigating to a new feature, indicating that you're done doing something.

And note importantly, that this particular lambda function is of type row scope.

So basically, what that means is, if you have several action buttons, you can place them within these two brackets here, and they will automatically be lined up like a row.

Now all we need to do is just type icon and then add in the parentheses here.

And this is because we're actually going to be passing this icon in from the parent composable.

As I said, moments ago, I just wanted to give you a sneak preview of the icon itself.

We're not going to be writing it yet, but we will do so later on.

The important thing to understand here is that we're deciding about how to handle on click and what this thing actually looks like in the parent composable, we're not actually doing it within the toolbar.

And by pulling that responsibility out of the toolbar, that's how we get the reusability that we want.

Right click on the components package, go to New kotlin file, and this one's going to be called loading screen.

Let's create our loading screen composable.

The first thing we'll need is a surface.

So you might be wondering, why are we using a surface here in particular, in this case, I really just want like a space surface of the UI, which has a particular color and specific dimensions.

Here I've set Phil max height to a fraction of point eight F, which is basically saying I want it to take up most of the width, or sorry, most of the height of the user interface.

But I might want some space for something like a an ad banner or something of that nature.

Anyways, I basically want an icon or an image which is stacked on top of a progress bar which will be stacked on top of some kind of like text.

So for that kind of situation, obviously we're going to want to use a column.

Obviously, we'll be centering things.

Go ahead and import that.

Now I'm noticing it's not improperly importing our I think there's something within the Compose libraries, which basically mimics our so let me just fix those imports before we proceed.

As you can see here, I've just copy and pasted it in the our import.

And now we're good to go.

That's our logo.

Here's your progress bar.

Okay, so you might be wondering about this painter thing.

So basically, in the alpha version of compose, we had to specify whether it was a vector asset or a bitmap asset and stuff like that.

So we can just use this generic painter resource thing and point it to basically anything in our drawable.

And it will actually figure out whether it's a bitmap or a vector asset.

Also, I wanted to point out the copy function here.

Suppose you have a color and you want to slightly change the alpha value or you have one of these textile objects and you want to make some kind of change to it.

The copy function is super handy for doing that.

In this part of the tutorial, we will create the event sealed class view model and presenter for the act of game feature.

Before we do that, let us look at a few design decisions involved in this architecture.

The purpose of our presentation logic class, which I call logic, for short, is exactly as the name implies, it handles the work of coordinating the container view model and backend repositories.

If notified of a non stop event, it will also cancel all co routines, it does not possess any Android platform code, which makes it loosely coupled and very easy to test.

I might also consider reusing it for a desktop version of this app.

But we'll see.

The purpose of the view model is also to do exactly what the name implies, it is a virtual representation of the user interface, which the view observes.

In simpler terms, it is a model of the view, it exposes function types, which is a very simple and easy standing for the observer pattern.

In situations where we don't require multiple observers.

Each time our logic class updates the view model, the view model will automatically publish the new data to the view.

Another design decision with this view model is that it does not extend jetpack view model.

There are several reasons for this decision, some of them simple, and some of them quite technical.

The simple reason is that using jetpack view model creates tight coupling with the Android platform.

And it has its own set of boilerplate code and dependencies, which I'm not a huge fan of.

In short, it doesn't solve more problems than it creates in this particular application.

And I wanted to practice creating view models which might be usable for kotlin desktop or kotlin.

j s.

The technical reason why is that in this application, we simply don't need to persist the data across activity instances or process death in order to have a good user experience.

Instead, we just make a fairly cheap call to the Android file system and reload the data from there if such events occur.

Now, before you apply that reasoning in every situation, understand that reloading data from a file system works fine in this application, but should not be considered a suitable replacement for unsaved instance state in every application you write.

If you like the models in save state handle, go right ahead and use it.

We also employ the strategy pattern to clean up the interface which our logic class exposes to the container in the view.

Each subclass of the sealed class represents an action that can occur in the view or container.

Rather than having a function for every UI event.

We have one function that accepts a single object that can represent multiple different paths of execution.

That's the strategy pattern.

Right click On the UI package, and go to new package called active game, right click on this new package, go to New kotlin file or class, and we're going to create an interface, and it's going to be called active game container.

This word container is a technical term.

The way I'm using it here is to kind of signify something which contains a large portion of an application or an entire application.

In my opinion, a container doesn't usually deal much with the business kind of logic of the application.

It basically just wires things together and builds things and kind of serves as an entry point.

In the next part of this tutorial, I'll explain what we'll actually be using as a container.

But by using an interface here, I'm basically stating quite explicitly that I might change my mind about what we use as a container.

Anyways, it only contains two abstract functions.

Right click on the active game package again, and we're going to create a sealed class this time.

And it's going to be called active game event.

So as I explained in the above comment, the active game event sealed class represents every kind of user interaction of a given feature, in this case, the active game feature.

This is a very common pattern that I use and we'll see how it works with our base logic abstract class which we created in the common package.

Okay, we are now going to create our view model.

Firstly, let's create a small little class here which will be like a virtual representation of a single tile in a Sudoku puzzle.

So obviously, x&y represent the x&y coordinates of the particular Sudoku tile value will represent what we talked about in graph data structures as the color again, it's literally just a number, I don't know why we need to call it a color.

Now it has focused indicates that the user has clicked on a particular tile, after which they can click on one of the input buttons to change that particular number.

And finally, a read only tile you can think of as a tile, which is like a given clue in the puzzle.

So therefore, the user is not allowed to actually change any read only tiles.

So before we start reading this view model, I just wanted to mention a couple of things here.

As discussed in the introduction for this particular section, I didn't actually want to use any of the jetpack libraries to achieve a publisher subscriber relationship between the view model and the view.

Now, it turns out that that publisher, subscriber relationship or pattern is actually quite easy to implement.

But in this case, I actually found it simpler to just use kotlin function types to achieve what I would call a poor person's publisher, subscriber pattern or observer pattern.

So this really means something simple in practice, although it might look kind of complicated for those who aren't really familiar with working with function types.

Our view model will possess these nullable function type references.

As we'll see in a moment, what we can do is each time we update the view model from the presentation logic class, we can then update the view by extension by invoking these function types from within the view model.

Now, the reason why we're using notables here is from within the view model.

I can never be 100% certain if there is actually anything listening.

But with that being said, I feel like if I played around with this particular class for a couple of hours, I could probably streamline a little bit and maybe make some of these internal variables private or something like that.

So really what I'm saying here is Feel free to take this general idea of having a view model, which isn't tightly coupled to Android jetpack, but also feel free to experiment with it and see if you can optimize it.

So with that out of the way, let's create some function types.

So all of these function types will be prefixed with sub to ensure good legibility.

Active game screen state is actually something we will create in the next part of this tutorial.

So just go ahead and leave it glowing red here.

Okay, let me just briefly explain these different function types.

So the board state is basically a virtual representation of the Sudoku board.

Obviously, the content state basically just means three different states.

So either we're loading the data, the user has a currently active game that they're solving, or the user has completed a particular game, we will use this to animate between different states in the user interface.

Now, timer state has to do with the count up timer, which basically records how long it takes for the user to complete a given Sudoku game.

So just to hopefully clear up any confusion here, timer state will be the actual long value in milliseconds representing the time and then sub timer state is the way that we actually update the user interface after we update the new timer state.

Let's finish off the rest of these variables.

These are quite obviously default values.

Next, we'll write a function to initialize this view model.

Okay, so let's just pause for a moment.

What we're doing here is we're taking the state of the data as it existed in storage, we're giving it to the view model, and then what we're doing is we're building the view models own virtual representation of that state.

Now, the view models internal representations will have things like has focus, which are concerned specifically of the user interface and not necessarily something that I would include in the original domain model.

Also, in case you're wondering, the key value is basically created from hashing the x value and the y value.

This is something that we covered very early on in this tutorial, in case you've jumped ahead.

Again, active gain screen state is something that we will create in the Compose part of the tutorial.

Here, we're binding that data to the view model.

And then we will invoke our function types to update the view assuming it's listening.

And that's it for our init function.

Now, we just have a few more functions, which will be called by our presenter to do various things with the state of the view model.

So here, we're just updating an individual tile.

So what we're doing here is when the user hits a particular tile, that's going to send a message into the presenter, which will have a particular x and y coordinate, and then the presenter will call this particular function.

And so what it will do is it will look for the tile which the user clicked on based on that X and Y value, and set that one to has focus equals true.

And then for every other tile, we want to set it to false.

Otherwise, we could have a situation where the user has selected multiple different tiles, which is not something our application is supposed to be allowed to Do and this would be the situation where our back end has determined that the current puzzle is complete.

Right click on the active game package.

And let's create a new kotlin class, which is going to be called active game logic.

Okay, so before we proceed, this is definitely one of those situations where I strongly suggest having the complete source code open on the side while you follow along here.

Obviously, I'm going to do my best not to make any mistakes, but it's possible that I will make a mistake.

Active game logic represents the presentation logic of this particular feature of the application.

As we'll see, it coordinates between the container the view model, and then by extension, the view, as well as the back end of the application.

Let's start with the constructor.

Okay, so just a bit of review before we move on, for the time being the container will actually be an activity.

But there's a possibility in the future, I might move to using fragments as containers instead, at this point, I don't really want to, but we'll just see if that makes sense in the future.

But this is the entire reason why I have included an interface here so that I can change what's behind the interface very easily.

The view model is pretty clear, we just wrote it.

Game repo is where we store the game data.

So that includes the game settings as well as the current progress of the users game stats.

repple is where we store the records for the shortest times to completion of each different difficulty and size of puzzle.

And if you're wondering what the dispatcher is go back and watch the common package when we created that we created this dispatcher provider and I basically explained what the purpose of it is there.

Base logic is also something that we created in the common package.

And we'll see the function that we inherit from that class in a moment.

Okay, let's start for a moment about co routines.

So one way to think about scopes, whether we're talking about co routine scope, or dagger or whatever, is to really just consider that it's about a life cycle.

Now, you're probably wondering, why are we not making something like a view model or a fragment or an activity, our life cycle class? Well, in case you haven't noticed by now, I don't like any kind of tight coupling to the Android platform, if I can avoid it.

There's a number of other reasons.

But one of the main ones is that because this class contains all of the presentation logic, in a sense, it's the head decision maker for this feature of the application, then, in my opinion, it makes sense to make it responsible for cancellation of any co routines, which happened to be currently running as far as this on event function, which we inherit from base logic.

Well, basically, this is an implementation of the strategy pattern.

I won't give you a long and technical explanation here.

It's actually a very simple pattern.

But basically, it provides sort of like a singular entry point into this particular class.

So instead of having like a single function, for every event, we have one function, which takes in an argument, our active game event, which is capable of representing all the different events, and I just find that really cleans up the interfaces between different classes, interfaces is used in the general sense in that statement.

Okay, first, let's implement our co routine context.

Remember, jobtracker exists in base logic, but we also need to initialize it Okay, now before we proceed, there's something really important we need to implement which is a ko routine timer.

As I mentioned before, the active game Screen does have a count up timer.

Now some of you are probably going to be wondering, why didn't I use the Java timer class or the androids count up timer or whatever it's called.

Basically, I did try using those things.

And they presented different application breaking problems.

And it turned out to be easiest just to create this kind of CO routine timer.

Okay, so this requires a little bit of explanation, obviously.

So we'll notice two different keywords here, which might be unfamiliar to some of you, we have the inline and cross inline keywords.

So whenever you see the inline keyword, the easiest way to understand that is to understand that it just means copy, paste.

And if you want to know what that means in code, then I suggest you decompile some of your kotlin code, which uses the inline modifier, and you'll see how the inline function is actually copy and pasted into the call site.

Now we have something else going on here, which is a cross inline function type.

So before I explain what the cross inline action is, let's talk about what this function actually does.

So here we have a pretty standard spin lock, while true.

So it's a loop that's going to just endlessly execute, it's going to invoke that function type, and then it's going to delay for 1000 milliseconds.

Now, there's a couple different things going on here.

Number one, you have to understand that we will be delaying this ko routine, but it's not actually going to block the thread that it's on, which is of course a big win.

Now the other thing that's going on here is action is going to be a lambda expression that we will pass into this particular function.

Really the only thing crossing line does is it basically makes it so that in the lambda function, which we will pass into this function here, we're not allowed to write a return statement in that function.

So in the most general sense here, what we're doing is we're taking a preventative step, to avoid a situation where we might accidentally return from within the lamda that we pass in here causing unexpected behavior.

Now, you're probably wondering, since we have this endless loop going on, how do we actually stop this particular core routine? Well, what we're going to do is we're going to create a job.

Let's just do that now.

And what we'll do soon is we will actually assign this job variable to our start co routine timer, and that will allow us to cancel it.

Let's just write another quick extension function to do with this timer business and then I'll explain what it does.

In experimenting with the user interface, how to make the timer the least janky or most accurate that it could be it turned out that subtracting one from the value each time we write it to the back end created a more consistent timer.

But one particular edge case is if the value equals zero, then obviously we don't want to subtract one from it.

Otherwise the timer will say negative one at first and that just doesn't look very good.

Okay, so with all that done, we can get to implementing the rest of the presentation logic Okay, so when the user hits an input button, we can have two different situations that could occur.

In one situation, the user has already selected a tile, which would become the focus tile.

Or it might be that they just hit an input button without actually focusing a tile, in which case, we don't really want to do anything.

Okay, so if you're wondering about the details of game repo, you can go back to the part of the tutorial where we actually build it.

Basically, we're going to be creating a lambda to represent the success case, and then another lambda to represent like an error exception case, to make that a little bit more legible.

I'll just add in a comment here.

Okay, so if you're again, wondering how we actually cancel the timer, this is exactly how we do it, we cancel the job object.

Now we'll write this other function in a moment.

Basically, if it's a new record, then we want to render the user interface slightly differently than if it wasn't a record.

But before we do that, let's finish off the error case.

So in order to actually know if it's record, we actually need to pass the value back into the stats repo just to check on that.

So I'm going to be honest, the error handling in this application is not the best, neither is it really the worst show error Well, for the time being just actually show a toast message explaining that some error occur.

Okay, just a quick fix.

This is actually supposed to be elapsed time not timer state.

Next, we have on new game clicked.

You'll notice a recurring theme here, which is at any time we want to perform concurrent operations.

So anytime we're working with the back end, we're going to wrap that into a launch co routine.

There's a lot of different ways to work with CO routines.

This is just one of the most simple straightforward ways to do it.

In my opinion.

Okay, so what we're doing here is first we're asking the view model has the user completed the current game, if they haven't, we actually want to store the progress the user has made in their current game, when they hit on new game clicked, because maybe they hit it by accident, or they want to be able to go back and finish the game or some reason like that.

That's right The update with time function.

Again, we have success and error cases.

Hopefully, that's pretty clear at this point.

Next, we'll implement that function.

Next, we'll write the cancel stuff function.

So basically, the cancel stuff function essentially cancels every ko routine.

Next, we'll write on start.

I forgot to mention earlier, the reason why we had an underscore in one of the functions for is complete.

It's just kind of a convention for a lambda argument or parameter, which doesn't actually end up getting used.

In this case, we're going to use it.

Okay, so obviously, this is where we start the coroutine timer, and we only want to do that when on start is called.

Now again, I feel like I could have handled this a little bit better, it kind of goes against my rules to consider standard flow the application as an exception.

But basically, what we're going to do here is in the event that we ask the storage for a current game, and it doesn't actually retrieve anything, generally speaking, this situation is going to occur when the user has run the application for the first time, and hence, there wouldn't actually be any data stored.

So in that particular case, we would want to do this.

Now, we could also end up here because of some kind of legitimate exception, but in that particular case, I still think navigating to the new game feature is still actually a good way to handle that good but maybe not the best.

Next we have on stop.

Okay, so onstop is actually tied to the lifecycle of the Android activity or fragment that it's bound to.

So when this function is called, that basically means that we want to save the user's current progress and then kind of shut everything down.

Finally, we have on tile focus.

So this would be when a user actually selects a particular Sudoku tile.

Now this function is incredibly complicated, so brace yourself.

Okay, I lied.

That's actually everything we need to do.

In this part of the tutorial, we will create the user interface for the active game feature.

Before proceeding I strongly suggest you watch my video entitled How to Understand jetpack compose a beginner's guide to composable and read composition.

We will be writing many composable and setting up re composition as well.

But that video is made for people who are just getting started with compose.

It explains what a composable is, what re composition is, and how to avoid doing read composition the wrong way.

And believe me, it is fairly easy to screw up read composition if you aren't aware of how it works.

I will link to this video in the pinned comment below.

This video is full of a ton of information, so please take a look at the timestamps in the description box below.

Topics include basic widgets, such as text, text, button, image, icon, spacer, and divider layouts, such as box column row box with constraints and my favorite constraint layout.

Simple transition animations to animate between a loading screen, active game and a complete game.

I also show you how to communicate with our presentation logic and our view model using function types and lambda expressions.

Before we write the composed code, though, I show you how to set up an activity as a container for composable.

The process for doing this is almost identical for fragments if you prefer them, I also show you how to write a very simple dependency injection extension function, which hides the backend details from the front end.

Right click on the active game package and go to new activity and choose an empty activity.

And make sure you uncheck generate a layout file.

And this activity will be called active game activity.

It will be the launcher activity.

Now in case you're wondering why we're using this wizard instead of just creating a class file.

The reason is simply that by using the wizard it will add an entry into the manifest so we don't have to do that.

Just to briefly recap the purpose of this activity here is as a feature specific container.

Let's start by creating a reference to our active game logic class.

Next, let's implement the active game container interface.

Click on the red and hit alt enter.

For show error, we will use the extension function that we created way earlier on in this tutorial.

Go ahead and import.

Also in case you're wondering, this is single expression syntax here it basically just replaces the brackets and return statement with just an equal sign.

Next one implement on new game click.

Now obviously we haven't created new game activity yet so that will show up as red until we do.

We also need to override two more lifecycle methods.

Here will tell the logic class that everything is ready to go And then we'll also override on stop.

And that will obviously signal the logic class that it's time to cancel things and tear stuff down.

Finally, we just need to add a few lines to on create.

First of all, create the view model.

Now, this is a really important part, what we're going to do next is we're basically going to anchor our composable that we'll be creating in the next part of this tutorial, to the activity here.

This is also something you can call inside of a fragment.

Go ahead and import that.

Naturally, we're gonna wrap everything in our compose theme.

Now, this is a very critically important thing to understand and a very important pattern.

So when we create active game screen, we're going to pass in a function type, which will serve as our event handler, which is basically my way of saying that is the way in which we will forward the events, the onClick events and stuff like that, that occur in the composable to our presentation logic class.

So make sure you pay attention to what I'm saying here, because this is a really important part, even if you don't use presenters wood, or whatever.

Function types are an excellent way to handle on click events, especially if you combine them with the strategy pattern, which we discussed in the previous section.

Okay, now, if you've never seen a function reference, I believe it's called basically what we're doing here is we are pointing to the on event function of the logic class.

This really is a function reference.

So hopefully, you can understand what I'm talking about here.

We'll also pass in the view model.

Now lastly, we actually need to build our logic class.

So what we'll do is we will write that code in an extension function, but what we can do first is just read it here.

And that's everything we need to do in our activity.

Right click on active game, go to new package, and this package will be called build logic.

Right click on that package and go to New kotlin file or classes is going to be a file.

And it's going to be called build active game logic.

If you've been watching my channel, or live streams for some time, you'll know that I talk a lot about dependency injection service locators.

And one of the things I say all the time is that in a small application, you really don't need to use di container, like hilt, dagger, whatever, you can use it.

But what I always advise for beginners is to write the code that these things generate for you, yourself first, so that you understand what these frameworks are doing for you.

So that's exactly what we're going to do.

We're going to write the kind of thing that these frameworks generate for you.

And in a small application, it's actually very simple code to write.

And, of course, it's going to return active game logic.

Okay, let's pause for just one moment here.

So in case you're wondering how we get the path to the storage directory that we can use for this application, you can call context dot files dir dot path.

Finally, our dispatcher.

And that's all we need to do.

Right click on the active game feature, and create new kotlin file called active game screen.

First, let's create an enum.

This enum represents different states, which this feature of the user interface can possess.

The actual state is held in the view model, but we will see how we can update our composable UI by binding to the view models function types we created in the previous part of this tutorial.

Active game screen represents the root composable.

In this hierarchy of composable, it has the responsibility of setting up the core elements of the UI, and also animating between them.

event handler function type reference is how we call back to the presentation logic.

When the user interacts with the application, it must be passed down to any composable, which has such interactions, we also pass in the view model, which is how we actually give the data to our UI.

In very simple language, whenever we have some kind of data or state, which may change at runtime, we want to wrap that data in a remember delegate.

This tells the Compose library under the hood, to watch for changes and to redraw the UI if a change occurs.

Now mutable transition state is used specifically for animations here, so don't use this everywhere.

We will see a more general purpose example of a remembered state later on.

Remember, delegate prepares compose for updates, but we also need a way to actually update the value.

We do this by binding a lambda expression to one of the function types which our view model possesses.

When one of those functions is invoked in the view model, the program automatically jumps to and executes this code within our composable.

This is what actually triggers the re composition.

We have a remembered transition state and a way to update that state from the view model.

Now we need to set up the transition animations themselves.

This is where you can get as creative as you like.

In this app.

Each content state has its own composable associated with it.

We animate between them simply by changing the alpha value or transparency.

Now it was truly up as red a moment ago, the way I fixed that was to manually import the Compose runtime.

So the transition spec tells compose details about what the animation should look like.

Essentially, this means we don't have to write our own mathematical instructions, which is great for someone like me who sucks at arithmetic.

One option for compose is to use the scaffold composable as a skeleton for your UI.

I personally prefer to do this myself, since it's not really that difficult, and it doesn't hide anything from me.

First, we have our app toolbar.

Let's go ahead and create that new game icon.

These icons come from the Compose material library, I highly recommend you use it.

This is how we actually trigger an on click event.

As explained in a previous part of the tutorial, by creating our toolbar icon here and passing it into the app toolbar composable, we make the app toolbar reusable.

Below the toolbar we have the main content of this screen, which can have three different states.

Each time a re composition occurs, this one statement will be executed again.

The act of alpha value will change when the transition animation occurs, thus fading out the previous content state and fading in the new one.

Obviously, we will create these in a moment.

And that's it for our route composable the most complex part of our UI comes from an active Sudoku game.

A nine by nine puzzle has 81 different texts composable, which is a large number of widgets.

The way I went about writing this composable was to think of each part of the Sudoku game as a particular layer or element.

Be sure to avoid writing God composable by making usage of helper functions, which break down the UI into the smallest reasonable parts.

box with constraints is kind of like a composable wrapper, which gives us information about the height, width and other measurements, we can use that information within its lambda expression.

We need to know the screen width in order to determine how wide and tall the Sudoku board should be.

Here we asked for the max width of this constraint layout.

Here we ask for the max width of this layout composable.

But we need that value to be in density independent pixels, and it needs to be relative to the density of the current screen as well.

That's where the two dp extension function comes in.

And it uses the local density to determine that value.

The margin of the board also needs to change based on the screen density.

I arrived at these values simply by testing the app on various densities using the emulator.

Next, we will write a constraint layout, which is a totally awesome way to manage dynamic layouts.

Now in order to constrain composable to each other, we need a way for them to reference each other.

This is equivalent to setting IDs for XML views.

First, we create these references and you will see how to bind them later on.

Let's create a layout container for the puzzle board.

Okay, so this is really important see how we're passing in that reference in the constrain as parameter there.

This is how we actually associate a particular composable with a particular reference.

This box composable will be associated with the name board.

Next we'll create the Sudoku board itself.

Again, the boundary is like the size of the puzzle.

So it's either a four by four puzzle or a nine by nine puzzle.

So boundary would either be four or nine.

This is supposed to say size.

So the offset here is used to evenly distribute the screen real estate for each Sudoku, tile and grid line.

Here's a way to make a mutable state which is not associated with some kind of transition animation.

So this is the more general purpose approach.

So the first argument here view model dot board state can be thought of as the initial value, never equal policy ensures that even minor changes in the state like has focus actually triggers a re composition.

Again, this is how we actually update the value once the view model is updated.

As you can see, here, again, I'm making usage of lots of helper functions to break things down.

Here we render the text fields which represent tiles in the puzzle, they can either be read only or mutable, thus, meaning that we need to render them slightly differently.

So here, we're saying if the user sets a particular tile to a value of zero, we actually just want to render it as an empty tile.

The main idea here is that we're using the x and y values of each individual tile along with the offset in order to evenly position each tile So when the user selects a tile, it's going to become focused and we want to render that tile obviously a little bit different than an unfocused tile.

Now we'll render the read only squares.

Next we'll create the board grid.

So Sq RT is an extension, which is actually defined in the computation logic.

In retrospect, I probably should have defined that in the common package, but it's pretty obvious what it does.

So this function here, we'll draw the grid lines that separate the Sudoku puzzles.

To make it more obvious to the user which sub grids are which we draw different borders to separate the four by four or nine by nine sub grids.

This is why we're using modulo here.

So this will draw both the vertical and the horizontal lines.

Okay, so we're jumping back into the game content composable to finish it off.

below our Sudoku board, we have some different icons to indicate the difficulty of the puzzle.

Next we need a layout container for the count up timer that's great, the timer texts composable.

Now the default value, it's just empty.

Okay, we're back in the game content composable.

The last thing we need to do is just add a layout container for the input buttons.

Now we're about to hard code some values in here and that is kind of bad practice.

But the reason is that the Compose team deprecated flow row, which I'm still upset about, and it worked perfectly for this situation, and I've been too lazy to implement flow myself.

Hey, at least I'm being honest.

In case you're wondering, 0.4 and five dot dot nine will emit a range inclusive of those values.

Let's create that composable a spacer is pretty self explanatory, it just takes up some space in the layout.

Next we have the buttons themselves.

This text button wrapper allows us to style a nice looking button instead of just adding on click on a text composable.

Alright, that's it for gain content.

Now we need to do the game complete content screen, which is obviously when a user finishes a game.

So this is basically just two images stacked on top of each other, but we're only going to render one of them if it is actually a new record that the user made.

So since we don't actually create the emoji events icon, we can change the color of it using this color filter thing.

Pretty handy.

Next, we have two texts composable.

And that's it.

Congratulations.




  1. Android और iOS के लिए YouTube के साथ शॉर्ट्स कैसे बनाएं

    TikTok की बढ़ती लोकप्रियता ने YouTube जैसे प्रतिस्पर्धी ऐप्स को समान विकल्प जोड़ने के लिए प्रेरित किया है। नतीजतन, एंड्रॉइड और आईओएस पर, अब आप पोर्ट्रेट ओरिएंटेशन में शॉर्ट्स नामक छोटे वीडियो बना सकते हैं, जो 60 सेकंड तक सीमित हैं। इस ट्यूटोरियल में, हम आपको Android और iOS ऐप्स पर YouTube Shorts बना

  1. इन 3 निःशुल्क Android ऐप्स के साथ आसानी से एनिमेटेड टेक्स्ट बनाएं

    एनिमेटेड टेक्स्ट हमेशा आपके मानक टेक्स्ट की तुलना में देखने में बहुत अधिक मजेदार होता है। टेक्स्ट के एनिमेटेड होने पर आप जितने भी प्रभाव जोड़ सकते हैं, वे संदेश को और अधिक आकर्षक बनाते हैं। अच्छी खबर यह है कि ऐसे ऐप्स हैं जो आपके निर्माण में आपकी सहायता कर सकते हैं। सूची में निम्नलिखित ऐप्स निःशुल्

  1. रिएक्टिव नेटिव और नेटिव बेस के साथ एंड्रॉइड न्यूज ऐप कैसे बनाएं

    हम एक ऐसी दुनिया में रहते हैं जहां चीजें लगातार बदल रही हैं। इसलिए यदि आप जो हो रहा है उस पर अद्यतित रहना चाहते हैं, तो आपको एक अच्छा समाचार ऐप चाहिए। कुछ अच्छी तकनीक सीखने और वर्तमान में बने रहने में आपकी मदद करने के लिए, इस ब्लॉग पोस्ट में हम रिएक्ट नेटिव का उपयोग करके Android के लिए एक समाचार ए