Android
 Computer >> コンピューター >  >> システム >> Android

Kotlin と Jetpack Compose を使用して Android アプリを作成する

Jetpack Compose は、ネイティブ UI を構築するための Android の最新のツールキットです。 Android での UI 開発を簡素化し、加速します。

freeCodeCamp.org YouTube チャンネルで、Kotlin と Jetpack Compose を使用して数独 Android アプリを作成する方法を説明するコースをリリースしました。

その過程で、グラフのデータ構造とアルゴリズムについても学びます。

Ryan M. Kay がこのコースを開発しました。 Ryan は非常に経験豊富な開発者であり、教師でもあります。

このコースで取り上げるセクションは次のとおりです。

  • アプリ設計アプローチ:サードパーティ ライブラリのミニマリズムと MV-Whatever アーキテクチャ
  • ドメイン パッケージ:リポジトリ パターン、列挙型、データ クラス、封印されたクラス、ハッシュ コード、インターフェイス
  • 共通パッケージ:拡張関数と変数、Open-Closed Principle (OCP)、抽象クラス、シングルトン
  • 持続性 (ストレージ) パッケージ:Java ファイル システム ストレージ、Jetpack Proto Datastore を使用したクリーン アーキテクチャ バックエンド
  • UI パッケージ:Jetpack Compose UI の基本、スタイル、タイポグラフィ、ライト テーマとダーク テーマ
  • UI コンポーネント パッケージ:修飾子、再利用可能なツールバー、読み込み画面
  • UI アクティブ ゲーム機能パッケージ:プレゼンテーション ロジックとビューモデル、コルーチン、Kotlin 関数タイプ
  • UI アクティブ ゲーム機能パッケージ:Jetpack Compose UI とアクティビティ コンテナーを使用した数独ゲーム
  • 計算ロジック パッケージ:n サイズの *正方形* 数独のグラフ DS とアルゴリズムの概要、設計、およびテスト

以下または freeCodeCamp.org YouTube チャンネルで全コースをご覧ください (3.5 時間の視聴)。

トランスクリプト

(自動生成)

このコースでは、jetpack Compose UI ライブラリを使用して Android アプリを作成する方法を学習します。

その過程で、グラフ アルゴリズムとデータ構造について学びます。

Ryan M. Kay がこのコースを教えています。

彼は非常に経験豊富な開発者であり、インストラクターでもあります。

みんなどうしたの? Ryan です。グラフ数独アプリケーションのチュートリアル シリーズにようこそ。

これは主に、グラフのデータ構造とアルゴリズム、および Android ジェットパック構成の新しい UI ライブラリの理解を深めるために作成したアプリケーションです。

シリーズのこのパートでは、シリーズの主な目標とトピックの概要を説明し、驚くかもしれないいくつかの設計上の決定について説明します。

プロジェクトのソース コードの公開バージョンを最新の状態に保つように努めます。

また、何かが非推奨になったり機能しなくなったりした場合、そのソース コードを確認するのはあなたの責任です。

このシリーズの形式はスタイルに沿ったコードです。これは、学習するための最良の方法は、私が説明するコードを一緒に書くことであることを意味します.

上級開発者は、直接学習できるように完全なソースを利用できますが、ビデオを見て知識の穴を埋めることもできます。

私は kotlin 言語のかなり高度な機能と、時代を超えたソフトウェア アーキテクチャの原則を使用しています。

初心者の方は、自分のペースでコードをフォローすることが非常に重要です。

最初は進歩しているとは思えないかもしれませんが、書いている内容をまだ理解していなくても、コードを書くスキルを練習していることを理解してください。

また、書きやすく、読みやすく、修正しやすく、改善しやすく、テストしやすいコードの書き方を秘密裏に教えます。

ただし、時代を超越したソフトウェア設計の原則を暗記するよう求めるのではなく、このアプリケーションを構築する際にそれらを学び、実際に使用します。

このチュートリアルでは、グラフ データ構造とアルゴリズム jetpack がクリーンな UI アーキテクチャを構成し、kotlin 言語機能を構成する 4 つの一般的なトピックがあります。

有向カラー グラフのトピックと、n サイズの数独パズルを解くアルゴリズムと未解決の数独パズルを生成するためのアルゴリズムを作成する実験について説明します。

Jetpack Compose を使用してユーザー インターフェイス全体を構築します。これにより、UI をすべて kotlin で作成できます。

XML のビューとスタイルとは対照的です。

堅牢でクリーンなアーキテクチャで一般的に説明されている原則を使用して、単純なアプリを接続する方法を紹介します。

ただし、これらのトピックは独自の方法で教えているため、専門用語や過剰なエンジニアリングを期待しないでください。

また、この美しいプログラミング言語の基本機能と高度な機能をいつ、どのように使用するかについても説明します。

ビデオのこの部分は、中級および上級の開発者を対象としています。

チュートリアルを完了するために、この設計上の決定を理解する必要はありません。

このチュートリアル全体を通して、compose と proto データ ストアを除いて、Android ジェットパックのライブラリはほとんど使用していないことに気付くでしょう。ライブラリの廃止や変更に対する耐性が高まります。

これは、Android SDK と標準ライブラリが、Android ジェットパックなどのサードパーティ ライブラリよりも頻繁に変更される傾向にあるためです。

また、jetpack ビューモデル、ジェットパック、ナビゲーション、ヘルプなどのライブラリが処理する可能性のあるものは、私たちが手書きする必要があることも意味します。

私は実際にそれが好きですが、あなたの価値観は違うかもしれません.

ここでの私の目標は、これらのツールに興味がある場合に、これらのツールを学ぶことを思いとどまらせることではありません。

そうは言っても、このような小さなアプリでは、独自のビューモデル ナビゲーションや依存性注入コードをそれらなしで簡単に作成できることに驚くかもしれません。

このアプリケーションは、アーキテクチャに関係なく Model View を使用します。これは、他の人のスタイルには従わないという私の言い方です。

このトピックを長年研究してきた私は、優れたソフトウェア設計の原則を理解する上で、プロジェクトの要件をアーキテクチャの指針としています。

この場合、compose は MVVM ベースのアプローチ用に作成されていることがわかりましたが、特定の理由でプレゼンテーション ロジック クラスを追加しました。

この理由は、受動的なビューまたは謙虚なオブジェクトと呼ばれます.

ビューまたはビュー モデルで各画面の制御フローを管理する代わりに、そのロジックを別のクラスにプルしました。

このクラスは、サードパーティの依存関係がないため、作成とテストが非常に簡単です。

そして、ビュー モデルが醜い神のオブジェクトになるのを防ぎます。

いつか試してみてください。

このアーキテクチャは、ソフトウェア アーキテクチャの最も重要な原則である関心の分離を適用するだけで設計しました。

シリーズのこのパートは以上です。

それでは、コーディングを始めましょう。

ドメイン パッケージは 2 つのものを表します。データ クラス、定数、およびプログラムが仮想的に表す必要がある十分な情報など、最も一般的な情報です。

また、このプログラムが実行しなければならない最も一般的なこと。プログラムが関数とインターフェイスを使用して表現します。

本質的に、これは私が評価する新しいプログラムの基盤であり、反復可能なプロセスを使用してドメイン パッケージまたはモジュールを設計します。

そのプロセスを明確かつ簡単に紹介します。

情報システムとアプリケーションの設計方法について、私のチャンネルでこのビデオをチェックしてください。

そのビデオは、その特定のトピックについて、私がエジプトの何人かのソフトウェア エンジニアに行った講演の録画です。

とにかく、このパッケージのほとんどのコードは単純ですが、これから紹介するデザイン パターンが含まれています。

このパッケージには、リポジトリ設計パターンを採用するために使用されるいくつかのインターフェースが含まれています。

このパターンはファサード パターンとも呼ばれます。

パターンの一般的な目標は、その技術的な定義よりも単純です。

ファサードまたはリポジトリ パターンの技術的な定義は、サブシステムの詳細 (この場合は抽象化の背後にあるデータ ストレージ メカニズム) を隠すことです。

実際の例を見てみましょう。

その定義を理解するために、私たちのプレゼンテーション ロジック クラスは、インターフェースを実装するクラスではなく、これらのリポジトリ インターフェースへの参照を保持します。

これにより、プレゼンテーション ロジック クラスにいくつかの利点がもたらされます。

これらは、コンパイラ エラーを引き起こすことなく、互いに独立して構築できます。また、プレゼンテーション ロジック クラス内のコードを変更する必要なく、インターフェイスの偽の実装でテストできます。

また、ファイル ストレージからルーム データベースへの切り替えなど、インターフェイスの別の実装を使用することを決定した場合、プレゼンテーション ロジック クラスを変更する必要なく、それを行うこともできます。

これらの利点は、疎結合されたソフトウェア システムを構築した結果です。

また、リポジトリまたはファサード パターンは、疎結合を促進する簡単な方法です。

実際、インターフェースは一般に疎結合を助長する傾向があります。

このパターンをどこでも使用する必要はありません。

重要なアーキテクチャ境界でそれらを使用することは、適切な一般規則です。

この場合、このアプリケーションのフロント エンドとバック エンドの間の境界としてそれらを使用しています。

まず始めに、ドメイン パッケージを右クリックし、[新しい kotlin ファイル] に移動して、難しさと呼ばれるファイルを作成します。

これは enum クラスになります。

kotlin の enum クラス、および Java やその他のさまざまな言語は、制限された値のセットを作成するのに役立ちます。

後で説明するように、kotlin でシール クラスを使用して、制限された型のセットを作成できます。

いずれにせよ、制限された値のセットを作成すること以外の主な利点は、列挙型によってプログラムの読みやすさが大幅に向上することです。

すぐにわかるように、列挙型エントリを追加しましょう。

明らかに、この列挙型は特定の数独パズルの難易度を表します。

ただし、先に進む前にもう 1 つ追加する必要があります。実際には、アルゴリズムでこれらの各エントリにいくつかの値を使用して、基本的に数独パズルの難易度を決定します。

したがって、kotlin で列挙型に値を追加するには、1 つまたは複数のプロパティを指定する必要があります。

ご覧のとおり、エントリが読み取られるようになったので、明らかに、いくつかの double を指定する必要があります。

ここで行う必要があるのはこれだけです。

メイン パッケージを右クリックし、クラスの新しい kotlin ファイルに移動します。

そして、settings というデータ クラスを作成します。

settings は私たちの最初のデータ モデルです。私はそれを、または単純な古い kotlin オブジェクトと呼んでいます。

すぐにわかるように、数独パズルのサイズである、難易度と境界の両方が含まれます。

したがって、4 x 4 の数独パズルには 4 の境界があり、9 x 9 には 9 の境界があります。

class キーワードの前にある data キーワードは、基本的に、equals ハッシュ コードやコピーなどのヘルパー メソッドをいくつか追加または生成します。

後で間違いなくコピーを利用することになるでしょう。おそらくこのクラスではなく、いくつかのクラスやその他のクラスで使用します.

ある時点で、生成されたハッシュ コード関数も使用します。

とにかく、これは非常に単純なクラスです。2 つのプロパティを追加するだけです。

以上です。

ドメイン パッケージを右クリックし、新しい kotlin クラスまたはファイルに移動します。

今回は、user statistics というクラスを作成します。

これがデータ クラスになります。

現在、このクラスの目的は、数独パズルの特定の難易度またはサイズを解決するための、ユーザーの最短スラッシュ ベスト タイムを表すことです。

したがって、基本的には、ほぼ同じプロパティを大量に追加するだけです.

さて、IntelliJ IDEA または Android Studio でできることの 1 つは、そこをクリックして Ctrl D を押すだけで、必要な回数だけ新しい行にコピーすることです。

このクラスは基本的に以上です。

ここで、なぜ long を使用しているのか疑問に思われるかもしれません。実際には、ユーザーが特定のゲームを完了するのにかかる時間をミリ秒単位で保存しているのです。

そのため、長整数値が必要です。

再度、ドメイン パッケージを右クリックし、新しい kotlin ファイラー クラスに移動します。これは数独ノートという名前のデータ クラスになります。

ここから、私たちのデータ モデルがもう少し複雑で興味深いものになり始めます。

つまり、私がここで行っているのは、グラフ データ構造内の個々のノードを表しているということです。グラフ データ構造については、チュートリアルの専用の部分に到達したときに、後で詳しく説明します。

ただし、アイデアを提供するために、有向カラー グラフを実際に作成します。

ここで注意すべき重要なことは、この特定のケースでは、この色という用語は実際には数字を指すだけであり、ある種のノードで関連付けられる値にすぎないということです.

繰り返しになりますが、プレビューを提供するために、私がグラフ データ構造と言うとき、より適切な名前はネットワーク データ構造です。

これはノードとエッジの集合であり、基本的にノード間の関係を結ぶ線のようなものです。

とにかく、データ構造のこの特定のノード構造は、色または値を持ちます。これは、1 から 9 または 1 から 4 の整数であり、00 は空の数独タイルのように表されます。フロントエンド。

これらのメモには X 座標と Y 座標も含まれるため、左上は x zero y zero、右下は x Eight、y Eight となり、Zero Based インデックスを使用します。

したがって、x 1 から x 9 までを開始する代わりに、基本的にはそれを 1 だけ減算します。

前置きは以上です。コードを書き始めましょう。

それでは、x と y の値から始めましょう。

次に、変数となる色を追加します。これは、実行中に変化する可能性があるためです。

次に、読み取り専用と呼ばれるブール値を追加します。それが何であるかは、書き込み後に説明します。

さて、ここでの読み取り専用ブール値の目的は非常に単純です。

数独パズルを本質的に生成して未解決にした場合、つまり、新しい数独パズルを作成し、一定数の手がかりを削除して、ゲームを実際にプレイ可能で楽しいものにします。

数独ボードまたは数独グラフのデータ構造にあるこれらの数字の一部は、ユーザーが自分で変更できないという手がかりのようなものであり、読み取り専用になります。

後で説明します。

読み取り専用の数独ノードまたはタイルを、ユーザーが編集できるものとは異なる方法で描画するため、これはユーザー インターフェイスにも影響します。

まだ完了していません。前に説明したように、データ クラス キーワードによって提供されたハッシュ コード関数をオーバーライドする必要があります。次に、ハッシュ コードを取得する関数も追加する必要があります。

さて、ハッシュ コードにはデフォルトの実装があります。これは、ここでさまざまなプロパティに指定された値に基づいています。実際には、別のことを行うだけです。

get hash と入力します。

これは実際に作成する関数でもあり、x と y の 2 つのパラメーターをここに追加します。

さて、get hash 関数を追加します。これはトップ レベルになります。つまり、数独ノード クラスのブラケットの外に配置されます。

よし、この関数を実装してみましょう。それから、ここで何をしているのかを説明します。

では、ハッシュ コードまたはハッシュ値とは何かを説明することから始めましょう。

つまり、本質的には、ある種のアルゴリズムに基づいて生成されたキーまたは一意の識別子です。

この場合、非常に単純なアルゴリズムがあり、x の値に 100 を掛けるだけです。

y の値はそのままにしておきます。

基本的に、これら 2 つの値を組み合わせて整数にするだけです。

ここで、x に 100 を掛ける理由は、9 x 9 の数独パズルでそれを行わなかった場合、X と Y の値が技術的に異なっていても、結果のハッシュ コードがいくつかの異なるノードで一意ではありません。

簡単に言えば、数独パズルの個々のタイルごとにハッシュ コードを一意にするために、100 を掛けています。

ハッシュ コードを使用している理由については、一般的に、かなり単純にしようと思います。

基本的に、グラフの各ノードをリンクされたハッシュ マップに格納します。

したがって、ハッシュ値はそのハッシュ マップのキーを表します。

ハッシュ マップにはキーと値のペアがあります。ご存じない場合は、すぐに確認してください。

しかし、ユーザー インターフェースも X および Y 座標スタイルで表現されるため、これは非常に便利です。

ですから、私の言葉を信じてください。

ハッシュ コードは、多数の要素があり、個々の要素ごとに参照を維持する必要がない場合に非常に便利です。

代わりに、ハッシュ コードを取得することで参照を取得できます。

ああ、その前に、ここにもう 1 つ追加する必要があります。これをシリアライズ可能に実装します。

基本的に、これが行うことは、数独ノードとパズル全体をファイルに読み書きできるようにすることです。

一度に 1 つのパズルしか保存しないので、データベースのようなものは使いたくありませんでした。ファイル システムを使用する方が理にかなっており、シリアライズ可能であり、それを簡単に行うことができます。

さて、この特定のパッケージでもう一度作成する最後のデータ モデルが 1 つあります。クラスの新しい kotlin ファイルを右クリックします。これは数独パズルと呼ばれます。

繰り返しますが、これはデータ クラスになります。

私が好んでデータ モデルと呼んでいるように、データ モデルは現実世界のオブジェクト (この場合は数独パズル) の仮想表現であると考えるのが良い方法です。

私が最初にこのクラスを設計した方法は、数独パズルを構成するものについて重要な質問をすることです.

境界のようなもので、行または列ごとに 4 つのタイルがあるのか​​、それとも 9 つのタイルがあるのか​​など、問題があります。

そして最も重要なことは、グラフのデータ構造自体があることです。

ユーザーが特定のパズルを解くのにかかった経過時間もあります。

それでは先に進み、これらのプロパティを追加してから、後で説明する必要があるものについて説明します。

また、忘れる前に、シリアライズ可能な実装をここに追加しましょう。

わかりました。おそらく、新しい Sudoku のビルドとは何か疑問に思っているでしょう。

ここで実際に行うことは、一連のトップレベル関数を実際に呼び出すことです。これにより、さまざまなアルゴリズムがすべて形成され、特定のサイズに基づいて新しい数独パズルを構築および生成するために必要になります。 、これは境界が表すものであり、特定の難易度です。

また、リンクされたリストでいっぱいのリンクされたハッシュマップで何が起こっているのか疑問に思っているなら、それは一種の隣接リストを表す方法です.

繰り返しになりますが、グラフのデータ構造とそれらを表現するさまざまな方法、または少なくともこの特定のアプリケーションでそれらをどのように表現するかについては、後でその特定のパッケージとトピックに到達するときに詳しく説明します.

しかし、当分の間、これは私たちの仮想数独パズルを表していることを理解してください。最後に行うことは、小さな小さなメソッドを追加することです。少なくとも私の意見では。

そして、単一式の構文を使用します。

なので、equals グラフと入力します。

I ゲーム リポジトリと呼ばれる新しいインターフェイスを作成しましょう。

インターフェイスの前に大文字の I を付けるというこの命名規則を使用するのが好きです。

そして、後で説明するように、永続化パッケージに I MPL の接尾辞を追加します。これは、このインターフェースを実装するクラスへの実装を自然に表します。

小規模から中規模のアプリケーションを設計するときにできることの 1 つは、実際にリポジトリ インターフェースの機能を概念的に考えることができるということです。

ユース ケース自体として、ここで記述しようとしているものは、セーブ ゲーム アップデート ゲーム アップデート ノードのように呼ばれます。これは、ユース ケース タイプのアプリケーション デザインでユーザー ストーリーのように行っている場合と非常によく似ています。 /P>

だから私はそこにそれを入れたかっただけです.

複数の異なるデータ ソースとリポジトリをさらに調整する必要がある、より複雑なアプリケーションでは、Martin Fowler または Robert Martin, Uncle Bob.

しかし、この特定のケースでは、より単純なアプリケーションでは、一般的に言えば、クラス自体としてのユース ケースは、通常、抽象化の不要な追加レイヤーです。

ここでは、プレゼンターやビュー モデルなどを使用して、リポジトリと直接やり取りします。

これは、このサイズのアプリケーションにとって十分な量の抽象化です。

インターフェースがどのように機能するかというと、クラスと非常によく似ていますが、すぐにわかるように、実装することも、作成する関数に本体を与えることも実際には許可されていません。一般に抽象関数または関数スタブと呼ばれるもの。

ここで重要な点が 2 つあります。

まず、サスペンド キーワードが適用されます。これは、これらの関数が、この特定のインターフェイスを参照するロジック クラスまたはプレゼンターに存在するコ ルーチン スコープから実際に呼び出されるためです。

現時点で並行性を確立するために必要なことはこれだけです。

よく知らない方のために説明すると、ここで使用しているのは関数型と呼ばれるものです。

そこで、プレゼンテーション ロジックがそれらの関数を渡すロジック クラスに存在する 2 つの関数への参照を実際に渡します。

そして、リポジトリの実装では、この特定のインターフェースを実装するもの、つまり、何らかの結果 (成功または失敗) でコールバックする方法です。

ここで疑問に思われるかもしれませんが、この特定のケースでは、なぜ unit があるのに、その矢印が意味する unit を返すのでしょうか?これは、kotlin コンパイラーが私たちが何をするように指示しているかを正確に理解するために、私たちがしなければならないことです。

したがって、これは、この特定の関数に void または基本的に何も渡さず、その特定の関数から何も返さないことと同じです。

ただし、RX Java の経験がある場合は、oncomplete のような信号を送信します。基本的には、この関数が呼び出されたときにアプリケーションを正常に再開したいだけです。

しかし、この特定の関数は実際には何も返す必要はありません。

後ほど、成功関数型を介して実際に値を返す必要がある場合の例をいくつか見ていきます。

繰り返しになりますが、ゲームの保存とゲームの更新の違いは何ですか?基本的に、ゲームの更新機能では、経過時間を含む数独パズル全体を大規模に作成しますが、実際に必要なのはゲームの経過時間を更新することだけである場合もあります。 、ユーザーがアプリケーションから離れたときなど。

ここで私が行っているのは、ユーザーの観点から達成したいことに応じて、ある種の特殊な機能を作成していることです。

ここで、これらの特定の関数型から実際に値を返したい場合を示します。

さて、onsuccess のために、ここで効果的に言っているのは、この特定の関数は、この特定のサスペンド関数の実装で呼び出されるとき、明らかに true または false のいずれかのブール値で呼び出されなければならないということです。

基本的に、ここで行っていることは、ユーザーが数独パズルの 1 つのノードまたはタイルを更新したときに、それがパズルの最後のタイルになる可能性があるということです。

その特定のケースでは、パズルが正しく、ユーザーが最後のタイルを入力したと仮定すると、それは獲得が完了したことを示します。

そのため、ここでは少し混乱するかもしれません.

しかし、成功したからといって、必ずしもゲーム自体が完成したわけではありません。

だからこそ、そこで差別化を図っています。

さて、この特定のケースでは、ユーザーがアクティブなゲームに戻ったときに、現在のゲームを取得したいと考えています。

実際には、ユーザーがゲームを完了してアプリケーションから離れ、アプリケーションを再起動するというエッジ ケースがあります。

そのため、まだ完了フラグを渡しています。

ここでは、明らかに設定オブジェクトを返します。

別のインターフェイスを作成しましょう。

そして、それは I gain、データ ストレージと呼ばれる予定です。

そして、それは明らかにインターフェースです。

さて、インターフェイス自体を書く前に、少し違うことをします.

結果ラッパーを作成するか、関数型プログラマーのいずれかの mon 広告に触発されたとだけ言っておきましょう。

Seal クラスは、kotlin プログラミング言語のシンプルな機能の中で、私が本当に気に入っている機能の 1 つです。

これにより、制限されたタイプのセットを作成でき、それらのタイプには特定の値を含めることができます。

そして、基本的にこれが何をするか、すぐにわかるように、ゲームデータストレージの特定の関数からオブジェクトを返すことができます.

そして、この特定のオブジェクトは、複数の異なる状態を表すことができます.

前に AI ゲーム リポジトリで示したように、ここでは、エラー状態と成功状態を 2 つの別個の関数参照として表しています。

この例では、これら両方の状態を 1 つのオブジェクトで表します。

先に進む前に、最後のポイントとして、たとえば、成功事例のみを表現したい状況がある場合、onsuccess で unit を返すのと同じように、実際にできることは、オブジェクトを使用して、完成したオブジェクトのように言うことができます。

しかし、このアプリケーションでは実際にはそれを行いません。

したがって、それを追加する必要はありません。

さぁ、インターフェースを完成させましょう。

I 設定ストレージと呼ばれる別のインターフェイスを作成します。

ここで、結果ラッパーも使用します。

ですから、実際にはこれをコピーして貼り付けます。

そして、いくつかの名前を変更します。

よし、インターフェースを書こう。

このパッケージ用のもう 1 つのインターフェースです。

これは、リポジトリと綴ることができれば pi 統計と呼ばれることになり、明らかにインターフェイスになります。

ここでもう 1 つの最後のポイントは、更新される統計がレコードであるかどうかに基づいて、レコードが実際にユーザー インターフェイスの何かに影響を与えることです。つまり、特定の境界または数独パズルのサイズの完了の最短時間です。

共通パッケージには、さまざまなクラスや機能で再利用されるコードが含まれています。

チュートリアルのこの部分では、インテリジェントで効率的な方法でコードを共有するために設計された、さまざまな kotlin 言語機能について学習します。

対象となるトピックには、拡張機能と問題が含まれます。これらの抽象クラス、オープン クローズド原則、オブジェクト、シングルトン、コ ルーチン ディスパッチャーなどです。

コードを書く前に、オープン クローズドの原則について話しましょう。

OCP は比較的紛らわしい概念ですが、後で分解する独自の口頭での定義を提供するために、できる限り明確な方法で説明しようとします。

変更が予想される一般的に再利用されるソフトウェア エンティティには、固定のパブリック インターフェイスと、その実装を変更する方法が必要です。

その特定の定義を理解するために、いくつかのことを解き明かす必要があります。

まず、ソフトウェア エンティティと言う場合、通常はクラスまたは関数について話していますが、それ以外のいくつかのことも考えられます。

それでは、パブリック インターフェイスとは何を意味するのか、また、一般的に再利用されている場合は修正する必要があると主張する理由を調べてみましょう。

パブリック インターフェイスとは、具体的に Java または kotlin インターフェイスについて話しているのではなく、クラスまたは関数の公開されている側面を意味します。

これは Android のチュートリアルなので、アクティビティ クラスの例を見てみましょう。

アクティビティは、頻繁に再利用され、時間の経過とともに変化することが期待されるという私の要件に適合します。

したがって、OCP について考えるのに最適なケースです。

アクティビティのすべてのサブクラスには、パブリック インターフェイスの一部である onCreate 関数を含める必要があり、それを参照するクラスで使用できるようになります。

このパブリック インターフェイスを変更しない理由は非常に単純です。

Android プラットフォームの開発者が突然、保存されたインスタンス、状態バンドルをすべてのライフサイクル関数から非推奨にして削除することを決定したとします。

この公開インターフェースは、このプラットフォームの更新に関連するほぼすべての Android プログラムで使用されているため、すべてのコードが壊れてしまいます。

つまり、このパラメーターを削除していないすべてのコード ベースのすべてのアクティビティ サブクラスはコンパイルできないということです。

これが、アクティビティなどの一般的に再利用されるソフトウェア エンティティについて具体的に話している理由であり、それらのパブリック インターフェイスの変更をできるだけ少なくすることが重要である理由です。

固定の公開インターフェースが本当に重要な理由を確立したので.

次の質問は簡単です。

次に、パブリック インターフェイスの実装を変更するためのメカニズムまたは方法をどのように提供しますか? kotlin には、この問題を解決するための多くのオプションがあることがわかりました。

それらをすべて口頭で説明するのではなく、コードで使用する方法を教えます。

このアプリケーションをビルドするときに、共通パッケージを右クリックして、新しい kotlin ファイルまたはクラスに移動します。

そして、これは実際には抽象クラスになる予定であり、ここではオプションを提供していません.

基本ロジックを入力し、abstract キーワードを追加します。

残念ながら、単純な古い継承における抽象クラスとインターフェースの違いを説明する時間はありません.

この特定のコースでは、これを取り上げ、他のビデオ コースで非常に明確に説明しています。

しかし、ここでインタフェースの代わりに抽象クラスを使用している理由を説明します。

抽象クラスを使用する理由は、動作を共有したい場合です。

たとえば、関数スタブまたは抽象関数を作成します。これらは、基本ロジックから継承するすべてのクラスで共有したいと考えています。

また、変数を共有したいのですが、この特定の変数は public ではなく保護する必要があります。

インターフェイスを使用してこれを実行しようとすると、必然的にその特定の値がパブリックになるため、ジェネリック型も使用します。

それでは、その方法をお見せしましょう。

したがって、ジェネリック型の構文は山かっこを使用するだけです。

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.

おめでとう。




  1. これらの3つの無料Androidアプリでアニメーションテキストを簡単に作成

    アニメーションテキストは、通常のテキストよりもずっと楽しく見ることができます。テキストがアニメーション化されるときに追加できるすべての効果により、メッセージがより魅力的になります。良いニュースは、作成に役立つアプリがあることです。 リストにある次のアプリは無料です。期待したものでない場合は、アンインストールするだけです。アプリには、アニメーションテキストに映画のイントロタイプスタイルなどのさまざまな機能を提供するさまざまなオプションがあります。 1。アニメーションテキスト アニメーションテキストはリストの一番上にあります。優れたアニメーションオプションを備えた使いやすいアプリです。アプリを

  2. React Native と Native Base を使用して Android ニュース アプリを作成する方法

    私たちは物事が絶えず変化する世界に住んでいます。したがって、何が起こっているかを最新の状態に保ちたい場合は、優れたニュースアプリが必要になります. クールな技術を学び、最新の状態を保つために、このブログ投稿では、React Native を使用して Android 用のニュース アプリケーションを作成します。さまざまなニュース チャンネルから見出しを取得し、カテゴリ別に表示します。 完了すると、アプリは次のようになります。それでは早速見ていきましょう。 Expo のインストール方法 では、万博とは? Expo は、React Native アプリをすばやく簡単に構築してデプロイするの