Skip to content

Commit

Permalink
Migrate to Jetpack View Binding (#1002)
Browse files Browse the repository at this point in the history
* refactor: migrate carselection/ views to view binding

* refactor: specify android:id for <include> envirocar_toolbar layouts

* refactor: migrate logbook/ views to view binding

* refactor: migrate dashboard/ views to view binding

* refactor: migrate BaseMainActivity & SplashScreenActivity to view binding

* chore: resolve view binding migration issues

* refactor: migrate login/ views to view binding

* refactor: migrate obdselection/ views to view binding

* refactor: migrate others/ views to view binding

* fix: specify android:id for layout/envirocar_toolbar include

* fix: DashboardFragment view binding

* fix: CarSelectionListAdapter view binding

* fix: OBDSelectionFragment view binding

* fix: BaseMainActivity setContentView call

* refactor: migrate trackdetails/ & tracklist/ views to view binding

* build: enable view binding
https://developer.android.com/topic/libraries/view-binding

* build: remove Butter Knife dependency & workaround

* fix: LogbookCarSpinnerAdapter getView NPE

* fix: BluetoothHandler registerReceiver

* fix: LogbookListAdapter getView
  • Loading branch information
alexmercerind committed May 31, 2024
1 parent dc9f955 commit 4c909a4
Show file tree
Hide file tree
Showing 53 changed files with 1,116 additions and 1,020 deletions.
7 changes: 1 addition & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
# header note.
#Tue Jan 26 12:51:21 CET 2016
sdk.dir=/home/matthes/opt/android/sdk
org.gradle.jvmargs=-Xmx2048M
android.useAndroidX=true
android.enableJetifier=true
# TODO: Remove after Butter Knife -> Jetpack View Binding migration.
android.nonFinalResIds=false
org.gradle.jvmargs=-Xmx2048M \
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ agp = "8.4.0"
androidSegmented = "1.0.6"
androidxCore = "1.13.1"
appcompat = "1.6.1"
butterknife = "10.2.3"
cardview = "1.0.0"
circleimageview = "3.1.0"
commonsCompress = "1.21"
Expand Down Expand Up @@ -71,8 +70,6 @@ androidx-rules = { module = "androidx.test:rules", version.ref = "rules" }
androidx-runner = { module = "androidx.test:runner", version.ref = "runner" }
androidx-sqlite = { module = "androidx.sqlite:sqlite", version.ref = "sqlite" }
androidx-sqlite-framework = { module = "androidx.sqlite:sqlite-framework", version.ref = "sqlite" }
butterknife = { module = "com.jakewharton:butterknife", version.ref = "butterknife" }
butterknife-compiler = { module = "com.jakewharton:butterknife-compiler", version.ref = "butterknife" }
circleimageview = { module = "de.hdodenhof:circleimageview", version.ref = "circleimageview" }
commons-compress = { module = "org.apache.commons:commons-compress", version.ref = "commonsCompress" }
core = { module = "com.google.android.play:core", version.ref = "playCore" }
Expand Down
4 changes: 1 addition & 3 deletions org.envirocar.app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ android {

buildFeatures {
buildConfig true
viewBinding true
}

signingConfigs {
Expand Down Expand Up @@ -110,9 +111,6 @@ dependencies {
implementation libs.dagger
annotationProcessor libs.dagger.compiler

implementation libs.butterknife
annotationProcessor libs.butterknife.compiler

implementation libs.otto

// Google dependencies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/envirocar_toolbar"></include>
<include
android:id="@+id/envirocar_toolbar"
layout="@layout/envirocar_toolbar"
/>

<RelativeLayout
android:layout_width="match_parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
android:background="@color/white"
android:orientation="vertical">

<include layout="@layout/envirocar_toolbar" />
<include android:id="@+id/envirocar_toolbar" layout="@layout/envirocar_toolbar" />

<RelativeLayout
android:id="@+id/activity_car_selection_top"
Expand Down
10 changes: 7 additions & 3 deletions org.envirocar.app/res/layout/activity_track_details_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@
android:text="yea"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<include layout="@layout/activity_track_details_attributes_header" />
<include
android:id="@+id/activity_track_details_attributes_header"
layout="@layout/activity_track_details_attributes_header" />

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -118,7 +120,9 @@
android:alpha="0.34"
android:background="@color/background_material_dark" />

<include layout="@layout/activity_track_details_attributes" />
<include
android:id="@+id/activity_track_details_attributes"
layout="@layout/activity_track_details_attributes" />

</LinearLayout>
</androidx.cardview.widget.CardView>
Expand All @@ -127,12 +131,12 @@

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/activity_track_details_fab"
style="@style/Widget.enviroCar.Fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:elevation="8dp"
android:src="@drawable/ic_timeline_white_24dp"
style="@style/Widget.enviroCar.Fab"
app:layout_anchor="@id/activity_track_details_appbar_layout"
app:layout_anchorGravity="bottom|right|end" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
with the enviroCar app. If not, see http://www.gnu.org/licenses/.
-->
<tools:LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -46,4 +46,4 @@
android:layout_height="0dp"
android:layout_weight="1"/>

</tools:LinearLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
android:textSize="18sp"/>
</androidx.appcompat.widget.Toolbar>

<include layout="@layout/fragment_tracklist_cardlayout_content" />
<include
android:id="@+id/fragment_tracklist_cardlayout_content"
layout="@layout/fragment_tracklist_cardlayout_content" />

</LinearLayout>
</androidx.cardview.widget.CardView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
</RelativeLayout>

<include
android:id="@+id/fragment_tracklist_cardlayout_content"
layout="@layout/fragment_tracklist_cardlayout_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion org.envirocar.app/res/layout/send_log_layout_new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/envirocar_toolbar" />
<include android:id="@+id/envirocar_toolbar" layout="@layout/envirocar_toolbar" />

<ScrollView
android:layout_width="match_parent"
Expand Down
3 changes: 0 additions & 3 deletions org.envirocar.app/src/org/envirocar/app/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;

import com.mapbox.mapboxsdk.Mapbox;

Expand All @@ -50,7 +48,6 @@
import org.envirocar.remote.service.TermsOfUseService;
import org.envirocar.remote.service.TrackService;
import org.envirocar.remote.service.UserService;
import org.envirocar.storage.EnviroCarVehicleDB;

import javax.inject.Inject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
*/
package org.envirocar.app.handler;

import android.annotation.SuppressLint;
import static android.content.Context.BLUETOOTH_SERVICE;

import android.app.Activity;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -35,19 +36,15 @@
import com.squareup.otto.Bus;
import com.squareup.otto.Produce;

import org.envirocar.app.recording.RecordingService;
import org.envirocar.app.rxutils.RxBroadcastReceiver;
import org.envirocar.app.views.others.TroubleshootingFragment;
import org.envirocar.core.events.bluetooth.BluetoothDeviceDiscoveredEvent;
import org.envirocar.core.events.bluetooth.BluetoothDeviceSelectedEvent;
import org.envirocar.core.events.bluetooth.BluetoothStateChangedEvent;
import org.envirocar.core.injection.InjectApplicationScope;
import org.envirocar.core.logging.Logger;
import org.envirocar.core.utils.ServiceUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -141,15 +138,16 @@ public BluetoothHandler(@InjectApplicationScope Context context, Bus bus) {
this.bus = bus;

// Get the default bluetooth adapter.
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

// Register ourselves on the eventbus.
this.bus.register(this);

// Register this handler class for Bluetooth State Changed broadcasts.
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
ContextCompat.registerReceiver(
this.context, bluetoothPairingReceiver, filter, ContextCompat.RECEIVER_EXPORTED
this.context, mBluetoothStateChangedReceiver, filter, ContextCompat.RECEIVER_EXPORTED
);
}

Expand Down
35 changes: 15 additions & 20 deletions org.envirocar.app/src/org/envirocar/app/views/BaseMainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import org.envirocar.app.BaseApplicationComponent;
import org.envirocar.app.R;
import org.envirocar.app.databinding.ActivityBaseMainBottomBarBinding;
import org.envirocar.app.events.TrackchunkEndUploadedEvent;
import org.envirocar.app.handler.ApplicationSettings;
import org.envirocar.app.handler.BluetoothHandler;
Expand All @@ -66,8 +67,6 @@

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
Expand All @@ -81,6 +80,8 @@ public class BaseMainActivity extends BaseInjectorActivity {

private static final String TROUBLESHOOTING_TAG = "TROUBLESHOOTING";

private ActivityBaseMainBottomBarBinding binding;

private FragmentStatePagerAdapter fragmentStatePagerAdapter;
private MenuItem prevMenuItem;

Expand Down Expand Up @@ -114,10 +115,8 @@ public class BaseMainActivity extends BaseInjectorActivity {
@Inject
protected ValidateAcceptedTerms validateTermsOfUse;

@BindView(R.id.navigation)
protected BottomNavigationView navigationBottomBar;

@BindView(R.id.fragmentContainer)
protected ViewPager viewPager;

private CompositeDisposable subscriptions = new CompositeDisposable();
Expand All @@ -127,19 +126,13 @@ public class BaseMainActivity extends BaseInjectorActivity {
private Scheduler.Worker mMainThreadWorker = AndroidSchedulers.mainThread().createWorker();


private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
switch (item.getItemId()) {
case R.id.navigation_dashboard:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_my_tracks:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_others:
viewPager.setCurrentItem(2);
return true;
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = item -> {
if (item.getItemId() == R.id.navigation_dashboard)
viewPager.setCurrentItem(0);
else if (item.getItemId() == R.id.navigation_my_tracks)
viewPager.setCurrentItem(1);
else if (item.getItemId() == R.id.navigation_others)
viewPager.setCurrentItem(2);
return false;
};

Expand All @@ -152,10 +145,12 @@ protected void injectDependencies(BaseApplicationComponent baseApplicationCompon

@Override
protected void onCreate(Bundle savedInstanceState) {
// LOGGER.info("BaseMainActivity : onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_main_bottom_bar);
ButterKnife.bind(this);
binding = ActivityBaseMainBottomBarBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

navigationBottomBar = binding.navigation;
viewPager = binding.fragmentContainer;

navigationBottomBar.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigationBottomBar.setSelectedItemId(R.id.navigation_dashboard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
*/
package org.envirocar.app.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import org.envirocar.app.BaseApplicationComponent;
import org.envirocar.app.R;
import org.envirocar.app.injection.BaseInjectorActivity;
import org.envirocar.app.views.carselection.CarSelectionAddCarFragment;
import org.envirocar.core.entity.Manufacturers;
import org.envirocar.core.logging.Logger;
import org.envirocar.storage.EnviroCarVehicleDB;
Expand All @@ -35,14 +33,11 @@

import javax.inject.Inject;

import butterknife.ButterKnife;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;

/**
Expand Down Expand Up @@ -93,7 +88,6 @@ public void onComplete() {

setContentView(R.layout.activity_splashscreen);
getWindow().setNavigationBarColor(getResources().getColor(R.color.cario_color_primary_dark));
ButterKnife.bind(this);

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
Expand Down
Loading

0 comments on commit 4c909a4

Please sign in to comment.