The source code of an application Weather Averages
You are free to use this code in any manner you see fit, but please refrain from creating direct clones of my application on Google Play Store or other platforms.
package ua.in.khol.oleh.touristweathercomparer;
public interface Secrets {
String OPEN_WEATHER_API_KEY = "YOUR_OPEN_WEATHER_API_KEY";
String GEOCODING_API_KEY = "YOUR_GEOCODING_API_KEY";
String PLACES_API_KEY = "YOUR_PLACES_API_KEY";
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="maps_sdk_for_android_api_key" translatable="false">YOUR_MAPS_SDK_FOR_ANDROID_API_KEY</string>
</resources>
MVVM + DI + RX
In the Weather Averages application, ViewModel retrieval is handled uniquely in Activity and Fragment components. This process is critical to the MVVM architecture, ensuring that ViewModels are appropriately instantiated and tied to their respective lifecycle owners.
This method is used in the ActivityView
class to dynamically determine the ViewModel class type associated with a specific activity. It employs Java generics and reflection to achieve this:
package ua.in.khol.oleh.touristweathercomparer.ui;
public abstract class ActivityView<VM extends ActivityViewModel>
extends AppCompatActivity
implements ViewModelStoreOwner {
private Class<VM> getViewModelClass() {
Class<? extends ActivityView> aClass = getClass();
Class superclass = aClass.getSuperclass();
Type genericSuperclass = superclass.getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type actualTypeArgument = parameterizedType.getActualTypeArguments()[0];
return (Class<VM>) actualTypeArgument;
}
}
In FragmentView
, the getViewModelClass method operates slightly differently, tailored for fragments:
package ua.in.khol.oleh.touristweathercomparer.ui;
public abstract class FragmentView<VM extends FragmentViewModel>
extends Fragment {
private Class<VM> getViewModelClass() {
Class<? extends FragmentView> aClass = getClass();
Type genericSuperclass = aClass.getGenericSuperclass();
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type actualTypeArgument = parameterizedType.getActualTypeArguments()[0];
return (Class<VM>) actualTypeArgument;
}
}
Location API
Weather API
Android Architecture Components
AndroidX
- Navigation Version 2.7.6
- Constraintlayout Version 2.7.6
- Appcompat Version 1.6.1
- Recyclerview Version 1.3.2
- Cardview Version 1.0.0
- Preference Version 1.2.1
- Room Version 2.6.1
ReactiveX
Square Open Source
- OkHttp Version 4.12.0
- Picasso Version 2.8
- Retrofit Version 2.9.0
- RxJava2 Adapter Version 2.9.0
- Gson Converter Version 2.9.0
- LeakCanary Version 2.13
Copyright 2020-2024 Oleh Kholiavchuk
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.