Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mvp with sqlite #18

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'


//Room dependencies
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
// butter knife
compile 'com.jakewharton:butterknife:8.8.1'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListPage" />
<activity android:name=".HomeScreen" />
<activity android:name=".ListProjects.ListProjectActivity" />
<activity android:name=".BranchPage.HomeScreenActivity" />
<activity android:name=".DescriptionActivity"></activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.androidtechies.majorproject.BranchPage;


import com.example.androidtechies.majorproject.Data.ProjectModel;

import java.util.List;

public interface HomeContract {

interface IHomePresenter {
void openNewActivity(String branch);
void insertData(List<ProjectModel> projects);
void deleteAllData();
}

interface IHomeView {
void showNewActivity(String branch);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.androidtechies.majorproject.BranchPage;

import com.example.androidtechies.majorproject.Data.DataManager;
import com.example.androidtechies.majorproject.Data.ProjectModel;

import java.util.List;

public class HomePresenter implements HomeContract.IHomePresenter{

private final DataManager manager;
private HomeContract.IHomeView view;

public HomePresenter(HomeContract.IHomeView view, DataManager dataSource) {
this.view = view;
this.manager =dataSource;
}

/*
Called on any of the four button clicks , receives branch value & passes it to view function
*/
@Override
public void openNewActivity(String branchValue) {
view.showNewActivity(branchValue);

}

/*
Inserts value into sqlite database, interacts with Datamanger only
*/
@Override
public void insertData(List<ProjectModel> projects) {
manager.saveProject(projects);
}

/*
Currently, only logs , not implemented yet
*/
@Override
public void deleteAllData() {
manager.nukeTable();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.example.androidtechies.majorproject.BranchPage;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.androidtechies.majorproject.Data.DataManager;
import com.example.androidtechies.majorproject.Data.ProjectModel;
import com.example.androidtechies.majorproject.ListProjects.ListProjectActivity;
import com.example.androidtechies.majorproject.R;
import com.example.androidtechies.majorproject.Utils.InjectionClass;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeScreenActivity extends AppCompatActivity implements View.OnClickListener,HomeContract.IHomeView{
public static final String HomeScreenTag = "HomeScreenActivity";

@BindView(R.id.cseTag)
Button cseButton;
@BindView(R.id.itTag)
Button itButton;
@BindView(R.id.eceTag)
Button eceButton;
@BindView(R.id.eeeTag)
Button eeeButton;

HomePresenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);

ButterKnife.bind(this);
DataManager source = InjectionClass.provideDataSource(this);
presenter = new HomePresenter(this , source);

//checking first time app starting
if(source.checkFirstTime()) {
source.setFirstTime(false);
Log.d(HomeScreenTag, "first time run");
//populating sqlite database
populateDatabase();
}

//setting listeners on all buttons
cseButton.setOnClickListener(this);
itButton.setOnClickListener(this);
eceButton.setOnClickListener(this);
eeeButton.setOnClickListener(this);
}


/*
Function which takes values from string-arrays & call insert function to insert into database
*/
//Todo replace team name with modules
private void populateDatabase() {
List<ProjectModel> arrayList = new ArrayList<>();
int lengthIt = getResources().getStringArray(R.array.project_titles_it).length;
ProjectModel model;
for(int i=0 ; i<lengthIt; i++) {
model = new ProjectModel(
getResources().getString(R.string.it),
getResources().getStringArray(R.array.project_titles_it)[i],
getResources().getStringArray(R.array.project_introductions_it)[i],
getResources().getStringArray(R.array.project_technology_used_it)[i],
getResources().getStringArray(R.array.modules_of_project)[i]
);
arrayList.add(model);
}
presenter.insertData(arrayList);
}

//Todo add more button clicks when data is available

/*
Handles button click by checking IDs
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cseTag : break;
case R.id.itTag : presenter.openNewActivity(getString(R.string.it));
break;
case R.id.eceTag : break;
case R.id.eeeTag : break;
}
}


@Override
public void showNewActivity(String branchValue) {
Intent intent = new Intent(this, ListProjectActivity.class);
intent.putExtra(HomeScreenTag, branchValue);
startActivity(intent);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example.androidtechies.majorproject.Data;

import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;


import com.example.androidtechies.majorproject.Data.db.DbSource;
import com.example.androidtechies.majorproject.Data.pref.IPrefHelper;
import com.example.androidtechies.majorproject.Utils.LogAndToastUtil;

import java.util.List;

public class DataManager implements IDataManager {
//tag by class name
private static final String TAG = DataManager.class.getName();


private static volatile DataManager INSTANCE;

private DbSource dbSource;
private IPrefHelper prefHelper;

// Prevent direct instantiation.
private DataManager(DbSource dbSource, IPrefHelper prefHelper) {
this.dbSource = dbSource;
this.prefHelper = prefHelper;
}


public static DataManager getInstance(@NonNull Context context, @NonNull IPrefHelper prefHelper) {
if (INSTANCE == null) {
synchronized (DataManager.class) {
if (INSTANCE == null) {
INSTANCE = new DataManager(DbSource.getInstance(context), prefHelper);
}
}
}
return INSTANCE;
}



/*
preference Manager
*/

@Override
public boolean checkFirstTime() {
return prefHelper.checkFirstTime();
}

@Override
public void setFirstTime(boolean value) {
prefHelper.setFirstTime(value);
}


/*
SQLite database manager
*/

@Override
public Cursor getBranchProjects(@NonNull String branch) {
return dbSource.getBranchProjects(branch);
}

@Override
public void saveProject(@NonNull List<ProjectModel> projects) {
dbSource.saveProject(projects);
}

@Override
public long getCountProjects() {

return dbSource.getCountProjects();
}

@Override
public void nukeTable() {
LogAndToastUtil.Logging("nuke table function called!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.androidtechies.majorproject.Data;

import com.example.androidtechies.majorproject.Data.db.IDbSource;
import com.example.androidtechies.majorproject.Data.pref.IPrefHelper;

public interface IDataManager extends IPrefHelper, IDbSource {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.example.androidtechies.majorproject.Data;


import android.os.Parcel;
import android.os.Parcelable;

public class ProjectModel implements Parcelable {

private String projectBranch;
private String titleOfProject;
private String introOfProject;
private String technologyUsed;
private String modulesOfProject;


public ProjectModel(String projectBranch, String titleOfProject, String introOfProject, String technologyUsed, String modulesOfProject) {
this.projectBranch = projectBranch;
this.titleOfProject = titleOfProject;
this.introOfProject = introOfProject;
this.technologyUsed = technologyUsed;
this.modulesOfProject = modulesOfProject;
}

/*
Getters and Setters
*/
public String getProjectBranch() {
return projectBranch;
}

public void setProjectBranch(String projectBranch) {
this.projectBranch = projectBranch;
}

public String getTitleOfProject() {
return titleOfProject;
}

public void setTitleOfProject(String titleOfProject) {
this.titleOfProject = titleOfProject;
}

public String getIntroOfProject() {
return introOfProject;
}

public void setIntroOfProject(String introOfProject) {
this.introOfProject = introOfProject;
}

public String getTechnologyUsed() {
return technologyUsed;
}

public void setTechnologyUsed(String technologyUsed) {
this.technologyUsed = technologyUsed;
}

public String getModulesOfProject() {
return modulesOfProject;
}

public void setModulesOfProject(String modulesOfProject) {
this.modulesOfProject = modulesOfProject;
}

/*
parcellable functions
*/

@Override
public int describeContents() {
return hashCode();
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(titleOfProject);
parcel.writeString(introOfProject);
parcel.writeString(technologyUsed);
parcel.writeString(modulesOfProject);
}

protected ProjectModel(Parcel in) {
titleOfProject = in.readString();
introOfProject = in.readString();
technologyUsed = in.readString();
modulesOfProject = in.readString();
}

public static final Parcelable.Creator<ProjectModel> CREATOR = new Parcelable.Creator<ProjectModel>() {
@Override
public ProjectModel createFromParcel(Parcel in) {
return new ProjectModel(in);
}

@Override
public ProjectModel[] newArray(int size) {
return new ProjectModel[0];
}
};
}
Loading