Mars Land is a simple demo app using ViewModel & LiveData with Retrofit, Glide and Moshi in Kotlin.
This app demonstrates the following views and techniques:
- Retrofit to make api calls to an HTTP web service
- Moshi which handles the deserialization of the returned JSON to Kotlin data objects
- Glide to load and cache images by URL.
It leverages the following components from the Jetpack library:
- ViewModel
- LiveData
- Data Binding with binding adapters
- Navigation with the SafeArgs plugin for parameter passing between fragments
![Mars Land App Preview 1](https://private-user-images.githubusercontent.com/43620548/262148292-30cb64df-bf12-4bda-8fed-a50d62f857d0.jpg?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NjM1NTMsIm5iZiI6MTcyNDU2MzI1MywicGF0aCI6Ii80MzYyMDU0OC8yNjIxNDgyOTItMzBjYjY0ZGYtYmYxMi00YmRhLThmZWQtYTUwZDYyZjg1N2QwLmpwZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDA1MjA1M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTVmMzE0YWFjZjhiYjVmMzJiODBhZjI1MDU3NGU2YmJlNDNjYzE4MzViZThlZTJjYTA1NGFhYTQ2MDY2MDI2YTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.sbaRx9QmL-KHkhW-LwZJcNbrcYrownuLHPpj-q-PaZY)
![Mars Land App Preview 2](https://private-user-images.githubusercontent.com/43620548/262148308-f6d5244f-ee23-4217-85cb-d3548bb1e402.jpg?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NjM1NTMsIm5iZiI6MTcyNDU2MzI1MywicGF0aCI6Ii80MzYyMDU0OC8yNjIxNDgzMDgtZjZkNTI0NGYtZWUyMy00MjE3LTg1Y2ItZDM1NDhiYjFlNDAyLmpwZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDA1MjA1M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTFmN2UwNjllNTRiYjdiMmM4ZGMzMDUzYzk5YzQ4YjlhNzViZGEyYzEzM2I4OTNkODUzZjNjN2IxNjliMTAyNjMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.LR6b4IQloJ_sgERQKdJpIaZFfy76vbYqwKqzXqnxeos)
![Mars Land App Preview 3](https://private-user-images.githubusercontent.com/43620548/262148328-e5f4da30-6e19-45e1-b520-c3a3521a186c.jpg?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NjM1NTMsIm5iZiI6MTcyNDU2MzI1MywicGF0aCI6Ii80MzYyMDU0OC8yNjIxNDgzMjgtZTVmNGRhMzAtNmUxOS00NWUxLWI1MjAtYzNhMzUyMWExODZjLmpwZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDA1MjA1M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTEwZmQ4OTJmNDExMzhmZTkyM2JiZjA2NDY0NGIwYzdlMjAzZjkzZmVjMTUzMzI5MjhiZWE0NTUzOGE1NDRmZWEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Ary3WFzAxA8lO1wjwdpa4nsJkNYTAZNEUj9bg31iZRo)
REpresentational State Transfer Architecture is a common stateless Web architecture built using standard web components and protocols. Web services that offer this architecture are known as RESTful services. Following are some properties of RESTful Web services.
-
Requests to RESTful Web services are made in standardized way via URIs. These URIs are transferred to servers using Http protocol. Web browsers also work on Http protocol.
-
Http protocol contains an operation to tell the server what to do. Some common HTTP operations are GET for retrieving server data, POST/PUT for updating data, DELETE for deleting data from the server.
-
The server responses with formatted data in the common Web formats like XML or JSON.
-
The service also accepts query parameters as part of the URI. Query parameter is separated from the location of the service by a question mark, and multiple parameters are separated by & sign.
https://github.com/pawanharariya?tab=repositories&sort=name
Retrofit is a library that creates a network API based on the content of web services. It fetches data from web service and routes it through a separate converter library, that decodes the data and returns it in form of objects. Converter libraries convert the server response in useful formats, like JSON. Retrofit creates all necessary network layers and runs the requests on background threads, so they don't block the UI. To use Retrofit we create following components:
-
Retrofit object : It helps in creating APIs to communicate to server. It requires the base url of the server and a Convertor factory, that build the required Covertor.
-
Interface : It explains how Retrofit talks to web server using HTTP requests. Retrofit then creates an object that implements this interface with all of its methods. This is conceptually similar to DAO of ROOM. We expose Retrofit object using Singleton pattern since service calls are expensive, and we need only one Retrofit service instance.
Permissions protect the privacy of an Android User. Apps must request permission to access sensitive user data, such as contacts or call logs or to use system features like Camera or Internet. The permissions are added using <uses-permission>
tag in the Android Manifest file. Permissions like Internet access are normal permission. But accessing contacts or camera are sensitive permissions, and above API level 23, they are requested at runtime from the user, in addition to be declared in manifest. Highly sensitive special permissions like access to change system settings, can only be given by the user by going to system settings. If the runtime permission isn't granted or permission isn't declared in the manifest, trying to use the associated feature results in SecurityException
.
Displaying an Internet image includes downloading the image, buffering and decoding from compressed format to an image that can be used by Android. It should be cached either to an in-memory cache or storage-based cache or both. And, all this should happen in background thread so that UI remains responsive.
Glide library does all this and it just needs url and the ImageView. With Glide we can also display a placeholder image while the image is loading and an error image if the loading fails. This can be done using RequestOptions()
object.
Parceling is a way of sharing objects between different processes by flattening an object into a stream of data called a parcel. Each value in the object is written in sequence to the parcel. This can be done by implementing the parcelable interface. A bundle is a parcelable object that contains key-value store of parcelable objects.
We use bundles as the argument property in fragments because of the way Android lifecycle works. When activity is destroyed, when the app running in background is killed. All information in SaveInstanceState has to be from parcelables, because it is used to recreate objects when the app gets restarted which is a new process. When activity is recreated, the fragment manager recreates all fragments, since the arguments are parcelable, bundles can be stored in SaveInstanceState. This allows fragments to preserve their arguments when the process is destroyed and fragment is recreated.