kindof understand mvvm

This commit is contained in:
Bryson Steck 2021-05-02 22:38:15 -06:00
parent 1bf7224e15
commit 07fd2f2f47
11 changed files with 133 additions and 14 deletions

View file

@ -38,4 +38,23 @@ dependencies {
testImplementation 'junit:junit:4.+' testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
dependencies {
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava2 support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - RxJava3 support for Room
implementation "androidx.room:room-rxjava3:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
}
} }

View file

@ -3,14 +3,31 @@ package com.example.wiimmterfaceandroid;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentContainerView;
import androidx.room.Room;
import android.os.Bundle; import android.os.Bundle;
import com.example.wiimmterfaceandroid.database.AppDatabase;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
FragmentContainerView fcInput = findViewById(R.id.recent_friend_codes_fragment);
AppDatabase database = Room.databaseBuilder(this, AppDatabase.class, "friend-codes-db").build();
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.friend_code_input_fragment, WatchCodeFragment.class, null)
.setReorderingAllowed(true)
.commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.recent_friend_codes_fragment, RecentCodesFragment.class, null)
.setReorderingAllowed(true)
.commit();
}
} }
} }

View file

@ -1,4 +1,9 @@
package com.example.wiimmterfaceandroid; package com.example.wiimmterfaceandroid;
public class RecentCodesFragment { import androidx.fragment.app.Fragment;
public class RecentCodesFragment extends Fragment {
public RecentCodesFragment() { super(R.layout.fragment_recent_friend_codes); }
} }

View file

@ -1,4 +1,19 @@
package com.example.wiimmterfaceandroid; package com.example.wiimmterfaceandroid;
public class WatchCodeFragment { import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class WatchCodeFragment extends Fragment {
public WatchCodeFragment() { super(R.layout.friend_code_input_fragment); }
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
} }

View file

@ -0,0 +1,11 @@
package com.example.wiimmterfaceandroid.database;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import com.example.wiimmterfaceandroid.model.FriendCode;
@Database(entities = {FriendCode.class}, version=1)
public abstract class AppDatabase extends RoomDatabase {
public abstract FriendCodeDao getFriendCodeDao();
}

View file

@ -0,0 +1,30 @@
package com.example.wiimmterfaceandroid.database;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import com.example.wiimmterfaceandroid.model.FriendCode;
import java.util.List;
@Dao
public interface FriendCodeDao {
@Query("SELECT * FROM friendcode")
public List<FriendCode> getAll();
@Query("SELECT * FROM friendcode")
public FriendCode findByCode(String friendCode);
@Insert
public void insert(FriendCode friendCode);
@Update
public void update(FriendCode friendCode);
@Delete
public void delete(FriendCode friendCode);
}

View file

@ -0,0 +1,13 @@
package com.example.wiimmterfaceandroid.model;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
@Entity
public class FriendCode {
@ColumnInfo(name="name")
public String name;
@ColumnInfo(name="friendCode")
public String friendCode;
}

View file

@ -0,0 +1,9 @@
package com.example.wiimmterfaceandroid.viewmodel;
import com.example.wiimmterfaceandroid.model.FriendCode;
public class FriendCodeViewModel {
FriendCode friendCode = new FriendCode();
}

View file

@ -7,16 +7,17 @@
android:id="@+id/drawer_layout" android:id="@+id/drawer_layout"
tools:context=".MainActivity"> tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true"> android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
style="@style/Widget.MaterialComponents.AppBarLayout.Primary" android:id="@+id/appBarLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:fitsSystemWindows="true"> android:fitsSystemWindows="true"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar <com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar" android:id="@+id/toolbar"
@ -31,22 +32,22 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView <androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container2" android:id="@+id/friend_code_input_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />
<androidx.fragment.app.FragmentContainerView <androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container" android:id="@+id/recent_friend_codes_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" android:hapticFeedbackEnabled="false"
/> app:layout_constraintTop_toBottomOf="@+id/friend_code_input_fragment" />
<!-- Screen content --> <!-- Screen content -->
<!-- Use app:layout_behavior="@string/appbar_scrolling_view_behavior" to fit below top app bar --> <!-- Use app:layout_behavior="@string/appbar_scrolling_view_behavior" to fit below top app bar -->
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout> </androidx.drawerlayout.widget.DrawerLayout>

View file

@ -31,7 +31,7 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView <androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container" android:id="@+id/recent_friend_codes_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> app:layout_behavior="@string/appbar_scrolling_view_behavior" />

View file

@ -22,7 +22,6 @@
android:drawablePadding="15dp" android:drawablePadding="15dp"
android:ems="10" android:ems="10"
android:inputType="textPersonName" android:inputType="textPersonName"
android:text="Friend code"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" /> app:layout_constraintTop_toBottomOf="@+id/textView2" />