From 3227e43fc4bc914f8bde1d06017ae16ced3fe278 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 29 Sep 2019 09:49:30 +0800 Subject: Change project structures. --- app/src/main/java/User/UserDao.java | 22 ---------- app/src/main/java/User/UserData.java | 65 ------------------------------ app/src/main/java/User/UserRepository.java | 40 ------------------ app/src/main/java/User/UserViewModel.java | 49 ---------------------- 4 files changed, 176 deletions(-) delete mode 100644 app/src/main/java/User/UserDao.java delete mode 100644 app/src/main/java/User/UserData.java delete mode 100644 app/src/main/java/User/UserRepository.java delete mode 100644 app/src/main/java/User/UserViewModel.java (limited to 'app/src/main/java/User') diff --git a/app/src/main/java/User/UserDao.java b/app/src/main/java/User/UserDao.java deleted file mode 100644 index 2bf7b824..00000000 --- a/app/src/main/java/User/UserDao.java +++ /dev/null @@ -1,22 +0,0 @@ -package User; - -import androidx.lifecycle.LiveData; -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -@Dao -public interface UserDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(UserData userData); - - @Query("DELETE FROM users") - void deleteAllUsers(); - - @Query("SELECT * FROM users WHERE name = :userName COLLATE NOCASE LIMIT 1") - LiveData getUserLiveData(String userName); - - @Query("SELECT * FROM users WHERE name = :userName COLLATE NOCASE LIMIT 1") - UserData getUserData(String userName); -} diff --git a/app/src/main/java/User/UserData.java b/app/src/main/java/User/UserData.java deleted file mode 100644 index b3f71029..00000000 --- a/app/src/main/java/User/UserData.java +++ /dev/null @@ -1,65 +0,0 @@ -package User; - -import androidx.annotation.NonNull; -import androidx.room.ColumnInfo; -import androidx.room.Entity; -import androidx.room.PrimaryKey; - -@Entity(tableName = "users") -public class UserData { - @PrimaryKey - @NonNull - @ColumnInfo(name = "name") - private String name; - @ColumnInfo(name = "icon") - private String iconUrl; - @ColumnInfo(name = "banner") - private String banner; - @ColumnInfo(name = "karma") - private int karma; - @ColumnInfo(name = "is_gold") - private boolean isGold; - @ColumnInfo(name = "is_friend") - private boolean isFriend; - @ColumnInfo(name = "can_be_followed") - private boolean canBeFollowed; - - public UserData(@NonNull String name, String iconUrl, String banner, int karma, boolean isGold, boolean isFriend, boolean canBeFollowed) { - this.name = name; - this.iconUrl = iconUrl; - this.banner = banner; - this.karma = karma; - this.isGold = isGold; - this.isFriend = isFriend; - this.canBeFollowed = canBeFollowed; - } - - @NonNull - public String getName() { - return name; - } - - public String getIconUrl() { - return iconUrl; - } - - public String getBanner() { - return banner; - } - - public int getKarma() { - return karma; - } - - public boolean isGold() { - return isGold; - } - - public boolean isFriend() { - return isFriend; - } - - public boolean isCanBeFollowed() { - return canBeFollowed; - } -} diff --git a/app/src/main/java/User/UserRepository.java b/app/src/main/java/User/UserRepository.java deleted file mode 100644 index d781a694..00000000 --- a/app/src/main/java/User/UserRepository.java +++ /dev/null @@ -1,40 +0,0 @@ -package User; - -import android.os.AsyncTask; - -import androidx.lifecycle.LiveData; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class UserRepository { - private UserDao mUserDao; - private LiveData mUserLiveData; - - UserRepository(RedditDataRoomDatabase redditDataRoomDatabase, String userName) { - mUserDao = redditDataRoomDatabase.userDao(); - mUserLiveData = mUserDao.getUserLiveData(userName); - } - - LiveData getUserLiveData() { - return mUserLiveData; - } - - public void insert(UserData userData) { - new InsertAsyncTask(mUserDao).execute(userData); - } - - private static class InsertAsyncTask extends AsyncTask { - - private UserDao mAsyncTaskDao; - - InsertAsyncTask(UserDao dao) { - mAsyncTaskDao = dao; - } - - @Override - protected Void doInBackground(final UserData... params) { - mAsyncTaskDao.insert(params[0]); - return null; - } - } -} diff --git a/app/src/main/java/User/UserViewModel.java b/app/src/main/java/User/UserViewModel.java deleted file mode 100644 index 2c746cad..00000000 --- a/app/src/main/java/User/UserViewModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package User; - -import android.app.Application; -import androidx.lifecycle.AndroidViewModel; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.ViewModel; -import androidx.lifecycle.ViewModelProvider; -import androidx.annotation.NonNull; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class UserViewModel extends AndroidViewModel { - private UserRepository mSubredditRepository; - private LiveData mUserLiveData; - - public UserViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String id) { - super(application); - mSubredditRepository = new UserRepository(redditDataRoomDatabase, id); - mUserLiveData = mSubredditRepository.getUserLiveData(); - } - - public LiveData getUserLiveData() { - return mUserLiveData; - } - - public void insert(UserData userData) { - mSubredditRepository.insert(userData); - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - - @NonNull - private final Application mApplication; - private final RedditDataRoomDatabase mRedditDataRoomDatabase; - private final String mUsername; - - public Factory(@NonNull Application application, RedditDataRoomDatabase redditDataRoomDatabase, String username) { - mApplication = application; - mRedditDataRoomDatabase = redditDataRoomDatabase; - mUsername = username; - } - - @Override - public T create(Class modelClass) { - //noinspection unchecked - return (T) new UserViewModel(mApplication, mRedditDataRoomDatabase, mUsername); - } - } -} -- cgit v1.2.3