From 68ca4e4f862d45d0f0577e322f0717fb1f92f3b8 Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Fri, 13 May 2022 18:33:39 +0200 Subject: [PATCH] Rename all file to kebab-case --- Android/Activity.kt.md | 438 ------------------ Android/Adapter.kt.md | 86 ---- Android/Fragment.md | 154 ------ Android/Gradle & Building.md | 35 -- Android/MapsActivity.md | 84 ---- Android/SQLite.md | 101 ---- Android/activity.xml.md | 215 --------- C++/{C++.md => cpp.md} | 0 Bash/Bash Commands.md => bash/commands.md | 0 Bash/Bash Scripting.md => bash/scripting.md | 0 CSS/CSS.md => css/css.md | 0 Database/MongoDB.md => database/mongo-db.md | 0 Database/Redis.md => database/redis.md | 0 Database/SQL.md => database/sql.md | 0 {DotNet => dotnet}/C#/C#.md | 0 .../C#/async-programming.md | 0 .../C#/collections.md | 0 DotNet/C#/LINQ.md => dotnet/C#/linq.md | 0 .../C#/reactive-extensions.md | 0 .../C#/unit-tests.md | 0 .../asp.net}/Razor Syntax.md | 0 .../asp.net/app-configuration.md | 0 .../Blazor.md => dotnet/asp.net/blazor.md | 0 .../Filters.md => dotnet/asp.net/filters.md | 0 .../asp.net/middleware.md | 0 .../asp.net/minimal-api.md | 0 .../ASP.NET/MVC.md => dotnet/asp.net/mvc.md | 0 .../asp.net/razor-pages.md | 0 .../REST API.md => dotnet/asp.net/rest-api.md | 0 .../SignalR.md => dotnet/asp.net/signal-r.md | 0 .../asp.net/web-forms.md | 0 .../ADO.NET.md => dotnet/database/ado.net.md | 0 .../database/entity-framework.md | 0 .../Scripting.md => dotnet/godot/scripting.md | 0 .../SkiaSharp.md => dotnet/lib/skia-sharp.md | 0 .../unity/collisions.md | 0 .../unity/coroutines.md | 0 .../unity/input-manager.md | 0 .../unity/prefabs-instantiation.md | 0 .../unity/raycasting.md | 0 .../Scripting.md => dotnet/unity/scripting.md | 0 .../unity/vectors-tranfrorms-space.md | 0 .../xamarin/app.xaml.cs.md | 0 .../App.xaml.md => dotnet/xamarin/app.xaml.md | 0 .../xamarin/page.xaml.cs.md | 0 .../xamarin/page.xaml.md | 0 .../xamarin/view-model.cs.md | 0 .../xamarin/xamarin-essentials.md | 0 {Git => git}/git.md | 0 GraphQL.md => graph-ql.md | 0 HTML/HTML.md => html/html.md | 0 iOS/App (XCode 12).md | 14 - iOS/AppDelegate (XCode 11) .md | 75 --- iOS/SceneDelegate (XCode 11).md | 58 --- iOS/ContentView.md => ios/content-view.md | 0 Java/DAO.md => java/dao.md | 0 .../java-collection-framework.md | 0 Java/Java.md => java/java.md | 0 .../spring}/pom.xml.md | 0 .../spring/spring-project.md | 0 .../Servlet.md => java/web/servlet.md | 0 JavaScript/AJAX.md => javascript/ajax.md | 0 JavaScript/DOM.md => javascript/dom.md | 0 .../events-animation.md | 0 .../JavaScript.md => javascript/javascript.md | 0 JavaScript/jQuery.md => javascript/jquery.md | 0 .../react/react-router.md | 0 .../react/react-tests.md | 0 .../React.md => javascript/react/react.md | 0 .../react/redux-tests.md | 0 .../Redux.md => javascript/react/redux.md | 0 .../Svelte.md => javascript/svelte/svelte.md | 0 {Kotlin => kotlin}/kotlin.md | 0 Markdown.md => markdown.md | 0 .../composer.md | 0 PHP/Database.md => php/database.md | 0 .../dependecy-injection.md | 0 PHP/PHP.md => php/php.md | 0 .../plates-templating.md | 0 PHP/PSR-7.md => php/psr-7.md | 0 .../REST API.md => php/simple-mvc/rest-api.md | 0 .../simple-mvc/simple-mvc.md | 0 PHP/Unit Testing.md => php/unit-tests.md | 0 PHP/Web.md => php/web.md | 0 .../commands.md | 0 .../scripting.md | 0 .../jupyter/jupiter-notebook.md | 0 {Python/Libs/CLI => python/libs/cli}/click.md | 0 .../Tkinter.md => python/libs/gui/tkinter.md | 0 .../Pillow.md => python/libs/image/pillow.md | 0 .../libs/image/py-cairo.md | 0 .../NumPy.md => python/libs/math/numpy.md | 0 .../Pandas.md => python/libs/math/pandas.md | 0 .../Libs/Math => python/libs/math}/seaborn.md | 0 .../libs/web/beautiful-soup.md | 0 .../libs/web/flask/flask-requests.md | 0 .../libs/web/flask/flask.md | 0 .../Libs/Web => python/libs/web}/requests.md | 0 Python/Python.md => python/python.md | 0 .../standard-library}/argparse.md | 0 .../standard-library}/collections.md | 0 .../standard-library}/csv.md | 0 .../standard-library}/ftplib.md | 0 .../standard-library}/itertools.md | 0 .../standard-library}/json.md | 0 .../standard-library}/logging.md | 0 .../standard-library}/shutil.md | 0 .../standard-library}/smtplib.md | 0 .../standard-library}/socket.md | 0 .../standard-library}/sqllite.md | 0 .../standard-library}/time_datetime.md | 0 .../standard-library}/unittest.md | 0 ...arExpressions.md => regular-expressions.md | 0 Rust/Cargo.md => rust/cargo.md | 0 Rust/Rust.md => rust/rust.md | 0 Rust/Unit Tests.md => rust/unit-tests.md | 0 Swift/Swift.md => swift/swift.md | 0 117 files changed, 1260 deletions(-) delete mode 100644 Android/Activity.kt.md delete mode 100644 Android/Adapter.kt.md delete mode 100644 Android/Fragment.md delete mode 100644 Android/Gradle & Building.md delete mode 100644 Android/MapsActivity.md delete mode 100644 Android/SQLite.md delete mode 100644 Android/activity.xml.md rename C++/{C++.md => cpp.md} (100%) rename Bash/Bash Commands.md => bash/commands.md (100%) rename Bash/Bash Scripting.md => bash/scripting.md (100%) rename CSS/CSS.md => css/css.md (100%) rename Database/MongoDB.md => database/mongo-db.md (100%) rename Database/Redis.md => database/redis.md (100%) rename Database/SQL.md => database/sql.md (100%) rename {DotNet => dotnet}/C#/C#.md (100%) rename DotNet/C#/Async Programming.md => dotnet/C#/async-programming.md (100%) rename DotNet/C#/C# Collections.md => dotnet/C#/collections.md (100%) rename DotNet/C#/LINQ.md => dotnet/C#/linq.md (100%) rename DotNet/C#/Reactive Extensions.md => dotnet/C#/reactive-extensions.md (100%) rename DotNet/C#/Unit Testing.md => dotnet/C#/unit-tests.md (100%) rename {DotNet/ASP.NET => dotnet/asp.net}/Razor Syntax.md (100%) rename DotNet/ASP.NET/App Configuration.md => dotnet/asp.net/app-configuration.md (100%) rename DotNet/ASP.NET/Blazor.md => dotnet/asp.net/blazor.md (100%) rename DotNet/ASP.NET/Filters.md => dotnet/asp.net/filters.md (100%) rename DotNet/ASP.NET/Middleware.md => dotnet/asp.net/middleware.md (100%) rename DotNet/ASP.NET/Minimal API.md => dotnet/asp.net/minimal-api.md (100%) rename DotNet/ASP.NET/MVC.md => dotnet/asp.net/mvc.md (100%) rename DotNet/ASP.NET/Razor Pages.md => dotnet/asp.net/razor-pages.md (100%) rename DotNet/ASP.NET/REST API.md => dotnet/asp.net/rest-api.md (100%) rename DotNet/ASP.NET/SignalR.md => dotnet/asp.net/signal-r.md (100%) rename DotNet/ASP.NET/WebForms.md => dotnet/asp.net/web-forms.md (100%) rename DotNet/Database/ADO.NET.md => dotnet/database/ado.net.md (100%) rename DotNet/Database/EntityFramework.md => dotnet/database/entity-framework.md (100%) rename DotNet/Godot/Scripting.md => dotnet/godot/scripting.md (100%) rename DotNet/Lib/SkiaSharp.md => dotnet/lib/skia-sharp.md (100%) rename DotNet/Unity/Collisions.md => dotnet/unity/collisions.md (100%) rename DotNet/Unity/Coroutines.md => dotnet/unity/coroutines.md (100%) rename DotNet/Unity/Input Manager.md => dotnet/unity/input-manager.md (100%) rename DotNet/Unity/Prefabs Instantiation.md => dotnet/unity/prefabs-instantiation.md (100%) rename DotNet/Unity/Raycasting.md => dotnet/unity/raycasting.md (100%) rename DotNet/Unity/Scripting.md => dotnet/unity/scripting.md (100%) rename DotNet/Unity/Vector, Tranfrorm, Space.md => dotnet/unity/vectors-tranfrorms-space.md (100%) rename DotNet/Xamarin/App.xaml.cs.md => dotnet/xamarin/app.xaml.cs.md (100%) rename DotNet/Xamarin/App.xaml.md => dotnet/xamarin/app.xaml.md (100%) rename DotNet/Xamarin/Page.xaml.cs.md => dotnet/xamarin/page.xaml.cs.md (100%) rename DotNet/Xamarin/Page.xaml.md => dotnet/xamarin/page.xaml.md (100%) rename DotNet/Xamarin/ViewModel.cs.md => dotnet/xamarin/view-model.cs.md (100%) rename DotNet/Xamarin/Xamarin.Essentials.md => dotnet/xamarin/xamarin-essentials.md (100%) rename {Git => git}/git.md (100%) rename GraphQL.md => graph-ql.md (100%) rename HTML/HTML.md => html/html.md (100%) delete mode 100644 iOS/App (XCode 12).md delete mode 100644 iOS/AppDelegate (XCode 11) .md delete mode 100644 iOS/SceneDelegate (XCode 11).md rename iOS/ContentView.md => ios/content-view.md (100%) rename Java/DAO.md => java/dao.md (100%) rename Java/Java Collection Framework.md => java/java-collection-framework.md (100%) rename Java/Java.md => java/java.md (100%) rename {Java/Spring Framework => java/spring}/pom.xml.md (100%) rename Java/Spring Framework/Spring Project Structure.md => java/spring/spring-project.md (100%) rename Java/Java Web/Servlet.md => java/web/servlet.md (100%) rename JavaScript/AJAX.md => javascript/ajax.md (100%) rename JavaScript/DOM.md => javascript/dom.md (100%) rename JavaScript/Events & Animation.md => javascript/events-animation.md (100%) rename JavaScript/JavaScript.md => javascript/javascript.md (100%) rename JavaScript/jQuery.md => javascript/jquery.md (100%) rename JavaScript/React/React Router.md => javascript/react/react-router.md (100%) rename JavaScript/React/React Tests.md => javascript/react/react-tests.md (100%) rename JavaScript/React/React.md => javascript/react/react.md (100%) rename JavaScript/React/Redux Tests.md => javascript/react/redux-tests.md (100%) rename JavaScript/React/Redux.md => javascript/react/redux.md (100%) rename JavaScript/Svelte/Svelte.md => javascript/svelte/svelte.md (100%) rename {Kotlin => kotlin}/kotlin.md (100%) rename Markdown.md => markdown.md (100%) rename PHP/Composer & Autoloading.md => php/composer.md (100%) rename PHP/Database.md => php/database.md (100%) rename PHP/Dependecy Injection.md => php/dependecy-injection.md (100%) rename PHP/PHP.md => php/php.md (100%) rename PHP/Templates with Plates.md => php/plates-templating.md (100%) rename PHP/PSR-7.md => php/psr-7.md (100%) rename PHP/Simple_MVC/REST API.md => php/simple-mvc/rest-api.md (100%) rename PHP/Simple_MVC/Simple_MVC.md => php/simple-mvc/simple-mvc.md (100%) rename PHP/Unit Testing.md => php/unit-tests.md (100%) rename PHP/Web.md => php/web.md (100%) rename PowerShell/Powershell Commands.md => powershell/commands.md (100%) rename PowerShell/Powershell Scripting.md => powershell/scripting.md (100%) rename Python/Jupyter/Jupiter Notebook.md => python/jupyter/jupiter-notebook.md (100%) rename {Python/Libs/CLI => python/libs/cli}/click.md (100%) rename Python/Libs/GUI/Tkinter.md => python/libs/gui/tkinter.md (100%) rename Python/Libs/Image/Pillow.md => python/libs/image/pillow.md (100%) rename Python/Libs/Image/PyCairo.md => python/libs/image/py-cairo.md (100%) rename Python/Libs/Math/NumPy.md => python/libs/math/numpy.md (100%) rename Python/Libs/Math/Pandas.md => python/libs/math/pandas.md (100%) rename {Python/Libs/Math => python/libs/math}/seaborn.md (100%) rename Python/Libs/Web/BeautifulSoup.md => python/libs/web/beautiful-soup.md (100%) rename Python/Libs/Web/Flask/requests.md => python/libs/web/flask/flask-requests.md (100%) rename Python/Libs/Web/Flask/Flask.md => python/libs/web/flask/flask.md (100%) rename {Python/Libs/Web => python/libs/web}/requests.md (100%) rename Python/Python.md => python/python.md (100%) rename {Python/Standard Library => python/standard-library}/argparse.md (100%) rename {Python/Standard Library => python/standard-library}/collections.md (100%) rename {Python/Standard Library => python/standard-library}/csv.md (100%) rename {Python/Standard Library => python/standard-library}/ftplib.md (100%) rename {Python/Standard Library => python/standard-library}/itertools.md (100%) rename {Python/Standard Library => python/standard-library}/json.md (100%) rename {Python/Standard Library => python/standard-library}/logging.md (100%) rename {Python/Standard Library => python/standard-library}/shutil.md (100%) rename {Python/Standard Library => python/standard-library}/smtplib.md (100%) rename {Python/Standard Library => python/standard-library}/socket.md (100%) rename {Python/Standard Library => python/standard-library}/sqllite.md (100%) rename {Python/Standard Library => python/standard-library}/time_datetime.md (100%) rename {Python/Standard Library => python/standard-library}/unittest.md (100%) rename RegularExpressions.md => regular-expressions.md (100%) rename Rust/Cargo.md => rust/cargo.md (100%) rename Rust/Rust.md => rust/rust.md (100%) rename Rust/Unit Tests.md => rust/unit-tests.md (100%) rename Swift/Swift.md => swift/swift.md (100%) diff --git a/Android/Activity.kt.md b/Android/Activity.kt.md deleted file mode 100644 index 508c903..0000000 --- a/Android/Activity.kt.md +++ /dev/null @@ -1,438 +0,0 @@ -# Android App Activity.kt - -## Logging - -```kotlin -Log.i("tag", "logValue") //info log -Log.d("tag", "logValue") //debug log -Log.w("tag", "logValue") //warning log -Log.e("tag", "logValue") //error log -Log.c("tag", "logValue") //critical log -``` - -## Activity Life Cycle - -![Life Cycle](../.images/android_activity-lifecycle.png) - -```kotlin -package com.its. - -import androidx.appcompat.app.AppCompatActivity -import android.os.Bundle - -class MainActivity : AppCompatActivity() { - - //entry point of the activity - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.) - } - - override fun onStart() { - } - - override fun onResume() { - } - - override fun onPause() { - } - - override fun onStop() { - } - - override fun onRestart() { - } - - override fun onDestroy() { - } -} -``` - -### Passing data between activities - -In *laughing* activity: - -```kotlin -private fun openActivity() { - - //target to open Intent(opener, opened) - val intent = Intent(this, Activity::class.java) - - //pass data to launched activity - intent.putExtra("identifier", value) - - startActivity(intent) //launch another activity - } -``` - -In *launched* activity: - -```kotlin -val identifier = intent.getExtra("identifier") -``` - -## Hooks - -### Resources Hooks - -```kotlin -R.. //access to resource - -ContextCompat.getColor(this, colorResource) //extract color from resources -getString(stringResource) //extract string from resources -``` - -### XML hooks - -Changes in xml made in kotlin are applied after the app is drawn thus overwriting instructions in xml. - -In `activity.xml`: - -```xml - -``` - -in `Activity.kt`: - -```kotlin -var element = findViewById(R.id.) //old method - -.popery = value //access and modify view contents -``` - -## Activity Components - -### Snackbar - -Component derived from material design. If using old API material design dependency must be set in gradle. - -In `build.gradle (Module:app)`: - -```gradle -dependencies { - implementation 'com.google.android.material:material:' -} -``` - -In `Activity.kt`: - -```kotlin -import com.google.android.material.snackbar.Snackbar - -Snackbar - .make(activityID, message, Snackbar.TIME_ALIVE) //create snackbar - .setAction("Button Name", { action }) //add button to snackbar - .show() -``` - -## Opening External Content - -### Opening URLs - -```kotlin -val url = "https://www.google.com" -val intent = Intent(Intent.ACTION_VIEW) - -intent.setData(Uri.parse(url)) -startActivity(intent) -``` - -### Sharing Content - -```kotlin -val intent = Intent(Intent.ACTION_SEND) - -intent.setType("text/plain") //specifying shared content type -intent.putExtra(Intent.EXTRA_MAIL, "mail@address") //open mail client and pre-compile field if share w/ mail -intent.putExtra(Intent.EXTRA_SUBJECT, "subject") -intent.putExtra(Intent.EXTRA_TEXT, "text") //necessary since type is text -startActivity(Intent.startChooser(intent, "chooser title")) //let user choose the share app -``` - -### App Google Maps - -[Documentation](https://developers.google.com/maps/documentation/urls/android-intents) - -```kotlin -val uri = Uri.parse("geo: ") -val intent = Intent(Intent.ACTION_VIEW, uri) -intent.setPackage("com.google.android.apps.maps") //app to be opened -startActivity(intent) -``` - -### Make a call (wait for user) - -Preset phone number for the call, user needs to press call button to initiate dialing. - -```kotlin -fun call() { - val intent = Intent(Intent.ACTION_DIAL) - intent.setData(Uri.parse("tel: ")) - startActivity(intent) -} - -``` - -### Make a call (directly) - -In `AndroidManifest.xml`: - -```xml - -``` - -```kotlin -//https://developer.android.com/training/permissions/requesting - -//intercept OS response to permission popup -override fun onRequestPermissionResult(requestCode: Int, permissions: Array, grantResults: IntArray) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults) -} - -fun checkCallPermission() { - //check if permission to make a call has been granted - if (ContextCompact.checkSelfPermission(context!!, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { - // if permission has not been granted request it (opens OS popup, no listener available) - // request code needs to be specific for the permission - ActivityCompat.requestPermissions(context!!, arrayOf(android.Manifest.permission.CALL_PHONE), requestCode) - } else { - call() //if permission has been already given - } -} - -@SuppressLint("MissingPermission") // suppress warning of unhandled permission (handled in checkCallPermission) -fun call() { - val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel: ")) - startActivity(intent) -} -``` - -## Lists (with RecyclerView) - -A [LayoutManager][1] is responsible for measuring and positioning item views within a `RecyclerView` as well as determining the policy for when to recycle item views that are no longer visible to the user. -By changing the `LayoutManager` a `RecyclerView` can be used to implement a standard vertically *scrolling list*, a *uniform grid*, *staggered grids*, *horizontally scrolling collections* and more. -Several stock layout managers are provided for general use. - -[Adapters][2] provide a binding from an app-specific data set to views that are displayed within a `RecyclerView`. - -[1]:https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager -[2]:https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter - -```kotlin -var array: ArrayList? = null //create ArrayList of elements to be displayed -var adapter: RecyclerViewItemAdapter? = null //create adapter to draw the list in the Activity - -array.add(item) //add item to ArrayList - -// create LayoutManager for the recyclerView -val layoutManager = LayoutManager(context, LayoutManager.VERTICAL, reverseLayout: Bool) -recycleView.LayoutManager = layoutManager // assign LayoutManager for the recyclerView - -// handle adapter var containing null value -if (array != null) { - adapter = RecyclerViewItemAdapter(array!!) // valorize adapter with a adapter object - recyclerView.adapter = adapter // assign adapter to the recyclerView -} - -// add or remove item - -// tell the adapter that something is changed -adapter?.notifyDataSetChanged() -``` - -## WebView - -[WebView Docs](https://developerandroid.com/reference/android/webkit/WebView) - -```kotlin -webView.webViewClient = object : WebViewClient() { - - // avoid opening browsed by default, open webview instead - override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { - view?.loadUrl(url) // handle every url - return true - } - - override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { - - // stay in WebView until website changes - if (Uri.parse(url).host == "www.website.domain") { - return false - } - - Intent(Intent.ACTION_VIEW, Uri.parse(url).apply){ - startActivity(this) // open browser/app when the website changes to external URL - } - return true - } - - webView.settings.javaScriptEnabled = true // enable javascript - webView.addJavaScriptInterface(webAppInterface(this, "Android")) // create bridge between js on website an app - - // if in webView use android back butto to go to previous webpage (if possible) - override fun onKeyDown(keyCode: Int, event: KeyEvent?) :Boolean { - if (keyCode == KeyEvent.KEYCODE_BACK && webVew,canGoBack()) { // if previous url exists & back button pressed - webView.goBack() // go to previous URL - return true - } - } -} -``` - -## Web Requests (Using Volley) - -[Volley Docs](https://developer.android.com/training/volley) - -### Import & Permissions - -Import in `build.gradle`: - -```kotlin -implementation 'com.android.volley:volley:1.1.1' -``` - -Permissions in `AndroidManifest.xml`: - -```xml - -``` - -### Make the request - -Subsequent requests should be delayed to avoid allowing the user to make too frequent requests. - -```kotlin -private lateinit var queue: RequestQueue - -override fun onCreate(savedInstanceState: Bundle?){ - queue = Volley.newRequestQueue(context) -} - -// response is a String -private fun simpleRequest() { - - var url = "www.website.domain" - - var stringRequest = StringRequest(Request.Method.GET, url, Response.Listener { - Log.d() - }, - Response.ErrorListener{ - Log.e() - }) - - stringRequest.TAG = "TAG" // assign a tag to the request - - queue.add(stringRequest) // add request to the queue -} - -// response is JSON Object -private fun getJsonObjectRequest(){ - // GET -> jsonRequest = null - // POST -> jsonRequest = JsonObject - var stringRequest = JsonObjectRequest(Request.Method.GET, url, jsonRequest, Response.Listener { - Log.d() - // read end use JSON - }, - Response.ErrorListener{ - Log.e() - }) - - queue.add(stringRequest) // add request to the queue -} - -// response is array of JSON objects -private fun getJSONArrayRequest(){ - // GET -> jsonRequest = null - // POST -> jsonRequest = JsonObject - var stringRequest = JsonArrayRequest(Request.Method.GET, url, jsonRequest, Response.Listener { - Log.d() - // read end use JSON - }, - Response.ErrorListener{ - Log.e() - }) - - queue.add(stringRequest) // add request to the queue -} - - -override fun onStop() { - super.onStop() - queue?.cancelAll("TAG") // delete all request with a particular tag when the activity is closed (avoid crash) -} -``` - -### Parse JSON Request - -```kotlin -Response.Listener { response -> - var value = response.getSting("key") -} -``` - -## Data Persistance - -### Singleton - -Object instantiated during app init and is destroyed only on app closing. It can be used for data persistance since is not affected by the destruction of an activity. - -```kotlin -// Context: Interface to global information about an application environment. -class Singleton constructor(context: Context) { - - companion object { - - @Volatile - private var INSTANCE: Singleton? = null - - // synchronized makes sure that all instances of the singleton are actually the only existing one - fun getInstance(context: Context) = INSTANCE ?: synchronized(this) { - INSTANCE ?: Singleton(context).also { - INSTANCE = it - } - } - } -} -``` - -### SharedPreferences - -[SharedPreferences Docs](https://developer.android.com/training/data-storage/shared-preferences) - -**Get a handle to shared preferences**: - -- `getSharedPreferences()` — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app. -- `getPreferences()` — Use this from an Activity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name. - -```kotlin -val sharedPref = activity?.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE ) -val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) -``` - -**Write to shared preferences**: - -To write to a shared preferences file, create a `SharedPreferences.Editor` by calling `edit()` on your SharedPreferences. - -Pass the keys and values to write with methods such as `putInt()` and `putString()`. Then call `apply()` or `commit()` to save the changes. - -```kotlin -val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return -with (sharedPref.edit()) { - putInt(getString(R.string.key), value) - commit() // or apply() -} -``` - -`apply()` changes the in-memory `SharedPreferences` object immediately but writes the updates to disk *asynchronously*. -Alternatively, use `commit()` to write the data to disk *synchronously*. But because *commit()* is synchronous, avoid calling it from your main thread because it could pause the UI rendering. - -**Read from shared preferences**: - -To retrieve values from a shared preferences file, call methods such as getInt() and getString(), providing the key for the wanted value, and optionally a default value to return if the key isn't present. - -```kotlin -val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return -val defaultValue = resources.getInteger(R.integer.default_value_key) -val value = sharedPref.getInt(getString(R.string.key), defaultValue) -``` diff --git a/Android/Adapter.kt.md b/Android/Adapter.kt.md deleted file mode 100644 index 0c99941..0000000 --- a/Android/Adapter.kt.md +++ /dev/null @@ -1,86 +0,0 @@ -# RecycleView Cell Adapter - -[Adapters][1] provide a binding from an app-specific data set to views that are displayed within a `RecyclerView`. - -[1]:https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter - -```kotlin -package - -import android.content.Context -import android.util.Log -import android.view.LayoutInflater -import android.view.ViewGroup -import android.widget.Button -import android.widget.TextView -import androidx.recyclerview.widget.RecyclerView - -class RecipeAdapter : RecyclerView.Adapter { - - private var mDataset: ArrayList // RecyclerView data - - class ViewHolder : RecyclerView.ViewHolder { - var viewGroup: ViewGroup? = null - constructor(v: ViewGroup?) : super(v!!) { - viewGroup = v - } - } - - //adapter contractor, takes list of data - constructor(myDataset: ArrayList/*, mContext: Context?*/){ - mDataset = myDataset - //mContext = mContext - } - - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val v : ViewGroup = LayoutInflater.from(parent.context).inflate(R.layout.view_recipe_item, parent, false) as ViewGroup - - val vh = ViewHolder(v) - return vh - } - - override fun getItemCount(): Int { - return mDataset.size - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - - val mItem : Recipe = mDataset.get(position) - - val titleText = holder.viewGroup?.findViewById(R.id.titleText) - titleText?.text = mItem.title - - Log.d("Adapter","Title: "+mItem.title) - - val deleteButton = holder.viewGroup!!.findViewById